From fa4d0ba7e85421352f816e2aeef25ce0f89dbf6c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 10 Nov 2024 10:58:08 +0100 Subject: [PATCH] rename structs --- src/routes/blog.rs | 4 ++-- src/routes/contact.rs | 13 ++++------ src/routes/cours.rs | 4 ++-- src/routes/index.rs | 6 ++--- src/routes/portfolio.rs | 8 +++---- src/utils/markdown.rs | 18 +++++--------- src/utils/metadata.rs | 52 ++++++++++++++++++++-------------------- src/utils/misc.rs | 6 ++--- src/utils/routes/blog.rs | 14 ++++------- 9 files changed, 55 insertions(+), 70 deletions(-) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 6075bdd..5e8d3e9 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -7,7 +7,7 @@ use crate::{ template::{InfosPage, NavBar}, utils::{ markdown::File, - metadata::TypeFileMetadata, + metadata::MType, misc::{make_kw, read_file, Html}, routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR}, }, @@ -32,7 +32,7 @@ fn build_index(config: Config) -> String { let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); // Get about - let about: Option = read_file(format!("{blog_dir}/about.md"), TypeFileMetadata::Generic); + let about: Option = read_file(format!("{blog_dir}/about.md"), MType::Generic); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 53bd8a0..48490d1 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -8,7 +8,7 @@ use crate::{ template::{InfosPage, NavBar}, utils::{ markdown::File, - metadata::TypeFileMetadata, + metadata::MType, misc::{make_kw, read_file, Html}, routes::contact::{find_links, remove_paragraphs}, }, @@ -85,10 +85,7 @@ fn build_page(config: Config) -> String { let ext = ".md"; // Get about - let about = read_file( - format!("{contacts_dir}/about.md"), - TypeFileMetadata::Generic, - ); + let about = read_file(format!("{contacts_dir}/about.md"), MType::Generic); let socials_dir = "socials"; let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}")) @@ -96,7 +93,7 @@ fn build_page(config: Config) -> String { .map(|e| { read_file( e.unwrap().to_string_lossy().to_string(), - TypeFileMetadata::Contact, + MType::Contact, ) .unwrap() }) @@ -108,7 +105,7 @@ fn build_page(config: Config) -> String { .map(|e| { read_file( e.unwrap().to_string_lossy().to_string(), - TypeFileMetadata::Contact, + MType::Contact, ) .unwrap() }) @@ -120,7 +117,7 @@ fn build_page(config: Config) -> String { .map(|e| { read_file( e.unwrap().to_string_lossy().to_string(), - TypeFileMetadata::Contact, + MType::Contact, ) .unwrap() }) diff --git a/src/routes/cours.rs b/src/routes/cours.rs index 2b00df0..f4a934e 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -9,7 +9,7 @@ use crate::{ template::{InfosPage, NavBar}, utils::{ markdown::File, - metadata::TypeFileMetadata, + metadata::MType, misc::{make_kw, read_file, Html}, routes::cours::{excluded, get_filetree}, }, @@ -54,7 +54,7 @@ fn get_content( return None; } - read_file(format!("{cours_dir}/{filename}"), TypeFileMetadata::Generic) + read_file(format!("{cours_dir}/{filename}"), MType::Generic) } fn build_page(info: &web::Query, config: Config) -> String { diff --git a/src/routes/index.rs b/src/routes/index.rs index 999bb6b..11492ed 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -7,7 +7,7 @@ use crate::{ template::{InfosPage, NavBar}, utils::{ markdown::File, - metadata::TypeFileMetadata, + metadata::MType, misc::{make_kw, read_file, Html}, }, }; @@ -38,7 +38,7 @@ struct StyleAvatar { fn build_page(config: Config) -> String { let mut file = read_file( format!("{}/index.md", config.locations.data_dir), - TypeFileMetadata::Index, + MType::Index, ); // Default values @@ -68,7 +68,7 @@ fn build_page(config: Config) -> String { } } } else { - file = read_file("README.md".to_string(), TypeFileMetadata::Generic); + file = read_file("README.md".to_string(), MType::Generic); } config.tmpl.render( diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 1c37665..6e67c05 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -8,7 +8,7 @@ use crate::{ template::{InfosPage, NavBar}, utils::{ markdown::File, - metadata::TypeFileMetadata, + metadata::MType, misc::{make_kw, read_file, Html}, }, }; @@ -38,7 +38,7 @@ fn build_page(config: Config) -> String { // Get about let about = read_file( format!("{projects_dir}/about.md"), - TypeFileMetadata::Generic, + MType::Generic, ); // Get apps @@ -47,7 +47,7 @@ fn build_page(config: Config) -> String { .map(|e| { read_file( e.unwrap().to_string_lossy().to_string(), - TypeFileMetadata::Portfolio, + MType::Portfolio, ) .unwrap() }) @@ -65,7 +65,7 @@ fn build_page(config: Config) -> String { .map(|e| { read_file( e.unwrap().to_string_lossy().to_string(), - TypeFileMetadata::Portfolio, + MType::Portfolio, ) .unwrap() }) diff --git a/src/utils/markdown.rs b/src/utils/markdown.rs index ce4435c..fd5fb86 100644 --- a/src/utils/markdown.rs +++ b/src/utils/markdown.rs @@ -9,9 +9,9 @@ use std::fmt::Debug; use std::fs; use std::path::Path; -use crate::utils::metadata::TypeFileMetadata; +use crate::utils::metadata::MType; -use super::metadata::{get_metadata, FileMetadata, Metadata}; +use super::metadata::{get, MFile, Metadata}; /// File description #[derive(Content, Debug, Clone)] @@ -122,7 +122,7 @@ fn custom_img_size(html: &str) -> String { /// Fix local images to base64 and integration of markdown files fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { let mut metadata = Metadata { - info: FileMetadata::default(), + info: MFile::default(), math: false, mermaid: false, syntax_highlight: false, @@ -146,8 +146,7 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { if mime == "text/markdown" { let mut options = get_options(); options.extension.footnotes = false; - let data = - read_md(&img_path, &file, TypeFileMetadata::Generic, Some(options)); + let data = read_md(&img_path, &file, MType::Generic, Some(options)); el.replace(&data.content, ContentType::Html); // Store the metadata for later merging @@ -175,19 +174,14 @@ fn fix_images_and_integration(path: &str, html: &str) -> (String, Metadata) { } /// Transform markdown string to File structure -pub fn read_md( - path: &str, - raw_text: &str, - metadata_type: TypeFileMetadata, - options: Option, -) -> File { +pub fn read_md(path: &str, raw_text: &str, metadata_type: MType, options: Option) -> File { let arena = Arena::new(); let mut opt = options.map_or_else(get_options, |specific_opt| specific_opt); let root = parse_document(&arena, raw_text, &opt); // Find metadata - let metadata = get_metadata(root, metadata_type); + let metadata = get(root, metadata_type); // Update comrak render properties opt.render.hardbreaks = metadata.hardbreaks; diff --git a/src/utils/metadata.rs b/src/utils/metadata.rs index 532d3d6..a509d96 100644 --- a/src/utils/metadata.rs +++ b/src/utils/metadata.rs @@ -69,7 +69,7 @@ pub struct FileMetadataPortfolio { /// List of available metadata types #[derive(Hash, PartialEq, Eq, Clone, Copy)] -pub enum TypeFileMetadata { +pub enum MType { Blog, Contact, Generic, @@ -80,7 +80,7 @@ pub enum TypeFileMetadata { /// Structure who holds all the metadata the file have /// Usually all fields are None except one #[derive(Content, Debug, Default, Deserialize, Clone)] -pub struct FileMetadata { +pub struct MFile { pub hardbreaks: bool, pub blog: Option, pub contact: Option, @@ -92,7 +92,7 @@ pub struct FileMetadata { /// Global metadata #[derive(Content, Debug, Clone)] pub struct Metadata { - pub info: FileMetadata, + pub info: MFile, pub math: bool, pub mermaid: bool, pub syntax_highlight: bool, @@ -114,44 +114,44 @@ fn deserialize_metadata(text: &str) -> } /// Fetch metadata from AST -pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileMetadata { +pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile { root.children() .map(|node| { - let generic = FileMetadata { + let generic = MFile { hardbreaks: true, - ..FileMetadata::default() + ..MFile::default() }; match &node.data.borrow().value { // Extract metadata from frontmatter NodeValue::FrontMatter(text) => match mtype { - TypeFileMetadata::Blog => { + MType::Blog => { let metadata: FileMetadataBlog = deserialize_metadata(text); - FileMetadata { + MFile { blog: Some(metadata.clone()), hardbreaks: metadata.hardbreaks.unwrap_or_default(), - ..FileMetadata::default() + ..MFile::default() } } - TypeFileMetadata::Contact => { + MType::Contact => { let mut metadata: FileMetadataContact = deserialize_metadata(text); // Trim descriptions if let Some(desc) = &mut metadata.description { desc.clone_from(&desc.trim().into()); } - FileMetadata { + MFile { contact: Some(metadata), - ..FileMetadata::default() + ..MFile::default() } } - TypeFileMetadata::Generic => generic, - TypeFileMetadata::Index => FileMetadata { + MType::Generic => generic, + MType::Index => MFile { index: Some(deserialize_metadata(text)), - ..FileMetadata::default() + ..MFile::default() }, - TypeFileMetadata::Portfolio => FileMetadata { + MType::Portfolio => MFile { portfolio: Some(deserialize_metadata(text)), - ..FileMetadata::default() + ..MFile::default() }, }, _ => generic, @@ -160,22 +160,22 @@ pub fn get_metadata<'a>(root: &'a AstNode<'a>, mtype: TypeFileMetadata) -> FileM .next() .map_or_else( || match mtype { - TypeFileMetadata::Blog => FileMetadata { + MType::Blog => MFile { blog: Some(FileMetadataBlog::default()), - ..FileMetadata::default() + ..MFile::default() }, - TypeFileMetadata::Contact => FileMetadata { + MType::Contact => MFile { contact: Some(FileMetadataContact::default()), - ..FileMetadata::default() + ..MFile::default() }, - TypeFileMetadata::Generic => FileMetadata::default(), - TypeFileMetadata::Index => FileMetadata { + MType::Generic => MFile::default(), + MType::Index => MFile { index: Some(FileMetadataIndex::default()), - ..FileMetadata::default() + ..MFile::default() }, - TypeFileMetadata::Portfolio => FileMetadata { + MType::Portfolio => MFile { portfolio: Some(FileMetadataPortfolio::default()), - ..FileMetadata::default() + ..MFile::default() }, }, |data| data, diff --git a/src/utils/misc.rs b/src/utils/misc.rs index 28d977a..4d7aee5 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -13,7 +13,7 @@ use crate::config::FileConfiguration; use super::{ markdown::{read_md, File}, - metadata::{FileMetadata, Metadata, TypeFileMetadata}, + metadata::{MFile, MType, Metadata}, }; #[cached] @@ -57,7 +57,7 @@ impl Responder for Html { /// Read a file #[cached] -pub fn read_file(filename: String, expected_file: TypeFileMetadata) -> Option { +pub fn read_file(filename: String, expected_file: MType) -> Option { Path::new(&filename.clone()) .extension() .and_then(|ext| match ext.to_str().unwrap() { @@ -73,7 +73,7 @@ fn read_pdf(data: Vec) -> File { File { metadata: Metadata { - info: FileMetadata::default(), + info: MFile::default(), mermaid: false, syntax_highlight: false, math: false, diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 099fbaa..42c1633 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -19,7 +19,7 @@ use crate::{ utils::{ date::Date, markdown::{get_options, File}, - metadata::{get_metadata, FileMetadataBlog, TypeFileMetadata}, + metadata::{get, FileMetadataBlog, MType}, misc::{get_url, make_kw, read_file}, }, }; @@ -44,10 +44,7 @@ impl Post { let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}"); let ext = ".md"; - if let Some(file) = read_file( - format!("{blog_dir}/{}{ext}", self.url), - TypeFileMetadata::Blog, - ) { + if let Some(file) = read_file(format!("{blog_dir}/{}{ext}", self.url), MType::Blog) { self.content = Some(file.content); } } @@ -89,7 +86,7 @@ pub fn get_posts(location: &str) -> Vec { let options = get_options(); let root = parse_document(&arena, &text, &options); - let mut metadata = get_metadata(root, TypeFileMetadata::Blog).blog.unwrap(); + let mut metadata = get(root, MType::Blog).blog.unwrap(); // Always have a title metadata.title = metadata @@ -142,10 +139,7 @@ pub fn get_post( let blog_dir = format!("{data_dir}/{BLOG_DIR}/{POST_DIR}"); let ext = ".md"; - *post = read_file( - format!("{blog_dir}/{filename}{ext}"), - TypeFileMetadata::Blog, - ); + *post = read_file(format!("{blog_dir}/{filename}{ext}"), MType::Blog); let default = ( filename,