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