Skip to main content

faker_rust/default/
x.rs

1//! X (Twitter) generator
2
3use crate::base::sample;
4use crate::locale::fetch_locale;
5
6/// Generate a random X/Twitter username
7pub fn screen_name() -> String {
8    fetch_locale("x.screen_names", "en")
9        .map(|v| sample(&v))
10        .unwrap_or_else(|| sample(FALLBACK_SCREEN_NAMES).to_string())
11}
12
13/// Generate a random X/Twitter hashtag
14pub fn hashtag() -> String {
15    fetch_locale("x.hashtags", "en")
16        .map(|v| sample(&v))
17        .unwrap_or_else(|| sample(FALLBACK_HASHTAGS).to_string())
18}
19
20/// Generate a random X/Twitter tweet text
21pub fn tweet() -> String {
22    fetch_locale("x.tweets", "en")
23        .map(|v| sample(&v))
24        .unwrap_or_else(|| sample(FALLBACK_TWEETS).to_string())
25}
26
27// Fallback data
28const FALLBACK_SCREEN_NAMES: &[&str] = &[
29    "@elonmusk", "@nasa", "@rustlang", "@github", "@stackoverflow",
30    "@techcrunch", "@wired", "@verge", "@engadget", "@arstechnica",
31    "@mozilla", "@google", "@apple", "@microsoft", "@amazon",
32];
33
34const FALLBACK_HASHTAGS: &[&str] = &[
35    "#Rust", "#Programming", "#Tech", "#AI", "#MachineLearning",
36    "#WebDev", "#OpenSource", "#Coding", "#DevOps", "#Cloud",
37    "#CyberSecurity", "#DataScience", "#100DaysOfCode", "#TechNews",
38];
39
40const FALLBACK_TWEETS: &[&str] = &[
41    "Just shipped a new feature! 🚀",
42    "Learning Rust has been an amazing journey.",
43    "Open source is the future of software.",
44    "Working on something exciting today! 💻",
45    "Code reviews are essential for quality.",
46    "Debugging is like being a detective.",
47];
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_screen_name() {
55        let name = screen_name();
56        assert!(name.starts_with('@'));
57    }
58
59    #[test]
60    fn test_hashtag() {
61        let tag = hashtag();
62        assert!(tag.starts_with('#'));
63    }
64
65    #[test]
66    fn test_tweet() {
67        assert!(!tweet().is_empty());
68    }
69}