From 7b5598d0de12573205415136fbb9aa467e30fa8a Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 16 Jun 2023 00:33:31 +0530 Subject: status only for non-empty pulls --- src/main.rs | 14 ++++++++++++-- src/manager.rs | 26 +++++++++++++++++++------- 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() { Manager::default() }); - manager.add_feed(&url).await.print(); + let (status, errors): (Vec<_>, Vec<_>) = manager + .add_feeds_and_pull(&urls) + .await + .into_iter() + .partition(Result::is_ok); + status.iter().for_each(PrintResult::print); + errors.iter().for_each(PrintResult::print); manager.store().print(); } Command::ListFeeds => { @@ -107,7 +113,11 @@ async fn main() { }); let (status, errors): (Vec<_>, Vec<_>) = manager.pull().await.into_iter().partition(Result::is_ok); - status.iter().for_each(PrintResult::print); + status + .into_iter() + .map(Result::unwrap) + .filter(|s| !s.is_empty()) + .for_each(|s| println!("{s}")); errors.iter().for_each(PrintResult::print); manager.store().print(); } 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 { } impl Manager { - fn add_feed(&mut self, url: &str) -> Result<&mut Feed, Error> { - let link = Url::parse(&url).map_err(|e| AddError::InvalidUrl(e.to_string()))?; + fn add_feed>(&mut self, url: P) -> Result<(), Error> { + let link = Url::parse(url.as_ref()).map_err(|e| AddError::InvalidUrl(e.to_string()))?; // check if this feed is already present if self.feeds.iter().any(|f| f.link == link) { @@ -27,19 +27,31 @@ impl Manager { // add new feed self.feeds.push(feed); - Ok(self.feeds.last_mut().unwrap()) + Ok(()) } pub async fn add_feed_and_pull(&mut self, url: &str) -> Result { - let feed = self.add_feed(url)?; + self.add_feed(url)?; + let feed = self.feeds.last_mut().unwrap(); feed.pull() .await .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) } - pub async fn add_feeds_and_pull(&mut self, urls: &[&str]) -> Vec> { - let add_results = urls.into_iter().map(|url| self.add_feed(&url)); - self.pull() + pub async fn add_feeds_and_pull>( + &mut self, + urls: &[P], + ) -> Vec> { + for url in urls { + // TODO: handle this error + self.add_feed(&url); + } + futures::future::join_all(self.feeds.iter_mut().map(|feed| async { + feed.pull() + .await + .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) + })) + .await } pub async fn pull(&mut self) -> Vec> { 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 { impl fmt::Display for PullStatus { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_empty() { + return Ok(()); + } + write!( f, "{}", -- cgit v1.2.3