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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pub fn to_string(p1: &[crate::p1::Section]) -> String {
p1.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join("\n\n")
}
impl std::fmt::Display for crate::p1::Section {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "-- {}:", self.name.as_str())?;
if let Some(ref caption) = self.caption {
write!(f, " {}", caption)?;
}
for (k, v) in self.header.0.iter() {
write!(f, "\n{}: {}", k, v)?;
}
writeln!(f)?;
writeln!(f)?;
if let Some(ref body) = self.body {
write!(f, "\n{}\n\n", body)?;
}
for sub in self.sub_sections.0.iter() {
write!(f, "\n{}", sub)?;
}
writeln!(f)?;
writeln!(f)?;
writeln!(f)
}
}
impl std::fmt::Display for crate::p1::SubSection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "--- {}:", self.name.as_str())?;
if let Some(ref caption) = self.caption {
write!(f, " {}", caption)?;
}
for (k, v) in self.header.0.iter() {
write!(f, "\n{}: {}", k, v)?;
}
if let Some(ref body) = self.body {
write!(f, "\n\n{}", body)?;
}
writeln!(f)
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
use pretty_assertions::assert_eq;
#[test]
pub fn to_string() {
assert_eq!(
indoc!(
"
-- foo:
key: value
body ho
--- dodo: foo
-- bar:
bar body
"
),
super::to_string(&vec![
crate::p1::Section::with_name("foo")
.and_body("body ho")
.add_header("key", "value")
.add_sub_section(crate::p1::SubSection::with_name("dodo").and_caption("foo")),
crate::p1::Section::with_name("bar").and_body("bar body")
]),
)
}
}