Skip to main content

rss_proxy/
model.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct FeedItem {
6    pub id: String,
7    pub title: String,
8    pub link: String,
9    pub description: String,
10    pub content: Option<String>,
11    pub author: Option<String>,
12    pub published: Option<DateTime<Utc>>,
13    pub updated: Option<DateTime<Utc>>,
14    pub categories: Vec<String>,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct Feed {
19    pub title: String,
20    pub link: String,
21    pub description: String,
22    pub language: Option<String>,
23    pub items: Vec<FeedItem>,
24}
25
26impl Feed {
27    /// Convert parsed `feed_rs::model::Feed` to our clean internal `Feed` representation
28    pub fn from_feed_rs(parsed: feed_rs::model::Feed) -> Self {
29        let title = parsed
30            .title
31            .map(|t| t.content)
32            .unwrap_or_else(|| "Untitled Feed".to_string());
33
34        let link = parsed
35            .links
36            .first()
37            .map(|l| l.href.clone())
38            .unwrap_or_default();
39
40        let description = parsed
41            .description
42            .map(|d| d.content)
43            .unwrap_or_else(|| title.clone());
44
45        let language = parsed.language;
46
47        let items = parsed
48            .entries
49            .into_iter()
50            .map(|entry| {
51                let id = entry.id;
52                let title = entry
53                    .title
54                    .map(|t| t.content)
55                    .unwrap_or_else(|| "Untitled".to_string());
56
57                let link = entry
58                    .links
59                    .first()
60                    .map(|l| l.href.clone())
61                    .unwrap_or_default();
62
63                let description = entry.summary.map(|s| s.content).unwrap_or_else(|| {
64                    entry
65                        .content
66                        .as_ref()
67                        .and_then(|c| c.body.clone())
68                        .unwrap_or_default()
69                });
70
71                let content = entry.content.and_then(|c| c.body);
72
73                let author = entry.authors.first().map(|a| a.name.clone());
74
75                let published = entry.published;
76                let updated = entry.updated;
77
78                let categories = entry.categories.into_iter().map(|c| c.term).collect();
79
80                FeedItem {
81                    id,
82                    title,
83                    link,
84                    description,
85                    content,
86                    author,
87                    published,
88                    updated,
89                    categories,
90                }
91            })
92            .collect();
93
94        Self {
95            title,
96            link,
97            description,
98            language,
99            items,
100        }
101    }
102
103    /// Render internal Feed representation to RSS 2.0 XML
104    pub fn to_rss_xml(&self) -> Result<String, String> {
105        use rss::{ChannelBuilder, GuidBuilder, ItemBuilder};
106
107        let mut channel_builder = ChannelBuilder::default();
108        channel_builder
109            .title(self.title.clone())
110            .link(self.link.clone())
111            .description(self.description.clone());
112
113        if let Some(ref lang) = self.language {
114            channel_builder.language(Some(lang.clone()));
115        }
116
117        let rss_items: Vec<rss::Item> = self
118            .items
119            .iter()
120            .map(|item| {
121                let mut ib = ItemBuilder::default();
122                ib.title(Some(item.title.clone()))
123                    .link(Some(item.link.clone()))
124                    .description(Some(item.description.clone()));
125
126                let mut guid_builder = GuidBuilder::default();
127                guid_builder.value(if item.id.is_empty() {
128                    item.link.clone()
129                } else {
130                    item.id.clone()
131                });
132                guid_builder.permalink(item.id.starts_with("http"));
133                ib.guid(Some(guid_builder.build()));
134
135                if let Some(pub_date) = item.published.or(item.updated) {
136                    ib.pub_date(Some(pub_date.to_rfc2822()));
137                }
138
139                if let Some(ref author) = item.author {
140                    ib.author(Some(author.clone()));
141                }
142
143                let categories: Vec<rss::Category> = item
144                    .categories
145                    .iter()
146                    .map(|c| {
147                        let mut cat = rss::Category::default();
148                        cat.set_name(c.clone());
149                        cat
150                    })
151                    .collect();
152                ib.categories(categories);
153
154                ib.build()
155            })
156            .collect();
157
158        channel_builder.items(rss_items);
159        let channel = channel_builder.build();
160        Ok(channel.to_string())
161    }
162}