From f08b050c240f10b29d5c8af5e80f042c115cd49b Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Tue, 23 Aug 2022 18:18:10 +0200 Subject: Add function to create tera context for stories --- src/works/mod.rs | 2 ++ src/works/stories.rs | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'src/works') diff --git a/src/works/mod.rs b/src/works/mod.rs index ab7bcf5..f399fdd 100644 --- a/src/works/mod.rs +++ b/src/works/mod.rs @@ -9,4 +9,6 @@ pub trait Works: for<'a> Deserialize<'a> { Err(err) => panic!("Trying to create work from JSON File - {:?}", err), } } + + fn create_tera_context(&self) -> Vec; } diff --git a/src/works/stories.rs b/src/works/stories.rs index b437fb4..5d96ed8 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -1,10 +1,11 @@ -use serde::Deserialize; +use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize)] pub struct Stories(pub Vec); -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct Story { + pub id: u8, pub title: String, pub description: String, pub path_to_document: String, @@ -12,9 +13,19 @@ pub struct Story { pub language: Language, } -impl super::Works for Stories {} +impl super::Works for Stories { + fn create_tera_context(&self) -> Vec { + let mut context = Vec::new(); + for story in self.0.iter() { + context.push( + tera::Context::from_serialize(&story).expect("Trying to serialize to tera context"), + ); + } + context + } +} -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Serialize, Debug)] pub enum Language { German, English, -- cgit v1.2.3