diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2022-08-23 17:10:09 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2022-08-23 17:10:09 +0200 |
| commit | 5d6943eca8f60e5a7b1422f9c9be2ca93b52104f (patch) | |
| tree | 2ce7b3ef1f93bdc2feb948795a2780e6401ddd73 /src | |
| parent | 58f0228b4e769b39c330b0ed44deaa0fdaa669cf (diff) | |
Add Works trait & implement it for Stories
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 12 | ||||
| -rw-r--r-- | src/stories.rs | 0 | ||||
| -rw-r--r-- | src/works/mod.rs | 12 | ||||
| -rw-r--r-- | src/works/stories.rs | 21 |
4 files changed, 45 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 81c2c61..b3e907d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ use tera::{Context, Tera}; +use self::works::Work; + +mod works; + fn main() { let tera = match Tera::new("templates/**/*.html") { Ok(t) => t, @@ -17,4 +21,12 @@ fn main() { std::fs::write("docs/index.html", output) .expect("Failed to write rendered output to index.html"); + + let mut file = match std::fs::File::open(std::path::PathBuf::from("stories.json")) { + Ok(file) => file, + Err(err) => panic!("Trying to open stories.json in readmode - {:?}", err), + }; + + let stories = works::stories::Stories::new_from_file(file); + println!("Stories: {:?}", stories); } diff --git a/src/stories.rs b/src/stories.rs deleted file mode 100644 index e69de29..0000000 --- a/src/stories.rs +++ /dev/null diff --git a/src/works/mod.rs b/src/works/mod.rs new file mode 100644 index 0000000..ab7bcf5 --- /dev/null +++ b/src/works/mod.rs @@ -0,0 +1,12 @@ +use serde::Deserialize; + +pub mod stories; + +pub trait Works: for<'a> Deserialize<'a> { + fn new_from_file(file: std::fs::File) -> Self { + match serde_json::from_reader(std::io::BufReader::new(file)) { + Ok(work) => work, + Err(err) => panic!("Trying to create work from JSON File - {:?}", err), + } + } +} diff --git a/src/works/stories.rs b/src/works/stories.rs new file mode 100644 index 0000000..b437fb4 --- /dev/null +++ b/src/works/stories.rs @@ -0,0 +1,21 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct Stories(pub Vec<Story>); + +#[derive(Debug, Deserialize)] +pub struct Story { + pub title: String, + pub description: String, + pub path_to_document: String, + pub slug: String, + pub language: Language, +} + +impl super::Works for Stories {} + +#[derive(Deserialize, Debug)] +pub enum Language { + German, + English, +} |
