From 04515080eb74d7866ca3c8286f0dbab8de4fd75c Mon Sep 17 00:00:00 2001 From: Mar Date: Fri, 2 Aug 2024 16:32:47 +0200 Subject: [PATCH] For readability --- source/Main/publications.rs | 70 ++++++++++++++++++------------------- source/Main/renders.rs | 4 +-- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/source/Main/publications.rs b/source/Main/publications.rs index fb7f7cb..caa6fbd 100644 --- a/source/Main/publications.rs +++ b/source/Main/publications.rs @@ -7,7 +7,7 @@ use std::path::Path; use std::{fs, process}; -use jsonc_parser::parse_to_serde_value; +use jsonc_parser::{parse_to_serde_value as preparse_jsonc}; use log::{error, warn}; use serde::{Deserialize, Serialize}; @@ -19,42 +19,9 @@ pub(crate) trait CynthiaPublicationListTrait { fn get_root(&self) -> Option; fn get_by_id(&self, id: String) -> Option; fn validate(&self, config: CynthiaConfClone) -> bool; - fn new() -> CynthiaPublicationList; + fn load() -> CynthiaPublicationList; } impl CynthiaPublicationListTrait for CynthiaPublicationList { - fn new() -> CynthiaPublicationList { - if Path::new("./cynthiaFiles/published.yaml").exists() { - let file = "./cynthiaFiles/published.yaml".to_owned(); - let unparsed_yaml = fs::read_to_string(file).expect("Couldn't find or load that file."); - serde_yaml::from_str(&unparsed_yaml).unwrap_or_else(|_e| { - error!("Published.yaml contains invalid Cynthia-instructions.",); - Vec::new() - }) - } else { - let file = "./cynthiaFiles/published.jsonc".to_owned(); - let unparsed_json = match fs::read_to_string(file) { - Ok(t) => t, - Err(e) => { - error!("Couldn't find or load published.jsonc.\n\n\t\t{e}"); - process::exit(1); - } - }; - // println!("{}", unparsed_json); - let parsed_json: Option = - match parse_to_serde_value(unparsed_json.as_str(), &Default::default()) { - Ok(t) => t, - Err(e) => { - error!("Couldn't parse published.jsonc.\n\n\t\t{e}"); - process::exit(1); - } - }; - serde_json::from_value(parsed_json.into()).unwrap_or_else(|e| { - let k = e.line(); - error!("Published.json contains invalid Cynthia-instructions.\n\n\t\t{e}, {k}",); - Vec::new() - }) - } - } fn get_notfound(&self, config: CynthiaConfClone) -> Option { self.iter() .find(|x| { @@ -144,6 +111,39 @@ impl CynthiaPublicationListTrait for CynthiaPublicationList { // Return true if all checks passed return valid.iter().all(|x| *x); } + fn load() -> CynthiaPublicationList { + if Path::new("./cynthiaFiles/published.yaml").exists() { + let file = "./cynthiaFiles/published.yaml".to_owned(); + let unparsed_yaml = fs::read_to_string(file).expect("Couldn't find or load that file."); + serde_yaml::from_str(&unparsed_yaml).unwrap_or_else(|_e| { + error!("Published.yaml contains invalid Cynthia-instructions.",); + Vec::new() + }) + } else { + let file = "./cynthiaFiles/published.jsonc".to_owned(); + let unparsed_json = match fs::read_to_string(file) { + Ok(t) => t, + Err(e) => { + error!("Couldn't find or load published.jsonc.\n\n\t\t{e}"); + process::exit(1); + } + }; + // println!("{}", unparsed_json); + let preparsed: Option = + match preparse_jsonc(unparsed_json.as_str(), &Default::default()) { + Ok(t) => t, + Err(e) => { + error!("Couldn't parse published.jsonc.\n\n\t\t{e}"); + process::exit(1); + } + }; + serde_json::from_value(preparsed.into()).unwrap_or_else(|e| { + let k = e.line(); + error!("Published.json contains invalid Cynthia-instructions.\n\n\t\t{e}, {k}",); + Vec::new() + }) + } + } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] diff --git a/source/Main/renders.rs b/source/Main/renders.rs index 5930e32..55597db 100644 --- a/source/Main/renders.rs +++ b/source/Main/renders.rs @@ -65,7 +65,7 @@ pub(crate) fn check_pgid( } else { pgid }; - let published = CynthiaPublicationList::new(); + let published = CynthiaPublicationList::load(); if !published.validate(server_context.config.clone()) { error!("Incorrect publications found in publications.jsonc."); @@ -87,7 +87,7 @@ pub(crate) fn check_pgid( } } pub(crate) async fn render_from_pgid(pgid: String, config: CynthiaConfClone) -> RenderrerResponse { - let published = CynthiaPublicationList::new(); + let published = CynthiaPublicationList::load(); let publication = if pgid == *"" { published.get_root() } else {