1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
use std::{borrow::Cow, fmt};
use paige::Frontmatter;
#[derive(Clone, Debug)]
pub struct PageFrontmatter<'r> {
description: Option<Cow<'r, str>>,
}
impl<'r> PageFrontmatter<'r> {
pub fn description(&self) -> Option<&Cow<'r, str>> {
self.description.as_ref()
}
pub fn set_description<D: Into<Cow<'r, str>>>(&mut self, description: D) {
self.description = Some(description.into());
}
pub fn has_any(&self) -> bool {
self.description.is_some()
}
pub fn from_frontmatter(&mut self, fm: &'r Frontmatter<'r>) {
self.description = fm.get("description").map(|s| Cow::Borrowed(s));
}
}
impl<'r> Default for PageFrontmatter<'r> {
fn default() -> Self {
Self { description: None }
}
}
impl<'r> fmt::Display for PageFrontmatter<'r> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.has_any() {
return Ok(());
}
macro_rules! write_field {
($field:ident) => {
writeln!(f, "{}={}", stringify!($field), $field)?;
};
}
writeln!(f, "---")?;
if let Some(description) = self.description.as_ref() {
write_field!(description);
}
write!(f, "---")
}
}
|