was i stupid?

This commit is contained in:
Mylloon 2024-11-17 18:34:20 +01:00
parent 4ae660983f
commit fe0d15509c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -118,17 +118,11 @@ fn deserialize_metadata<T: Default + serde::de::DeserializeOwned>(text: &str) ->
/// Fetch metadata from AST /// Fetch metadata from AST
pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile { pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
root.children() root.children()
.map(|node| { .map(|node| match &node.data.borrow().value {
let generic = MFile {
hardbreaks: true,
..MFile::default()
};
match &node.data.borrow().value {
// Extract metadata from frontmatter // Extract metadata from frontmatter
NodeValue::FrontMatter(text) => match mtype { NodeValue::FrontMatter(text) => match mtype {
MType::Blog => { MType::Blog => {
let metadata: FileMetadataBlog = deserialize_metadata(text); let metadata = deserialize_metadata::<FileMetadataBlog>(text);
MFile { MFile {
blog: Some(metadata.clone()), blog: Some(metadata.clone()),
hardbreaks: metadata.hardbreaks.unwrap_or_default(), hardbreaks: metadata.hardbreaks.unwrap_or_default(),
@ -136,7 +130,7 @@ pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
} }
} }
MType::Contact => { MType::Contact => {
let mut metadata: FileMetadataContact = deserialize_metadata(text); let mut metadata = deserialize_metadata::<FileMetadataContact>(text);
// Trim descriptions // Trim descriptions
if let Some(desc) = &mut metadata.description { if let Some(desc) = &mut metadata.description {
desc.clone_from(&desc.trim().into()); desc.clone_from(&desc.trim().into());
@ -146,7 +140,10 @@ pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
..MFile::default() ..MFile::default()
} }
} }
MType::Generic => generic, MType::Generic => MFile {
hardbreaks: deserialize_metadata(text),
..MFile::default()
},
MType::Index => MFile { MType::Index => MFile {
index: Some(deserialize_metadata(text)), index: Some(deserialize_metadata(text)),
..MFile::default() ..MFile::default()
@ -156,30 +153,11 @@ pub fn get<'a>(root: &'a AstNode<'a>, mtype: MType) -> MFile {
..MFile::default() ..MFile::default()
}, },
}, },
_ => generic, _ => MFile {
} hardbreaks: true,
..MFile::default()
},
}) })
.next() .next()
.map_or_else( .unwrap()
|| match mtype {
MType::Blog => MFile {
blog: Some(FileMetadataBlog::default()),
..MFile::default()
},
MType::Contact => MFile {
contact: Some(FileMetadataContact::default()),
..MFile::default()
},
MType::Generic => MFile::default(),
MType::Index => MFile {
index: Some(FileMetadataIndex::default()),
..MFile::default()
},
MType::Portfolio => MFile {
portfolio: Some(FileMetadataPortfolio::default()),
..MFile::default()
},
},
|data| data,
)
} }