From 757090d32980564896d79301152490dc9b24bbb3 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 30 May 2023 10:56:39 +0530 Subject: fix an assortment of bugs - fix entry ordering for non-chronological rss feeds - fix link shortening by stripping http - begin work on adding more than one feed --- src/feed.rs | 7 ++++--- src/lib.rs | 17 ----------------- src/main.rs | 23 ++++++++++++++++++++--- src/manager.rs | 25 +++++++++++++++++-------- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/feed.rs b/src/feed.rs index f9a7893..77f1a45 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -104,6 +104,7 @@ impl Feed { // update entries self.entries = entries.into_iter().map(Result::unwrap).collect(); + self.entries.sort_by(|a, b| b.published.cmp(&a.published)); Ok(pull_status) } @@ -166,16 +167,16 @@ impl fmt::Display for Entry { f, "{} {} {}", self.published.format(crate::DATE_FMT), + Style::new().dimmed().paint(self.title.to_ascii_lowercase()), Style::new().fg(Color::Cyan).paint( self.link .as_str() .trim_end_matches('/') - .trim_start_matches("http://") - .trim_start_matches("https://") .trim_start_matches("http://www.") .trim_start_matches("https://www.") + .trim_start_matches("https://") + .trim_start_matches("http://") ), - Style::new().dimmed().paint(self.title.to_ascii_lowercase()), ) } } diff --git a/src/lib.rs b/src/lib.rs index 7376d44..5b2a501 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,20 +5,3 @@ pub mod manager; pub mod status; pub(crate) static DATE_FMT: &str = "%e %b %y"; - -pub trait PrintResult { - fn print(&self); -} - -impl PrintResult for Result -where - T: std::fmt::Display, - E: std::fmt::Display, -{ - fn print(&self) { - match self { - Ok(ok) => println!("{ok}"), - Err(err) => eprintln!("{err}"), - } - } -} diff --git a/src/main.rs b/src/main.rs index 8c94843..b36e08f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use chrono::{naive::Days, prelude::*}; use clap::{Args, Parser, Subcommand, ValueEnum}; -use syn::{manager::Manager, PrintResult}; +use syn::manager::Manager; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -29,7 +29,7 @@ impl Default for Command { #[derive(Args)] struct AddCommand { - url: String, + urls: Vec, } #[derive(Args)] @@ -49,11 +49,28 @@ struct PullCommand { target: Option, } +pub trait PrintResult { + fn print(&self); +} + +impl PrintResult for Result +where + T: std::fmt::Display, + E: std::fmt::Display, +{ + fn print(&self) { + match self { + Ok(ok) => println!("{ok}"), + Err(err) => eprintln!("{err}"), + } + } +} + #[tokio::main(flavor = "current_thread")] async fn main() { let opts = Cli::parse(); match &opts.command.unwrap_or_default() { - Command::Add(AddCommand { url }) => { + Command::Add(AddCommand { urls }) => { let mut manager = Manager::load().unwrap_or_else(|e| { eprintln!("{e}"); Manager::default() diff --git a/src/manager.rs b/src/manager.rs index 34e19be..5330d80 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -13,7 +13,7 @@ pub struct Manager { } impl Manager { - pub async fn add_feed(&mut self, url: &str) -> Result { + fn add_feed(&mut self, url: &str) -> Result<&mut Feed, Error> { let link = Url::parse(&url).map_err(|e| AddError::InvalidUrl(e.to_string()))?; // check if this feed is already present @@ -22,17 +22,24 @@ impl Manager { } // construct a new feed - let mut feed = Feed::new(link.clone()); - - let status = feed - .pull() - .await - .map_err(|pull_err| Error::Pull(link, pull_err))?; + let feed = Feed::new(link.clone()); // add new feed self.feeds.push(feed); - Ok(status) + Ok(self.feeds.last_mut().unwrap()) + } + + pub async fn add_feed_and_pull(&mut self, url: &str) -> Result { + let feed = self.add_feed(url)?; + 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 pull(&mut self) -> Vec> { @@ -71,6 +78,8 @@ impl Manager { } } +// an iterator over a combined list of feeds (assumes each feed is already a sorted list of +// entries) struct EntryIterator<'e> { all_entries: Vec<&'e [Entry]>, } -- cgit v1.2.3