diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2023-04-10 13:21:07 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2023-04-10 13:21:07 +0200 |
| commit | 655cd12e1d6dc0b68695e888ab1919e0387ee0ce (patch) | |
| tree | 7579711067a73d879ac6cb2d9dd8c204f97b004a /src/works | |
| parent | 02b7e77795480f87ad89f56210348e87a0f749ea (diff) | |
Add last modified date automation
Diffstat (limited to 'src/works')
| -rw-r--r-- | src/works/stories.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/works/stories.rs b/src/works/stories.rs index 9db3ec3..c2b4b8f 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -15,6 +15,7 @@ struct Story { path_to_document: String, number_of_pages: u16, language: Language, + #[serde(default)] last_update: NaiveDate, } @@ -40,6 +41,16 @@ impl super::Works for Stories { } anyhow::Ok(()).with_context(|| "Failed to render single story pages") } + + // create stories from stories.json and add last modification date + fn new_from_file(file: std::fs::File) -> anyhow::Result<Self> { + let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(&file))?; + for story in stories.0.iter_mut() { + story.get_last_modified()?; + } + // add error context + Ok(stories) + } } impl Story { @@ -87,6 +98,16 @@ impl Story { let text = std::fs::read_to_string(self.get_content_html_path())?; anyhow::Ok(text).with_context(|| format!("Failed to extract text for {}", self.title)) } + + // get date of last modification of pdf file + fn get_last_modified(&mut self) -> anyhow::Result<chrono::NaiveDate> { + use std::os::linux::fs::MetadataExt; + let pdf_last_modification = std::fs::metadata(self.get_pdf_path())?.st_mtime(); + let html_last_modification = std::fs::metadata(self.get_html_path())?.st_mtime(); + let date = chrono::NaiveDateTime::from_timestamp(std::cmp::max(pdf_last_modification, html_last_modification), 0).date(); + self.last_update = date; + Ok(date) + } } // language of story content |
