diff options
author | Akshay <[email protected]> | 2023-06-15 20:03:31 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2023-06-15 20:03:31 +0100 |
commit | 7b5598d0de12573205415136fbb9aa467e30fa8a (patch) | |
tree | d18237265744fbbb9d3a1bf2ae888d2a8d267eb2 /src | |
parent | 757090d32980564896d79301152490dc9b24bbb3 (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 14 | ||||
-rw-r--r-- | src/manager.rs | 26 | ||||
-rw-r--r-- | src/status.rs | 4 |
3 files changed, 35 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index b36e08f..48617b2 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -76,7 +76,13 @@ async fn main() { | |||
76 | Manager::default() | 76 | Manager::default() |
77 | }); | 77 | }); |
78 | 78 | ||
79 | manager.add_feed(&url).await.print(); | 79 | let (status, errors): (Vec<_>, Vec<_>) = manager |
80 | .add_feeds_and_pull(&urls) | ||
81 | .await | ||
82 | .into_iter() | ||
83 | .partition(Result::is_ok); | ||
84 | status.iter().for_each(PrintResult::print); | ||
85 | errors.iter().for_each(PrintResult::print); | ||
80 | manager.store().print(); | 86 | manager.store().print(); |
81 | } | 87 | } |
82 | Command::ListFeeds => { | 88 | Command::ListFeeds => { |
@@ -107,7 +113,11 @@ async fn main() { | |||
107 | }); | 113 | }); |
108 | let (status, errors): (Vec<_>, Vec<_>) = | 114 | let (status, errors): (Vec<_>, Vec<_>) = |
109 | manager.pull().await.into_iter().partition(Result::is_ok); | 115 | manager.pull().await.into_iter().partition(Result::is_ok); |
110 | status.iter().for_each(PrintResult::print); | 116 | status |
117 | .into_iter() | ||
118 | .map(Result::unwrap) | ||
119 | .filter(|s| !s.is_empty()) | ||
120 | .for_each(|s| println!("{s}")); | ||
111 | errors.iter().for_each(PrintResult::print); | 121 | errors.iter().for_each(PrintResult::print); |
112 | manager.store().print(); | 122 | manager.store().print(); |
113 | } | 123 | } |
diff --git a/src/manager.rs b/src/manager.rs index 5330d80..83e8c1b 100644 --- a/src/manager.rs +++ b/src/manager.rs | |||
@@ -13,8 +13,8 @@ pub struct Manager { | |||
13 | } | 13 | } |
14 | 14 | ||
15 | impl Manager { | 15 | impl Manager { |
16 | fn add_feed(&mut self, url: &str) -> Result<&mut Feed, Error> { | 16 | fn add_feed<P: AsRef<str>>(&mut self, url: P) -> Result<(), Error> { |
17 | let link = Url::parse(&url).map_err(|e| AddError::InvalidUrl(e.to_string()))?; | 17 | let link = Url::parse(url.as_ref()).map_err(|e| AddError::InvalidUrl(e.to_string()))?; |
18 | 18 | ||
19 | // check if this feed is already present | 19 | // check if this feed is already present |
20 | if self.feeds.iter().any(|f| f.link == link) { | 20 | if self.feeds.iter().any(|f| f.link == link) { |
@@ -27,19 +27,31 @@ impl Manager { | |||
27 | // add new feed | 27 | // add new feed |
28 | self.feeds.push(feed); | 28 | self.feeds.push(feed); |
29 | 29 | ||
30 | Ok(self.feeds.last_mut().unwrap()) | 30 | Ok(()) |
31 | } | 31 | } |
32 | 32 | ||
33 | pub async fn add_feed_and_pull(&mut self, url: &str) -> Result<PullStatus, Error> { | 33 | pub async fn add_feed_and_pull(&mut self, url: &str) -> Result<PullStatus, Error> { |
34 | let feed = self.add_feed(url)?; | 34 | self.add_feed(url)?; |
35 | let feed = self.feeds.last_mut().unwrap(); | ||
35 | feed.pull() | 36 | feed.pull() |
36 | .await | 37 | .await |
37 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) | 38 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) |
38 | } | 39 | } |
39 | 40 | ||
40 | pub async fn add_feeds_and_pull(&mut self, urls: &[&str]) -> Vec<Result<PullStatus, Error>> { | 41 | pub async fn add_feeds_and_pull<P: AsRef<str>>( |
41 | let add_results = urls.into_iter().map(|url| self.add_feed(&url)); | 42 | &mut self, |
42 | self.pull() | 43 | urls: &[P], |
44 | ) -> Vec<Result<PullStatus, Error>> { | ||
45 | for url in urls { | ||
46 | // TODO: handle this error | ||
47 | self.add_feed(&url); | ||
48 | } | ||
49 | futures::future::join_all(self.feeds.iter_mut().map(|feed| async { | ||
50 | feed.pull() | ||
51 | .await | ||
52 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) | ||
53 | })) | ||
54 | .await | ||
43 | } | 55 | } |
44 | 56 | ||
45 | pub async fn pull(&mut self) -> Vec<Result<PullStatus, PullError>> { | 57 | pub async fn pull(&mut self) -> Vec<Result<PullStatus, PullError>> { |
diff --git a/src/status.rs b/src/status.rs index 205e2a9..4479a69 100644 --- a/src/status.rs +++ b/src/status.rs | |||
@@ -27,6 +27,10 @@ impl PullStatus { | |||
27 | 27 | ||
28 | impl fmt::Display for PullStatus { | 28 | impl fmt::Display for PullStatus { |
29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
30 | if self.is_empty() { | ||
31 | return Ok(()); | ||
32 | } | ||
33 | |||
30 | write!( | 34 | write!( |
31 | f, | 35 | f, |
32 | "{}", | 36 | "{}", |