Code cleanup and fixing typos
continuous-integration/drone/push Build is passing Details

This commit is contained in:
projectmoon 2024-03-22 11:31:51 +01:00
parent 31a875e4a5
commit d4f9e7f882
3 changed files with 25 additions and 47 deletions

View File

@ -19,13 +19,11 @@ static GEMFEED_POST_REGEX: Lazy<regex::Regex> =
Lazy::new(|| Regex::new(r#"(\d\d\d\d-\d\d-\d\d)"#).unwrap()); Lazy::new(|| Regex::new(r#"(\d\d\d\d-\d\d-\d\d)"#).unwrap());
fn is_gemfeed_post_link(node: &GemtextNode) -> bool { fn is_gemfeed_post_link(node: &GemtextNode) -> bool {
if let GemtextNode::Link { match node {
text: Some(title), .. GemtextNode::Link {
} = node text: Some(title), ..
{ } => GEMFEED_POST_REGEX.is_match_at(title, 0),
GEMFEED_POST_REGEX.is_match_at(title, 0) _ => false,
} else {
false
} }
} }
@ -34,14 +32,11 @@ fn parse_gemfeed(base_url: &Url, gemfeed: &GemtextAst) -> Result<Vec<GemfeedEntr
.inner() .inner()
.into_iter() .into_iter()
.filter(|node| is_gemfeed_post_link(node)) .filter(|node| is_gemfeed_post_link(node))
.map(|node| GemfeedEntry::from_ast(base_url, node)) .map(|node| GemfeedEntry::from_gemtext(base_url, node))
.collect() .collect()
} }
fn parse_atom( fn parse_atom(feed: &AtomFeed, settings: &GemfeedParserSettings) -> Result<Vec<GemfeedEntry>> {
feed: &AtomFeed,
settings: &GemfeedParserSettings,
) -> Result<Vec<GemfeedEntry>> {
feed.entries() feed.entries()
.into_iter() .into_iter()
.map(|entry| GemfeedEntry::from_atom(entry, &settings.atom_date_format)) .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<Gemfeed> { pub fn load_with_settings(url: &Url, settings: &GemfeedParserSettings) -> Result<Gemfeed> {
let resp = gemini_request(url)?; let resp = gemini_request(url)?;
match GemfeedType::from(resp.meta()) { 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), GemfeedType::Atom => Self::load_from_atom(url, resp, &settings),
_ => Err(anyhow!( _ => Err(anyhow!(
"Unrecognized Gemfeed mime type [meta={}]", "Unrecognized Gemfeed mime type [meta={}]",
@ -153,16 +148,15 @@ impl Gemfeed {
} }
} }
fn load_from_gemtext(url: &Url, resp: GeminiResponse) -> Result<Gemfeed> { fn load_from_gemfeed(url: &Url, resp: GeminiResponse) -> Result<Gemfeed> {
let maybe_feed = resp let maybe_feed = resp
.content() .content()
.to_owned() .to_owned()
.map(|text| GemtextAst::from_value(&text)); .map(|text| GemtextAst::from_value(&text));
if let Some(ref feed) = maybe_feed { match maybe_feed {
Self::load_from_ast(url, feed) Some(ref feed) => Self::load_from_ast(url, feed),
} else { _ => Err(anyhow!("Not a valid Gemfeed - could not parse gemtext")),
Err(anyhow!("Not a valid Gemfeed - could not parse gemtext"))
} }
} }
@ -225,10 +219,10 @@ pub struct GemfeedEntry {
#[allow(dead_code)] #[allow(dead_code)]
impl GemfeedEntry { impl GemfeedEntry {
pub fn from_ast(base_url: &Url, node: &GemtextNode) -> Result<GemfeedEntry> { pub fn from_gemtext(base_url: &Url, node: &GemtextNode) -> Result<GemfeedEntry> {
let link = GemfeedLink::try_from(node)?; let link = GemfeedLink::try_from(node)?;
// Gemfeeds have only the date--according to spec, it should be 12pm UTC. // Gemfeeds have only the date--according to spec, it should
println!("{:?}", link.published); // be 12pm UTC.
let publish_date = link let publish_date = link
.published .published
.map(|date| NaiveDate::parse_from_str(&date, "%Y-%m-%d")) .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> { 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() { if let Err(error) = self.body() {
return Err(error); return Err(error);
} }
// Which means that this Should Be Safe™ // ... which means that this Should Be Safe™.
Ok(self Ok(self
.body .body
.get_mut() .get_mut()
@ -330,10 +324,9 @@ impl TryFrom<&GemtextNode> for GemfeedLink {
to: path, to: path,
} = node.to_owned() } = node.to_owned()
{ {
let re = Regex::new(r#"(\d\d\d\d-\d\d-\d\d)"#).unwrap();
let path_buf = PathBuf::from(&path); let path_buf = PathBuf::from(&path);
let published: Option<String> = re let published: Option<String> = GEMFEED_POST_REGEX
.captures_at(&title, 0) .captures_at(&title, 0)
.map(|caps| caps.get(0)) .map(|caps| caps.get(0))
.and_then(|date| date.map(|published| published.as_str().to_owned())); .and_then(|date| date.map(|published| published.as_str().to_owned()));

View File

@ -49,7 +49,7 @@ impl WriteFreely {
} }
/// Logs the client out and renders this instance of the wrapper /// Logs the client out and renders this instance of the wrapper
/// unusable.n /// unusable.
pub async fn logout(mut self) -> Result<()> { pub async fn logout(mut self) -> Result<()> {
self.client.logout().await?; self.client.logout().await?;
Ok(()) Ok(())
@ -91,10 +91,11 @@ impl TryFrom<&GemfeedEntry> for PostCreateRequest {
.title(entry.title()) .title(entry.title())
.body(entry.body_as_markdown()?); .body(entry.body_as_markdown()?);
if let Some(publish_date) = published { let req = match published {
Ok(req.created(publish_date)) Some(publish_date) => req.created(publish_date),
} else { _ => req,
Ok(req) };
}
Ok(req)
} }
} }

View File

@ -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<Vec<String>> {
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)
}