diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2023-04-10 13:44:53 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2023-04-10 13:44:53 +0200 |
| commit | aa191707ba93ee1b0790be89dcb274864ffe884f (patch) | |
| tree | ffe853e5716fc9505a11bfe5d4f3e6e9e19e2d20 | |
| parent | 655cd12e1d6dc0b68695e888ab1919e0387ee0ce (diff) | |
Add number of pdf pages automation
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/works/stories.rs | 13 | ||||
| -rw-r--r-- | stories.json | 3 |
3 files changed, 12 insertions, 5 deletions
@@ -11,3 +11,4 @@ serde = {version = "1.0", features = ["derive"]} serde_json = "1.0" anyhow = "1.0" chrono = {version = "0.4", features = ["serde"]} +pdf = "0.8" diff --git a/src/works/stories.rs b/src/works/stories.rs index c2b4b8f..510f30b 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -13,9 +13,10 @@ struct Story { title: String, description: String, path_to_document: String, - number_of_pages: u16, language: Language, #[serde(default)] + number_of_pages: u32, + #[serde(default)] last_update: NaiveDate, } @@ -47,6 +48,7 @@ impl super::Works for Stories { let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(&file))?; for story in stories.0.iter_mut() { story.get_last_modified()?; + story.get_pages_num()?; } // add error context Ok(stories) @@ -99,7 +101,7 @@ impl Story { anyhow::Ok(text).with_context(|| format!("Failed to extract text for {}", self.title)) } - // get date of last modification of pdf file + // get date of last modification 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(); @@ -108,6 +110,13 @@ impl Story { self.last_update = date; Ok(date) } + + // get number of pages of pdf file + fn get_pages_num(&mut self) -> anyhow::Result<u32> { + let file = pdf::file::File::open(self.get_pdf_path())?; + self.number_of_pages = file.num_pages(); + Ok(file.num_pages()) + } } // language of story content diff --git a/stories.json b/stories.json index e9b10a9..fd01c0e 100644 --- a/stories.json +++ b/stories.json @@ -3,21 +3,18 @@ "title": "Mit gutem Gewissen", "description": "Der gefährliche Zauber von Leon wird von seinem wegweisenden Freund durchschaut. Kann er ihn stoppen?", "path_to_document": "mit_gutem_gewissen.pdf", - "number_of_pages": 7, "language": "German" }, { "title": "Das schweigende Spiegelbild", "description": "Das Spiegelbild verzweifelt in seiner Hilflosigkeit gegenüber seinem Ich.", "path_to_document": "das_schweigende_spiegelbild.pdf", - "number_of_pages": 2, "language": "German" }, { "title": "The fallen dragons of Bavaria", "description": "In his new life in Germany Kazuyuki is needed by dragons.", "path_to_document": "the_fallen_dragons_of_bavaria.pdf", - "number_of_pages": 3, "language": "English" } ] |
