Skip to main content

faker_rust/default/
currency.rs

1//! Currency generator - generates random currency names, codes, and symbols
2
3use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random currency name
7pub fn name() -> String {
8    fetch_locale_with_context("currency.name", "en", Some("currency"))
9        .map(|v| sample_with_resolve(&v, Some("currency")))
10        .unwrap_or_else(|| sample(FALLBACK_NAMES).to_string())
11}
12
13/// Generate a random currency code
14pub fn code() -> String {
15    fetch_locale_with_context("currency.code", "en", Some("currency"))
16        .map(|v| sample_with_resolve(&v, Some("currency")))
17        .unwrap_or_else(|| sample(FALLBACK_CODES).to_string())
18}
19
20/// Generate a random currency symbol
21pub fn symbol() -> String {
22    fetch_locale_with_context("currency.symbol", "en", Some("currency"))
23        .map(|v| sample_with_resolve(&v, Some("currency")))
24        .unwrap_or_else(|| sample(FALLBACK_SYMBOLS).to_string())
25}
26
27// Fallback data
28const FALLBACK_NAMES: &[&str] = &[
29    "US Dollar",
30    "Euro",
31    "Japanese Yen",
32    "British Pound",
33    "Bitcoin",
34];
35const FALLBACK_CODES: &[&str] = &["USD", "EUR", "JPY", "GBP", "BTC"];
36const FALLBACK_SYMBOLS: &[&str] = &["$", "€", "¥", "£", "₿"];
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_name() {
44        assert!(!name().is_empty());
45    }
46
47    #[test]
48    fn test_code() {
49        assert!(!code().is_empty());
50    }
51}