Skip to main content

faker_rust/default/
avatar.rs

1//! Avatar generator - generates random avatar characters, quotes, and image URLs
2
3use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random Avatar character (from the movie)
7pub fn character() -> String {
8    fetch_locale_with_context("avatar.characters", "en", Some("avatar"))
9        .map(|v| sample_with_resolve(&v, Some("avatar")))
10        .unwrap_or_else(|| sample(FALLBACK_CHARACTERS).to_string())
11}
12
13/// Generate a random Avatar quote
14pub fn quote() -> String {
15    fetch_locale_with_context("avatar.quotes", "en", Some("avatar"))
16        .map(|v| sample_with_resolve(&v, Some("avatar")))
17        .unwrap_or_else(|| sample(FALLBACK_QUOTES).to_string())
18}
19
20/// Generate a random avatar image URL using RoboHash
21pub fn image(slug: Option<&str>, size: Option<&str>, format: Option<&str>) -> String {
22    let slug = slug.unwrap_or("avatar");
23    let size = size.unwrap_or("300x300");
24    let format = format.unwrap_or("png");
25    format!("https://robohash.org/{}.{}?size={}", slug, format, size)
26}
27
28const FALLBACK_CHARACTERS: &[&str] = &["Neytiri", "Jake Sully", "Miles Quaritch"];
29const FALLBACK_QUOTES: &[&str] = &["I see you.", "This is our land!"];
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_character() {
37        assert!(!character().is_empty());
38    }
39
40    #[test]
41    fn test_image() {
42        let url = image(None, None, None);
43        assert!(url.contains("robohash.org"));
44    }
45}