From 2ad90c785c9f67aa44bca18a4d77514d2795339b Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 2 Apr 2022 18:54:24 +0200 Subject: [PATCH] Add an option to include the JPG file extension. --- source/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/source/lib.rs b/source/lib.rs index 1e9c5db..88558c5 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -27,6 +27,7 @@ pub struct Generator { pub base_url: String, pub image_size: Option, + pub include_file_extension: bool, } impl Default for Generator { @@ -34,6 +35,7 @@ impl Default for Generator { Self { base_url: "www.gravatar.com".to_string(), image_size: None, + include_file_extension: false, } } } @@ -65,7 +67,16 @@ impl Generator { let base_url = &self.base_url; let hash = Self::hash_email(email); let query_parameters = self.query_parameters(); - format!("https://{base_url}/avatar/{hash}{query_parameters}") + + let file_extension = if self.include_file_extension { + ".jpg" + } else { + "" + }; + + format!( + "https://{base_url}/avatar/{hash}{file_extension}{query_parameters}" + ) } /// Returns all configurable options as a query parameter string. @@ -116,4 +127,21 @@ impl Generator { ..self } } + + /// Configures the Generator to add `.jpg` to the end of the hash. + /// + /// ```rust + /// use gravatar_rs::Generator; + /// + /// Generator::default().set_include_file_extension(true); + /// ``` + pub fn set_include_file_extension( + self, + include_file_extension: bool, + ) -> Self { + Self { + include_file_extension, + ..self + } + } }