From a5496fc1f2febe983ea40480aef900ec94fdf006 Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 2 Apr 2022 21:39:20 +0200 Subject: [PATCH] Add the default image parameter. --- source/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/lib.rs b/source/lib.rs index 88558c5..2499fdd 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -26,6 +26,7 @@ #[derive(Debug)] pub struct Generator { pub base_url: String, + pub default_image: Option, pub image_size: Option, pub include_file_extension: bool, } @@ -34,6 +35,7 @@ impl Default for Generator { fn default() -> Self { Self { base_url: "www.gravatar.com".to_string(), + default_image: None, image_size: None, include_file_extension: false, } @@ -87,6 +89,10 @@ impl Generator { let mut query_parameters = vec![]; + if let Some(default_image) = &self.default_image { + query_parameters.push(format!("d={}", encode(default_image))); + } + if let Some(image_size) = self.image_size { query_parameters.push(format!("s={}", encode(image_size))); } @@ -113,6 +119,26 @@ impl Generator { } } + /// Configures the Generator to include `d=` in the URL. + /// + /// See the [Gravatar documentation] for all the possible ways to use it. + /// + /// [Gravatar documentation]: https://gravatar.com/site/implement/images/#default-image + /// + /// ```rust + /// use gravatar_rs::Generator; + /// + /// // Use the "identicon" default image, a geometric pattern based on the + /// // email hash. + /// Generator::default().set_default_image("identicon"); + /// ``` + pub fn set_default_image(self, default_image: &str) -> Self { + Self { + default_image: Some(default_image.to_string()), + ..self + } + } + /// Configures the Generator to include a `s=` in the URL. /// /// ```rust