From aebc850de12dd204dd22dd97f08a1a1350eb8c1e Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 12 Feb 2025 01:31:25 +0200 Subject: [PATCH] skip not relevant records from `index.gmi` --- src/main.rs | 31 +++++++++++++++++++------------ src/path.rs | 30 +++++++++++++----------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 00566c0..7564c20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,22 +50,29 @@ fn crawl( Some(pub_date) => { // update `index.gmi` on channel `pub_date` change { - let remote = chrono::DateTime::parse_from_rfc2822(pub_date)?; - if status.is_none() || status.is_some_and(|local| local != remote) { - // update global state (to skip `index.gmi` overwrites without changes) - *status = Some(remote); + let remote_time = chrono::DateTime::parse_from_rfc2822(pub_date)?; + + if status.is_none() || status.is_some_and(|local_time| local_time != remote_time) { + // update global state to skip `index.gmi` overwrites without changes + *status = Some(remote_time); + + let index_path = Path::build(target, pub_date, true)?; + // build `index.gmi` members - let (mut file, mut data) = ( - File::create(Path::build(target, pub_date, true)?.index())?, - Vec::new(), - ); + let (mut file, mut data) = (File::create(index_path.index())?, Vec::new()); + // collect `index.gmi` data for item in channel.items().iter() { match item.pub_date() { Some(pub_date) => { - let path = Path::build(target, pub_date, true)?; + let item_path = Path::build(target, pub_date, true)?; - data.push(format!("=> {} {pub_date}", path.filename())); + // skip not relevant records from `index.gmi` + if item_path.path != index_path.path { + continue; + } + + data.push(format!("=> {} {pub_date}", item_path.item)); if let Some(description) = item.description() { data.push(description.to_string()); @@ -96,7 +103,7 @@ fn crawl( let path = match item.pub_date() { Some(pub_date) => { let path = Path::build(target, pub_date, true)?; - if metadata(path.filepath()).is_ok() { + if metadata(path.item()).is_ok() { exist += 1; continue; // skip existing records } @@ -142,7 +149,7 @@ fn crawl( } // record new item file - File::create(path.filepath())?.write_all(data.join("\n\n").as_bytes())?; + File::create(path.item())?.write_all(data.join("\n\n").as_bytes())?; } } None => output.warning("channel skipped as `pub_date` required by application"), diff --git a/src/path.rs b/src/path.rs index a884e39..e69c892 100644 --- a/src/path.rs +++ b/src/path.rs @@ -2,8 +2,8 @@ use std::error::Error; use std::path::MAIN_SEPARATOR; pub struct Path { - file: String, - path: String, + pub item: String, + pub path: String, } impl Path { @@ -12,27 +12,27 @@ impl Path { pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result> { use chrono::{DateTime, Datelike, Timelike}; - let date_time = DateTime::parse_from_rfc2822(pub_date)?; + let time = DateTime::parse_from_rfc2822(pub_date)?; - let file = format!( + let item = format!( "{:02}-{:02}-{:02}.gmi", - date_time.hour(), - date_time.minute(), - date_time.second() + time.hour(), + time.minute(), + time.second() ); let path = format!( "{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}", - date_time.year(), - date_time.month(), - date_time.day() + time.year(), + time.month(), + time.day() ); if mkdir { std::fs::create_dir_all(&path)?; } - Ok(Path { file, path }) + Ok(Path { item, path }) } // Getters @@ -41,11 +41,7 @@ impl Path { format!("{}{MAIN_SEPARATOR}index.gmi", self.path) } - pub fn filepath(&self) -> String { - format!("{}{MAIN_SEPARATOR}{}", self.path, self.file) - } - - pub fn filename(&self) -> &str { - &self.file + pub fn item(&self) -> String { + format!("{}{MAIN_SEPARATOR}{}", self.path, self.item) } }