From 925ae08bf740c9a884e1af22221dc477199e4cd3 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 11 Feb 2025 23:59:53 +0200 Subject: [PATCH] update index file separately --- src/main.rs | 55 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index f882470..00566c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,19 +48,43 @@ fn crawl( match channel.pub_date() { Some(pub_date) => { - // detect index file update required - let mut index_request = { + // update `index.gmi` on channel `pub_date` change + { let remote = chrono::DateTime::parse_from_rfc2822(pub_date)?; - if status.is_some_and(|local| local != remote) || status.is_none() { + if status.is_none() || status.is_some_and(|local| local != remote) { + // update global state (to skip `index.gmi` overwrites without changes) *status = Some(remote); - Some(( + // build `index.gmi` members + let (mut file, mut data) = ( File::create(Path::build(target, pub_date, true)?.index())?, Vec::new(), - )) - } else { - None + ); + // 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)?; + + data.push(format!("=> {} {pub_date}", path.filename())); + + if let Some(description) = item.description() { + data.push(description.to_string()); + } + + if let Some(content) = item.content() { + data.push(content.to_string()); + } + } + None => { + output.warning("item skipped as `pub_date` required by application") + } + } + } + // update `index.gmi` file with new version + file.write_all(data.join("\n\n").as_bytes())?; + output.debug("index file updated"); } - }; + } // handle feed items for item in channel.items().iter() { @@ -76,9 +100,6 @@ fn crawl( exist += 1; continue; // skip existing records } - if let Some((_, ref mut index)) = index_request { - index.push(format!("=> {} {pub_date}", path.filename())); - } data.push(format!("# {pub_date}")); path } @@ -89,16 +110,10 @@ fn crawl( }; if let Some(description) = item.description() { - if let Some((_, ref mut index)) = index_request { - index.push(description.to_string()); - } data.push(description.to_string()); } if let Some(content) = item.content() { - if let Some((_, ref mut index)) = index_request { - index.push(content.to_string()); - } data.push(content.to_string()); } @@ -129,12 +144,6 @@ fn crawl( // record new item file File::create(path.filepath())?.write_all(data.join("\n\n").as_bytes())?; } - - // update index file - if let Some((mut file, index)) = index_request { - file.write_all(index.join("\n\n").as_bytes())?; - output.debug("index file updated"); - } } None => output.warning("channel skipped as `pub_date` required by application"), }