diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2023-06-03 14:33:09 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2023-06-03 14:33:09 +0200 |
| commit | 30b707d610f4daf025da01b12247bd3b7ebd9e55 (patch) | |
| tree | 2c82ac3ba39af5fde041f074e51b4fb9f56c092f /src | |
| parent | 683ca3627f38461cbbdee321a15821b46e72ae6a (diff) | |
Clean command pattern for works
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/works/mod.rs | 8 | ||||
| -rw-r--r-- | src/works/stories.rs | 6 |
3 files changed, 16 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index 4dc6e5e..a531785 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use anyhow::{Context, Ok, Result}; use tera::Tera; -use crate::works::Works; +use self::works::stories::Stories; +use self::works::Works; mod works; @@ -17,15 +18,14 @@ fn main() -> Result<()> { std::fs::write("docs/index.html", output).context("Failed to write index.html")?; // render stories.html file - let stories = std::rc::Rc::new(works::stories::Stories::new_from_file( + Stories::new_from_file( &std::fs::File::open(std::path::PathBuf::from("stories.json")) .context("Failed to open stories.json")?, - )?); - std::rc::Rc::clone(&stories) - .render_overview_page(&tera, "stories") - .context("Failed to render overview page for stories")?; - std::rc::Rc::clone(&stories) - .render_single_pages(&tera, "story") - .context("Failed to render single pages for 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 77dd35d..0970cb1 100644 --- a/src/works/mod.rs +++ b/src/works/mod.rs @@ -22,15 +22,15 @@ pub trait Works: for<'a> Deserialize<'a> + Serialize { } // render page for every work - fn render_single_pages(&self, tera_instance: &tera::Tera, template_name: &str) -> Result<()>; + fn render_single_pages(self, tera_instance: &tera::Tera, template_name: &str) -> Result<Self>; // render corresponding overview HTML template // with its tera context fn render_overview_page( - &self, + self, tera_instance: &tera::Tera, filename_without_extension: &str, - ) -> Result<()> { + ) -> Result<Self> { std::fs::write( format!("docs/{}.html", filename_without_extension).as_str(), tera_instance @@ -51,6 +51,6 @@ pub trait Works: for<'a> Deserialize<'a> + Serialize { filename_without_extension ) })?; - Ok(()) + Ok(self) } } diff --git a/src/works/stories.rs b/src/works/stories.rs index f86d24a..2623db6 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -24,10 +24,10 @@ struct Story { impl super::Works for Stories { // render single pages for every story with the extracted text and a download button fn render_single_pages( - &self, + self, tera_instance: &tera::Tera, template_name: &str, - ) -> Result<()> { + ) -> Result<Self> { let mut result = Vec::new(); for story in self.0.iter() { if story.update_available().with_context(|| format!("Failed to check for update for {}", story.title))? { @@ -40,7 +40,7 @@ impl super::Works for Stories { )); } } - Ok(()) + Ok(self) } // create stories from stories.json and add last modification date |
