1
Fork 0

Add the default image parameter.

This commit is contained in:
Bauke 2022-04-02 21:39:20 +02:00
parent 2ad90c785c
commit a5496fc1f2
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 26 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#[derive(Debug)]
pub struct Generator {
pub base_url: String,
pub default_image: Option<String>,
pub image_size: Option<i32>,
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=<default image>` 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=<image size>` in the URL.
///
/// ```rust