Skip to content

Commit

Permalink
For readability
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Aug 2, 2024
1 parent 8be4212 commit 0451508
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
70 changes: 35 additions & 35 deletions source/Main/publications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -19,42 +19,9 @@ pub(crate) trait CynthiaPublicationListTrait {
fn get_root(&self) -> Option<CynthiaPublication>;
fn get_by_id(&self, id: String) -> Option<CynthiaPublication>;
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<serde_json::Value> =
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<CynthiaPublication> {
self.iter()
.find(|x| {
Expand Down Expand Up @@ -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<serde_json::Value> =
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)]
Expand Down
4 changes: 2 additions & 2 deletions source/Main/renders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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 {
Expand Down

0 comments on commit 0451508

Please sign in to comment.