Skip to main content

faker_rust/default/
superhero.rs

1//! Superhero generator
2
3use crate::base::sample;
4use crate::locale::fetch_locale;
5
6/// Generate a random superhero name
7pub fn name() -> String {
8    fetch_locale("superhero.names", "en")
9        .map(|v| sample(&v))
10        .unwrap_or_else(|| sample(FALLBACK_NAMES).to_string())
11}
12
13/// Generate a random superhero power
14pub fn power() -> String {
15    fetch_locale("superhero.powers", "en")
16        .map(|v| sample(&v))
17        .unwrap_or_else(|| sample(FALLBACK_POWERS).to_string())
18}
19
20/// Generate a random superhero prefix
21pub fn prefix() -> String {
22    fetch_locale("superhero.prefixes", "en")
23        .map(|v| sample(&v))
24        .unwrap_or_else(|| sample(FALLBACK_PREFIXES).to_string())
25}
26
27/// Generate a random superhero suffix
28pub fn suffix() -> String {
29    fetch_locale("superhero.suffixes", "en")
30        .map(|v| sample(&v))
31        .unwrap_or_else(|| sample(FALLBACK_SUFFIXES).to_string())
32}
33
34/// Generate a random superhero descriptor
35pub fn descriptor() -> String {
36    fetch_locale("superhero.descriptors", "en")
37        .map(|v| sample(&v))
38        .unwrap_or_else(|| sample(FALLBACK_DESCRIPTORS).to_string())
39}
40
41// Fallback data
42const FALLBACK_NAMES: &[&str] = &[
43    "Iron Man", "Captain America", "Thor", "Hulk", "Black Widow", "Hawkeye",
44    "Spider-Man", "Doctor Strange", "Black Panther", "Captain Marvel",
45    "Ant-Man", "Wasp", "Vision", "Scarlet Witch", "Falcon", "Winter Soldier",
46];
47
48const FALLBACK_POWERS: &[&str] = &[
49    "Super Strength", "Flight", "Invisibility", "Teleportation", "Telepathy",
50    "Shape Shifting", "Time Travel", "Elemental Control", "Healing Factor",
51    "Laser Vision", "Super Speed", "Invulnerability", "Mind Control",
52];
53
54const FALLBACK_PREFIXES: &[&str] = &[
55    "Captain", "Doctor", "Super", "Mega", "Ultra", "Dark", "Silver",
56    "Iron", "Green", "Black", "Red", "Blue", "Golden", "The Amazing",
57];
58
59const FALLBACK_SUFFIXES: &[&str] = &[
60    "Man", "Woman", "Girl", "Boy", "Knight", "Wing", "Storm", "Fire",
61    "Ice", "Thunder", "Shadow", "Light", "Guardian", "Protector",
62];
63
64const FALLBACK_DESCRIPTORS: &[&str] = &[
65    "The Mighty", "The Invincible", "The Unstoppable", "The Brave",
66    "The Fearless", "The Legendary", "The Eternal", "The Radiant",
67    "The Shadowy", "The Cosmic", "The Atomic", "The Mystic",
68];
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_name() {
76        assert!(!name().is_empty());
77    }
78
79    #[test]
80    fn test_power() {
81        assert!(!power().is_empty());
82    }
83
84    #[test]
85    fn test_prefix() {
86        assert!(!prefix().is_empty());
87    }
88
89    #[test]
90    fn test_suffix() {
91        assert!(!suffix().is_empty());
92    }
93
94    #[test]
95    fn test_descriptor() {
96        assert!(!descriptor().is_empty());
97    }
98}