diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2022-09-03 18:45:45 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2022-09-03 18:45:45 +0200 |
| commit | 18853a2baf20017e926e4bb465e3c02bb5a7af97 (patch) | |
| tree | ee9051cd54073878a407c93005405c116db57771 /src | |
| parent | 746c348f739a8d5f26d4cd76ed72646a338271c4 (diff) | |
Universalize method to create context from works and pass entire works object
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/works/mod.rs | 10 | ||||
| -rw-r--r-- | src/works/stories.rs | 12 |
3 files changed, 16 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index 3985b06..7f658ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,4 +32,11 @@ fn main() { let context = stories.create_tera_context(); println!("Context: {:?}\n", context); + + let stories_html_output = tera + .render("stories.html", &context) + .expect("Failed to render stories.html"); + + std::fs::write("docs/stories.html", stories_html_output) + .expect("Failed to write rendered output to stories.html"); } diff --git a/src/works/mod.rs b/src/works/mod.rs index 3dfd4a2..f062565 100644 --- a/src/works/mod.rs +++ b/src/works/mod.rs @@ -1,8 +1,8 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; pub mod stories; -pub trait Works: for<'a> Deserialize<'a> { +pub trait Works: for<'a> Deserialize<'a> + Serialize { fn new_from_file(file: std::fs::File) -> Self { match serde_json::from_reader(std::io::BufReader::new(file)) { Ok(work) => work, @@ -10,5 +10,9 @@ pub trait Works: for<'a> Deserialize<'a> { } } - fn create_tera_context(&self) -> tera::Context; + fn create_tera_context(&self) -> tera::Context { + let mut context = tera::Context::new(); + context.insert("works", &self); + context + } } diff --git a/src/works/stories.rs b/src/works/stories.rs index 450f3bc..73d89ad 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct Stories(pub Vec<Story>); #[derive(Debug, Deserialize, Serialize)] @@ -13,15 +13,7 @@ pub struct Story { pub language: Language, } -impl super::Works for Stories { - fn create_tera_context(&self) -> tera::Context { - let mut context = tera::Context::new(); - for story in self.0.iter() { - context.insert(format!("story-{}", story.id), &story); - } - context - } -} +impl super::Works for Stories {} #[derive(Deserialize, Serialize, Debug)] pub enum Language { |
