update index file separately

This commit is contained in:
yggverse 2025-02-11 23:59:53 +02:00
parent 4d4c78a4fc
commit 925ae08bf7

View file

@ -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"),
}