Skip to main content

faker_rust/default/
space.rs

1//! Space generator - generates random planets, moons, galaxies, and constellations
2
3use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random planet
7pub fn planet() -> String {
8    fetch_locale_with_context("space.planet", "en", Some("space"))
9        .map(|v| sample_with_resolve(&v, Some("space")))
10        .unwrap_or_else(|| sample(FALLBACK_PLANETS).to_string())
11}
12
13/// Generate a random moon
14pub fn moon() -> String {
15    fetch_locale_with_context("space.moon", "en", Some("space"))
16        .map(|v| sample_with_resolve(&v, Some("space")))
17        .unwrap_or_else(|| sample(FALLBACK_MOONS).to_string())
18}
19
20/// Generate a random galaxy
21pub fn galaxy() -> String {
22    fetch_locale_with_context("space.galaxy", "en", Some("space"))
23        .map(|v| sample_with_resolve(&v, Some("space")))
24        .unwrap_or_else(|| sample(FALLBACK_GALAXIES).to_string())
25}
26
27/// Generate a random constellation
28pub fn constellation() -> String {
29    fetch_locale_with_context("space.constellation", "en", Some("space"))
30        .map(|v| sample_with_resolve(&v, Some("space")))
31        .unwrap_or_else(|| sample(FALLBACK_CONSTELLATIONS).to_string())
32}
33
34// Fallback data
35const FALLBACK_PLANETS: &[&str] = &[
36    "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune",
37];
38const FALLBACK_MOONS: &[&str] = &[
39    "Moon", "Phobos", "Deimos", "Io", "Europa", "Ganymede", "Callisto",
40];
41const FALLBACK_GALAXIES: &[&str] = &["Andromeda", "Milky Way", "Triangulum", "Sombrero", "Cigar"];
42const FALLBACK_CONSTELLATIONS: &[&str] = &[
43    "Orion",
44    "Ursa Major",
45    "Ursa Minor",
46    "Canis Major",
47    "Cassiopeia",
48];
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_planet() {
56        assert!(!planet().is_empty());
57    }
58
59    #[test]
60    fn test_moon() {
61        assert!(!moon().is_empty());
62    }
63}