Skip to main content

faker_rust/default/
crypto.rs

1//! Crypto generator - generates random cryptocurrency coins, names, and symbols
2
3use crate::base::sample;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random cryptocurrency coin (e.g. "Bitcoin")
7pub fn coin() -> String {
8    fetch_locale_with_context("crypto_coin.coin", "en", Some("crypto_coin"))
9        .map(|v| sample_with_resolve(&v, Some("crypto_coin")))
10        .unwrap_or_else(|| sample(FALLBACK_COINS).to_string())
11}
12
13/// Generate a random cryptocurrency name (e.g. "Bitcoin")
14pub fn name() -> String {
15    coin()
16}
17
18/// Generate a random cryptocurrency symbol (e.g. "BTC")
19pub fn symbol() -> String {
20    fetch_locale_with_context("crypto_coin.symbol", "en", Some("crypto_coin"))
21        .map(|v| sample_with_resolve(&v, Some("crypto_coin")))
22        .unwrap_or_else(|| sample(FALLBACK_SYMBOLS).to_string())
23}
24
25// Fallback data
26const FALLBACK_COINS: &[&str] = &["Bitcoin", "Ethereum", "Litecoin", "Ripple", "Dogecoin"];
27const FALLBACK_SYMBOLS: &[&str] = &["BTC", "ETH", "LTC", "XRP", "DOGE"];
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_coin() {
35        assert!(!coin().is_empty());
36    }
37
38    #[test]
39    fn test_symbol() {
40        assert!(!symbol().is_empty());
41    }
42}