bbc_news_cli/
feeds.rs

1#[derive(Debug, Clone)]
2pub struct Feed {
3    pub name: String,
4    pub url: String,
5}
6
7impl Feed {
8    pub fn new(name: &str, url: &str) -> Self {
9        Self {
10            name: name.to_string(),
11            url: url.to_string(),
12        }
13    }
14}
15
16pub fn get_all_feeds() -> Vec<Feed> {
17    vec![
18        Feed::new("Top Stories", "https://feeds.bbci.co.uk/news/rss.xml"),
19        Feed::new("World", "https://feeds.bbci.co.uk/news/world/rss.xml"),
20        Feed::new("UK", "https://feeds.bbci.co.uk/news/uk/rss.xml"),
21        Feed::new("Business", "https://feeds.bbci.co.uk/news/business/rss.xml"),
22        Feed::new("Politics", "https://feeds.bbci.co.uk/news/politics/rss.xml"),
23        Feed::new("Health", "https://feeds.bbci.co.uk/news/health/rss.xml"),
24        Feed::new("Education & Family", "https://feeds.bbci.co.uk/news/education/rss.xml"),
25        Feed::new("Science & Environment", "https://feeds.bbci.co.uk/news/science_and_environment/rss.xml"),
26        Feed::new("Technology", "https://feeds.bbci.co.uk/news/technology/rss.xml"),
27        Feed::new("Entertainment & Arts", "https://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml"),
28        Feed::new("England", "https://feeds.bbci.co.uk/news/england/rss.xml"),
29        Feed::new("Northern Ireland", "https://feeds.bbci.co.uk/news/northern_ireland/rss.xml"),
30        Feed::new("Scotland", "https://feeds.bbci.co.uk/news/scotland/rss.xml"),
31        Feed::new("Wales", "https://feeds.bbci.co.uk/news/wales/rss.xml"),
32        Feed::new("Africa", "https://feeds.bbci.co.uk/news/world/africa/rss.xml"),
33        Feed::new("Asia", "https://feeds.bbci.co.uk/news/world/asia/rss.xml"),
34        Feed::new("Europe", "https://feeds.bbci.co.uk/news/world/europe/rss.xml"),
35        Feed::new("Latin America", "https://feeds.bbci.co.uk/news/world/latin_america/rss.xml"),
36        Feed::new("Middle East", "https://feeds.bbci.co.uk/news/world/middle_east/rss.xml"),
37        Feed::new("US & Canada", "https://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml"),
38    ]
39}
40
41pub fn get_default_feed() -> Feed {
42    Feed::new("Top Stories", "https://feeds.bbci.co.uk/news/rss.xml")
43}
44
45pub fn get_feed_by_name(name: &str) -> anyhow::Result<Feed> {
46    let name_lower = name.to_lowercase();
47    let feeds = get_all_feeds();
48
49    // Try exact match first (case-insensitive)
50    for feed in &feeds {
51        if feed.name.to_lowercase() == name_lower {
52            return Ok(feed.clone());
53        }
54    }
55
56    // Try partial match (shortcuts)
57    for feed in &feeds {
58        if feed.name.to_lowercase().contains(&name_lower) {
59            return Ok(feed.clone());
60        }
61    }
62
63    // Try common shortcuts
64    let feed = match name_lower.as_str() {
65        "tech" => Feed::new("Technology", "https://feeds.bbci.co.uk/news/technology/rss.xml"),
66        "biz" | "business" => Feed::new("Business", "https://feeds.bbci.co.uk/news/business/rss.xml"),
67        "pol" | "politics" => Feed::new("Politics", "https://feeds.bbci.co.uk/news/politics/rss.xml"),
68        "sci" | "science" => Feed::new("Science & Environment", "https://feeds.bbci.co.uk/news/science_and_environment/rss.xml"),
69        "entertainment" | "ent" => Feed::new("Entertainment & Arts", "https://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml"),
70        "edu" | "education" => Feed::new("Education & Family", "https://feeds.bbci.co.uk/news/education/rss.xml"),
71        _ => anyhow::bail!("Unknown feed: '{}'. Use 'world', 'uk', 'business', 'technology', etc.", name),
72    };
73
74    Ok(feed)
75}