diff --git a/src/gemfeed.rs b/src/gemfeed.rs index 07a7ef6..a199561 100644 --- a/src/gemfeed.rs +++ b/src/gemfeed.rs @@ -19,13 +19,11 @@ static GEMFEED_POST_REGEX: Lazy = Lazy::new(|| Regex::new(r#"(\d\d\d\d-\d\d-\d\d)"#).unwrap()); fn is_gemfeed_post_link(node: &GemtextNode) -> bool { - if let GemtextNode::Link { - text: Some(title), .. - } = node - { - GEMFEED_POST_REGEX.is_match_at(title, 0) - } else { - false + match node { + GemtextNode::Link { + text: Some(title), .. + } => GEMFEED_POST_REGEX.is_match_at(title, 0), + _ => false, } } @@ -34,14 +32,11 @@ fn parse_gemfeed(base_url: &Url, gemfeed: &GemtextAst) -> Result Result> { +fn parse_atom(feed: &AtomFeed, settings: &GemfeedParserSettings) -> Result> { feed.entries() .into_iter() .map(|entry| GemfeedEntry::from_atom(entry, &settings.atom_date_format)) @@ -129,7 +124,7 @@ impl Gemfeed { pub fn load_with_settings(url: &Url, settings: &GemfeedParserSettings) -> Result { let resp = gemini_request(url)?; match GemfeedType::from(resp.meta()) { - GemfeedType::Gemtext => Self::load_from_gemtext(url, resp), + GemfeedType::Gemtext => Self::load_from_gemfeed(url, resp), GemfeedType::Atom => Self::load_from_atom(url, resp, &settings), _ => Err(anyhow!( "Unrecognized Gemfeed mime type [meta={}]", @@ -153,16 +148,15 @@ impl Gemfeed { } } - fn load_from_gemtext(url: &Url, resp: GeminiResponse) -> Result { + fn load_from_gemfeed(url: &Url, resp: GeminiResponse) -> Result { let maybe_feed = resp .content() .to_owned() .map(|text| GemtextAst::from_value(&text)); - if let Some(ref feed) = maybe_feed { - Self::load_from_ast(url, feed) - } else { - Err(anyhow!("Not a valid Gemfeed - could not parse gemtext")) + match maybe_feed { + Some(ref feed) => Self::load_from_ast(url, feed), + _ => Err(anyhow!("Not a valid Gemfeed - could not parse gemtext")), } } @@ -225,10 +219,10 @@ pub struct GemfeedEntry { #[allow(dead_code)] impl GemfeedEntry { - pub fn from_ast(base_url: &Url, node: &GemtextNode) -> Result { + pub fn from_gemtext(base_url: &Url, node: &GemtextNode) -> Result { let link = GemfeedLink::try_from(node)?; - // Gemfeeds have only the date--according to spec, it should be 12pm UTC. - println!("{:?}", link.published); + // Gemfeeds have only the date--according to spec, it should + // be 12pm UTC. let publish_date = link .published .map(|date| NaiveDate::parse_from_str(&date, "%Y-%m-%d")) @@ -289,12 +283,12 @@ impl GemfeedEntry { } pub fn body_mut(&mut self) -> Result<&mut String, Error> { - // Forces init and also returns the error if init failed. + // Forces init and also returns the error if init failed ... if let Err(error) = self.body() { return Err(error); } - // Which means that this Should Be Safe™ + // ... which means that this Should Be Safe™. Ok(self .body .get_mut() @@ -330,10 +324,9 @@ impl TryFrom<&GemtextNode> for GemfeedLink { to: path, } = node.to_owned() { - let re = Regex::new(r#"(\d\d\d\d-\d\d-\d\d)"#).unwrap(); let path_buf = PathBuf::from(&path); - let published: Option = re + let published: Option = GEMFEED_POST_REGEX .captures_at(&title, 0) .map(|caps| caps.get(0)) .and_then(|date| date.map(|published| published.as_str().to_owned())); diff --git a/src/wf.rs b/src/wf.rs index 73f1c7b..5c18251 100644 --- a/src/wf.rs +++ b/src/wf.rs @@ -49,7 +49,7 @@ impl WriteFreely { } /// Logs the client out and renders this instance of the wrapper - /// unusable.n + /// unusable. pub async fn logout(mut self) -> Result<()> { self.client.logout().await?; Ok(()) @@ -91,10 +91,11 @@ impl TryFrom<&GemfeedEntry> for PostCreateRequest { .title(entry.title()) .body(entry.body_as_markdown()?); - if let Some(publish_date) = published { - Ok(req.created(publish_date)) - } else { - Ok(req) - } + let req = match published { + Some(publish_date) => req.created(publish_date), + _ => req, + }; + + Ok(req) } } diff --git a/src/writefreely.rs b/src/writefreely.rs deleted file mode 100644 index 4fac9c6..0000000 --- a/src/writefreely.rs +++ /dev/null @@ -1,16 +0,0 @@ -use anyhow::{anyhow, Error, Result}; - -use writefreely_client::{ - post::{PostCreateRequest, Slug}, - Client, -}; - -pub async fn slugs_on_writefreely(client: &Client, alias: &str) -> Result> { - let posts = client.collections().posts(alias).list().await?; - let slugs: Vec<_> = posts - .into_iter() - .flat_map(|post| post.slug) - .map(|slug| slug.to_string()) - .collect(); - Ok(slugs) -}