use serde::{Deserialize, Serialize}; use crate::person::{Person, PersonID, Sex}; use askama::Template; #[derive(Clone, Debug, Serialize, Deserialize, Template)] #[template(path = "stammbaum.html")] pub struct Stammbaum { members: Vec, } #[derive(Template)] #[template(path = "stammbaum_full.html")] pub struct StammbaumFull { members: Vec, } impl From for StammbaumFull { fn from(value: Stammbaum) -> Self { Self { members: value.members, } } } #[allow(dead_code)] impl Stammbaum { pub fn new(members: Vec) -> Self { Self { members } } pub fn get(&self, id: PersonID) -> Option<&Person> { self.members.iter().find(|p| p.id() == id) } } mod test { #![allow(unused_imports, clippy::unwrap_used)] use std::io::Write; use chrono::{TimeZone, Utc}; use crate::person::{Marriage, Sex}; use super::*; #[test] fn creation() { let mut mother = Person::new( "Jane", "Doe", Some("Musterfrau"), Sex::Female, Utc.with_ymd_and_hms(1990, 1, 2, 3, 4, 5).unwrap(), vec![], ); let mut father = Person::new( "John", "Doe", None, Sex::Male, Utc.with_ymd_and_hms(1991, 2, 3, 4, 5, 6).unwrap(), vec![], ); let marriage = Marriage::new( mother.id(), father.id(), Utc.with_ymd_and_hms(2020, 3, 4, 5, 6, 7).unwrap(), None, ); mother.add_marriage(marriage.clone()); father.add_marriage(marriage); let child = Person::new( "Johnny", "Doe", None, Sex::Male, Utc.with_ymd_and_hms(2021, 4, 5, 6, 7, 8).unwrap(), vec![mother.id(), father.id()], ); let stammbaum = Stammbaum::new(vec![father, mother, child]); let mut file = std::fs::File::create("test.json").unwrap(); file.write_all(serde_json::to_string_pretty(&stammbaum).unwrap().as_bytes()) .unwrap(); } }