From b9bc57c1e09547dc0ce54e65bdcc72605ea71ac7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 9 Nov 2024 16:58:52 +0100 Subject: [PATCH] split logic of contact --- src/routes/contact.rs | 52 +++---------------------------------- src/utils/routes/contact.rs | 50 +++++++++++++++++++++++++++++++++++ src/utils/routes/mod.rs | 1 + 3 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 src/utils/routes/contact.rs diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 263108c..5480afe 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -2,15 +2,15 @@ use actix_web::{get, routes, web, HttpRequest, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; -use std::fs::read_to_string; use crate::{ config::Config, + template::{InfosPage, NavBar}, utils::{ markdown::{File, TypeFileMetadata}, misc::{make_kw, read_file, Html}, + routes::contact::{find_links, remove_paragraphs}, }, - template::{InfosPage, NavBar}, }; const CONTACT_DIR: &str = "contacts"; @@ -32,47 +32,6 @@ async fn page(config: web::Data) -> impl Responder { Html(build_page(config.get_ref().to_owned())) } -/// Contact node -#[derive(Clone, Debug)] -struct ContactLink { - service: String, - scope: Option, - link: String, -} - -#[once(time = 60)] -fn find_links(directory: String) -> Vec { - // TOML filename - let toml_file = "links.toml"; - - // Read the TOML file and parse it - let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default(); - - let mut redirections = vec![]; - match toml::de::from_str::(&toml_str) { - Ok(data) => { - if let Some(section) = data.as_table() { - section.iter().for_each(|(key, value)| { - // Scopes are delimited with `/` - let (service, scope) = match key.split_once('/') { - Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())), - None => (key.to_owned(), None), - }; - - redirections.push(ContactLink { - service, - scope, - link: value.as_str().unwrap().to_owned(), - }); - }); - } - } - Err(_) => return vec![], - } - - redirections -} - #[routes] #[get("/{service}")] #[get("/{service}/{scope}")] @@ -92,7 +51,7 @@ async fn service_redirection(config: web::Data, req: HttpRequest) -> imp _ => false, }) // Returns the link - .map(|data| data.link.clone()) + .map(|data| data.url.clone()) .collect::>(); // This shouldn't be more than one link here @@ -119,11 +78,6 @@ struct NetworksTemplate { others: Vec, } -fn remove_paragraphs(list: &mut [File]) { - list.iter_mut() - .for_each(|file| file.content = file.content.replace("

", "").replace("

", "")); -} - #[once(time = 60)] fn build_page(config: Config) -> String { let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR); diff --git a/src/utils/routes/contact.rs b/src/utils/routes/contact.rs new file mode 100644 index 0000000..e0d25d2 --- /dev/null +++ b/src/utils/routes/contact.rs @@ -0,0 +1,50 @@ +use cached::proc_macro::once; +use std::fs::read_to_string; + +use crate::utils::markdown::File; + +/// Contact node +#[derive(Clone, Debug)] +pub struct Link { + pub service: String, + pub scope: Option, + pub url: String, +} + +#[once(time = 60)] +pub fn find_links(directory: String) -> Vec { + // TOML filename + let toml_file = "links.toml"; + + // Read the TOML file and parse it + let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default(); + + let mut redirections = vec![]; + match toml::de::from_str::(&toml_str) { + Ok(data) => { + if let Some(section) = data.as_table() { + section.iter().for_each(|(key, value)| { + // Scopes are delimited with `/` + let (service, scope) = match key.split_once('/') { + Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())), + None => (key.to_owned(), None), + }; + + redirections.push(Link { + service, + scope, + url: value.as_str().unwrap().to_owned(), + }); + }); + } + } + Err(_) => return vec![], + } + + redirections +} + +pub fn remove_paragraphs(list: &mut [File]) { + list.iter_mut() + .for_each(|file| file.content = file.content.replace("

", "").replace("

", "")); +} diff --git a/src/utils/routes/mod.rs b/src/utils/routes/mod.rs index 9cb7ea3..2b2e894 100644 --- a/src/utils/routes/mod.rs +++ b/src/utils/routes/mod.rs @@ -1,2 +1,3 @@ pub mod blog; +pub mod contact; pub mod cours;