c6o_obsidian_export/
frontmatter.rs1use serde_yaml::Result;
2
3pub type Frontmatter = serde_yaml::Mapping;
28
29pub fn frontmatter_from_str(mut s: &str) -> Result<Frontmatter> {
30 if s.is_empty() {
31 s = "{}";
32 }
33 let frontmatter: Frontmatter = serde_yaml::from_str(s)?;
34 Ok(frontmatter)
35}
36
37pub fn frontmatter_to_str(frontmatter: Frontmatter) -> Result<String> {
38 if frontmatter.is_empty() {
39 return Ok("---\n---\n".to_string());
40 }
41
42 let mut buffer = String::new();
43 buffer.push_str(&serde_yaml::to_string(&frontmatter)?);
44 buffer.push_str("---\n");
45 Ok(buffer)
46}
47
48#[derive(Debug, Clone, Copy)]
49pub enum FrontmatterStrategy {
51 Auto,
53 Always,
56 Never,
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use pretty_assertions::assert_eq;
64 use serde_yaml::Value;
65
66 #[test]
67 fn empty_string_should_yield_empty_frontmatter() {
68 assert_eq!(frontmatter_from_str("").unwrap(), Frontmatter::new())
69 }
70
71 #[test]
72 fn empty_frontmatter_to_str() {
73 let frontmatter = Frontmatter::new();
74 assert_eq!(
75 frontmatter_to_str(frontmatter).unwrap(),
76 format!("---\n---\n")
77 )
78 }
79
80 #[test]
81 fn nonempty_frontmatter_to_str() {
82 let mut frontmatter = Frontmatter::new();
83 frontmatter.insert(
84 Value::String("foo".to_string()),
85 Value::String("bar".to_string()),
86 );
87 assert_eq!(
88 frontmatter_to_str(frontmatter).unwrap(),
89 format!("---\nfoo: bar\n---\n")
90 )
91 }
92}