summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2023-06-03 14:52:30 +0200
committerWriterV2 <primary-v2@protonmail.com>2023-06-03 14:52:30 +0200
commit3d1e4859f6a09208966407cefaae5ba6803ddc26 (patch)
tree0764b115d5b5a06448a64b91cfc59e3c95202f4f
parent30b707d610f4daf025da01b12247bd3b7ebd9e55 (diff)
Refactor file operations to method that creates work
-rw-r--r--src/main.rs18
-rw-r--r--src/works/mod.rs14
-rw-r--r--src/works/stories.rs11
3 files changed, 26 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs
index a531785..e2aab81 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,21 +11,17 @@ fn main() -> Result<()> {
let tera = Tera::new("templates/**/*.html").context("Failed to create new Tera instance")?;
// render index.html file
- let context = tera::Context::new();
let output = tera
- .render("index.html", &context)
+ .render("index.html", &tera::Context::new())
.context("Failed to render index.html")?;
std::fs::write("docs/index.html", output).context("Failed to write index.html")?;
- // render stories.html file
- Stories::new_from_file(
- &std::fs::File::open(std::path::PathBuf::from("stories.json"))
- .context("Failed to open stories.json")?,
- )?
- .render_overview_page(&tera, "stories")
- .context("Failed to render overview page for stories")?
- .render_single_pages(&tera, "story")
- .context("Failed to render single pages for stories")?;
+ // render pages for stories
+ Stories::new_from_file("stories")?
+ .render_overview_page(&tera, "stories")
+ .context("Failed to render overview page for stories")?
+ .render_single_pages(&tera, "story")
+ .context("Failed to render single pages for stories")?;
Ok(())
}
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
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))?;