2022-04-02 12:30:33 +00:00
|
|
|
//! # gravatar-rs
|
|
|
|
//!
|
|
|
|
//! This crate provides an API for creating [Gravatar image URLs], and by
|
|
|
|
//! extension, [Ivatar/Libravatar image URLs].
|
|
|
|
//!
|
|
|
|
//! [Gravatar image URLs]: https://gravatar.com/site/implement/images/
|
|
|
|
//! [Ivatar/Libravatar image URLs]: https://wiki.libravatar.org/api/
|
|
|
|
//!
|
|
|
|
//! The default [`Generator::base_url`] is `www.gravatar.com`, if you want to
|
|
|
|
//! set a custom `base_url` use [`Generator::set_base_url`].
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use gravatar_rs::Generator;
|
|
|
|
//!
|
|
|
|
//! let generator = Generator::default();
|
|
|
|
//!
|
|
|
|
//! let gravatar_url = generator.generate("helllo@holllo.cc");
|
|
|
|
//!
|
|
|
|
//! assert_eq!(
|
|
|
|
//! gravatar_url,
|
|
|
|
//! "https://www.gravatar.com/avatar/ebff9105dce4954b1bdb57fdab079ff3"
|
|
|
|
//! );
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
/// A generator for Gravatar image URLs.
|
2022-04-02 12:05:23 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Generator {
|
|
|
|
pub base_url: String,
|
2022-04-02 15:01:00 +00:00
|
|
|
pub image_size: Option<i32>,
|
2022-04-02 12:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Generator {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
base_url: "www.gravatar.com".to_string(),
|
2022-04-02 15:01:00 +00:00
|
|
|
image_size: None,
|
2022-04-02 12:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Generator {
|
2022-04-02 12:30:33 +00:00
|
|
|
/// Hashes an email with [`md5`] according to the [Gravatar hashing steps].
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use gravatar_rs::Generator;
|
|
|
|
///
|
|
|
|
/// let hash = Generator::hash_email("helllo@holllo.cc");
|
|
|
|
///
|
|
|
|
/// assert_eq!(
|
|
|
|
/// hash,
|
|
|
|
/// "ebff9105dce4954b1bdb57fdab079ff3"
|
|
|
|
/// );
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [Gravatar hashing steps]: https://en.gravatar.com/site/implement/hash/
|
2022-04-02 12:05:23 +00:00
|
|
|
pub fn hash_email(email: &str) -> String {
|
|
|
|
let hash = md5::compute(email.trim().to_lowercase());
|
|
|
|
format!("{hash:x}")
|
|
|
|
}
|
|
|
|
|
2022-04-02 12:30:33 +00:00
|
|
|
/// Generates a new Gravatar image URL using the Generator's configuration.
|
|
|
|
///
|
|
|
|
/// See the top-level module documentation for examples.
|
2022-04-02 12:05:23 +00:00
|
|
|
pub fn generate(&self, email: &str) -> String {
|
|
|
|
let base_url = &self.base_url;
|
|
|
|
let hash = Self::hash_email(email);
|
|
|
|
|
|
|
|
format!("https://{base_url}/avatar/{hash}")
|
|
|
|
}
|
|
|
|
|
2022-04-02 12:34:31 +00:00
|
|
|
/// Configures the Generator to use a custom base URL for generated URLs.
|
2022-04-02 12:30:33 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use gravatar_rs::Generator;
|
|
|
|
///
|
|
|
|
/// // Use Libravatar instead of Gravatar.
|
|
|
|
/// Generator::default().set_base_url("cdn.libravatar.org");
|
|
|
|
/// ```
|
2022-04-02 12:05:23 +00:00
|
|
|
pub fn set_base_url(self, base_url: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
base_url: base_url.to_string(),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 15:01:00 +00:00
|
|
|
|
|
|
|
/// Configures the Generator to include a `s=<image size>` in the URL.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use gravatar_rs::Generator;
|
|
|
|
///
|
|
|
|
/// // Get 128px images instead of the default 80px.
|
|
|
|
/// Generator::default().set_image_size(128);
|
|
|
|
/// ```
|
|
|
|
pub fn set_image_size(self, image_size: i32) -> Self {
|
|
|
|
Self {
|
|
|
|
image_size: Some(image_size),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 12:05:23 +00:00
|
|
|
}
|