use crate::app::AppConfig; use crate::website::page::{Page, PageTemplate}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct WebSite { root_page: Page, assets_index: Vec<String>, templates: Vec<PageTemplate>, pages_index: HashMap<PathBuf, Page>, } impl WebSite { pub fn from_json(json: &str) -> Self { let mut obj: Self = serde_json::from_str(json).unwrap(); obj.build_assets_index(); obj.build_pages_index(obj.root_page.clone(), PathBuf::from("/")); obj.root_page.build_html(); for p in obj.root_page.sub_pages.iter_mut() { p.build_html(); } obj } pub fn load(config: &AppConfig) -> WebSite { let file_path = match &config.load { None => std::env::current_dir() .unwrap() .join("templates") .join("new_website.json"), Some(pth) => pth.clone(), }; WebSite::from_json(&std::fs::read_to_string(file_path).unwrap()) } fn build_pages_index(&mut self, root_page: Page, from_url: PathBuf) { let url = from_url.join(&root_page.metadata.url_slug); self.pages_index.insert(url.clone(), root_page.clone()); for p in root_page.sub_pages { self.build_pages_index(p, url.clone()); } } fn build_assets_index(&mut self) { unimplemented!(); } pub fn get_page_by_url(&self, url: &PathBuf) -> Option<&Page> { self.pages_index.get(&PathBuf::from("/").join(url)) } } // #[cfg(test)] // mod test_website { // use super::*; // use crate::testing::TEST_JSON_WEBSITE; // #[test] // fn test_index_pages_by_slug() { // let website = WebSite::from_json(TEST_JSON_WEBSITE); // let root_page = website.get_page_by_url(&PathBuf::from("/")); // assert!(root_page.is_some()); // let root_page = root_page.unwrap(); // assert_eq!(root_page.page_data.html_body, "<h1>Test Website</h1>"); // let sub_page = website.get_page_by_url(&PathBuf::from("subpage")); // assert!(sub_page.is_some()); // let sub_page = sub_page.unwrap(); // assert_eq!(sub_page.page_data.html_body, "<h1>A sub page</h1>"); // let nested_page = website.get_page_by_url(&PathBuf::from("subpage/nested")); // assert!(nested_page.is_some()); // let nested_page = nested_page.unwrap(); // assert_eq!(nested_page.page_data.html_body, "<h1>Nested page</h1>"); // } // }