Skip to main content

faker_rust/default/
app.rs

1//! App generator - generates random app names, versions, and authors
2
3use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random app name
7pub fn name() -> String {
8    fetch_locale_with_context("app.name", "en", Some("app"))
9        .map(|v| sample_with_resolve(&v, Some("app")))
10        .unwrap_or_else(|| sample(FALLBACK_NAMES).to_string())
11}
12
13/// Generate a random app version
14pub fn version() -> String {
15    fetch_locale_with_context("app.version", "en", Some("app"))
16        .map(|v| sample_with_resolve(&v, Some("app")))
17        .unwrap_or_else(|| sample(FALLBACK_VERSIONS).to_string())
18}
19
20/// Generate a random app author
21pub fn author() -> String {
22    fetch_locale_with_context("app.author", "en", Some("app"))
23        .map(|v| sample_with_resolve(&v, Some("app")))
24        .unwrap_or_else(crate::name::name)
25}
26
27// Fallback data
28const FALLBACK_NAMES: &[&str] = &["Redhold", "Treeflex", "Tri-tip", "Greenlam"];
29const FALLBACK_VERSIONS: &[&str] = &["0.1.0", "1.0.0", "2.1.3", "0.9.9"];
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_name() {
37        assert!(!name().is_empty());
38    }
39
40    #[test]
41    fn test_version() {
42        assert!(!version().is_empty());
43    }
44}