fetchium_core/proactive/
digest.rs1use crate::error::FetchiumResult;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Digest {
9 pub period: DigestPeriod,
10 pub topics: Vec<String>,
11 pub sections: Vec<DigestSection>,
12 pub generated_at: String,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum DigestPeriod {
18 Daily,
19 Weekly,
20 Monthly,
21}
22
23impl DigestPeriod {
24 pub fn parse(s: &str) -> Self {
25 match s.to_ascii_lowercase().as_str() {
26 "daily" => DigestPeriod::Daily,
27 "monthly" => DigestPeriod::Monthly,
28 _ => DigestPeriod::Weekly,
29 }
30 }
31
32 pub fn label(&self) -> &str {
33 match self {
34 DigestPeriod::Daily => "Daily",
35 DigestPeriod::Weekly => "Weekly",
36 DigestPeriod::Monthly => "Monthly",
37 }
38 }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct DigestSection {
43 pub topic: String,
44 pub headline: String,
45 pub items: Vec<DigestItem>,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct DigestItem {
50 pub title: String,
51 pub url: String,
52 pub snippet: String,
53 pub published: Option<String>,
54}
55
56pub struct DigestBuilder {
58 pub period: DigestPeriod,
59 pub topics: Vec<String>,
60 sections: Vec<DigestSection>,
61}
62
63impl DigestBuilder {
64 pub fn new(period: DigestPeriod, topics: Vec<String>) -> Self {
65 Self {
66 period,
67 topics,
68 sections: Vec::new(),
69 }
70 }
71
72 pub fn add_section(&mut self, topic: &str, items: Vec<crate::types::ResultItem>) {
73 if items.is_empty() {
74 return;
75 }
76 let headline = format!("{} updates in \"{}\"", items.len(), topic);
77 let digest_items: Vec<DigestItem> = items
78 .into_iter()
79 .map(|r| DigestItem {
80 title: r.title,
81 url: r.url,
82 snippet: r.snippet,
83 published: r.published_date,
84 })
85 .collect();
86 self.sections.push(DigestSection {
87 topic: topic.to_string(),
88 headline,
89 items: digest_items,
90 });
91 }
92
93 pub fn build(self) -> Digest {
94 Digest {
95 period: self.period,
96 topics: self.topics,
97 sections: self.sections,
98 generated_at: chrono::Utc::now().to_rfc3339(),
99 }
100 }
101}
102
103impl Digest {
104 pub fn to_markdown(&self) -> String {
105 let mut md = format!(
106 "# {} Fetchium Digest\n_Generated: {}_\n\n",
107 self.period.label(),
108 self.generated_at
109 );
110 if self.sections.is_empty() {
111 md.push_str("_No new findings for your topics this period._\n");
112 return md;
113 }
114 for section in &self.sections {
115 md.push_str(&format!("## {}\n_{}_\n\n", section.topic, section.headline));
116 for item in §ion.items {
117 md.push_str(&format!(
118 "- **[{}]({})** — {}\n",
119 item.title, item.url, item.snippet
120 ));
121 }
122 md.push('\n');
123 }
124 md
125 }
126
127 pub fn to_json(&self) -> FetchiumResult<String> {
128 Ok(serde_json::to_string_pretty(self)?)
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135
136 #[test]
137 fn test_digest_markdown_empty() {
138 let d = DigestBuilder::new(DigestPeriod::Weekly, vec!["rust".into()]).build();
139 let md = d.to_markdown();
140 assert!(md.contains("No new findings"));
141 }
142}