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/mod.rs | |
| parent | 30b707d610f4daf025da01b12247bd3b7ebd9e55 (diff) | |
Refactor file operations to method that creates work
Diffstat (limited to 'src/works/mod.rs')
| -rw-r--r-- | src/works/mod.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/works/mod.rs b/src/works/mod.rs index 0970cb1..a588ae8 100644 --- a/src/works/mod.rs +++ b/src/works/mod.rs @@ -1,3 +1,6 @@ +use std::fs::File; +use std::path::PathBuf; + use anyhow::{Context, Ok, Result}; use serde::{Deserialize, Serialize}; @@ -7,10 +10,13 @@ pub mod stories; pub trait Works: for<'a> Deserialize<'a> + Serialize { // create works from the corresponding JSON file, // e.g. stories from stories.json - fn new_from_file(file: &std::fs::File) -> Result<Self> { - let file = serde_json::from_reader(std::io::BufReader::new(file)) - .with_context(|| format!("Failed to create works from {:#?}", &file))?; - Ok(file) + fn new_from_file(workname: &str) -> Result<Self> { + let work = 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))?; + Ok(work) } // crate tera context from the group of work |
