diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2023-06-03 14:52:30 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2023-06-03 14:52:30 +0200 |
| commit | 3d1e4859f6a09208966407cefaae5ba6803ddc26 (patch) | |
| tree | 0764b115d5b5a06448a64b91cfc59e3c95202f4f /src/works/stories.rs | |
| parent | 30b707d610f4daf025da01b12247bd3b7ebd9e55 (diff) | |
Refactor file operations to method that creates work
Diffstat (limited to 'src/works/stories.rs')
| -rw-r--r-- | src/works/stories.rs | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/works/stories.rs b/src/works/stories.rs index 2623db6..1c96e2a 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::path::PathBuf; use anyhow::{Result, Ok, Context}; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; @@ -44,8 +46,13 @@ impl super::Works for Stories { } // create stories from stories.json and add last modification date - fn new_from_file(file: &std::fs::File) -> Result<Self> { - let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(file)).with_context(|| format!("Failed to read JSON from {:#?}", file))?; + fn new_from_file(workname: &str) -> Result<Self> { + let mut stories: Self = serde_json::from_reader(std::io::BufReader::new( + &File::open(PathBuf::from(workname)) + .with_context(|| format!("Failed to open {}.json", workname))?, + )) + .with_context(|| format!("Failed to create works from {}.json", workname))?; + for story in stories.0.iter_mut() { story.get_last_modified().with_context(|| format!("Failed to get last modified date for {}", story.title))?; story.get_pages_num().with_context(|| format!("Failed to get number of pages from {}'s PDF", story.title))?; |
