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
|
use std::ops::Add;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum Punct {
Nothing,
Space,
Comma,
Dot,
Colon,
}
impl Punct {
pub fn as_str(self) -> &'static str {
match self {
Self::Nothing => "",
Self::Space => " ",
Self::Comma => ", ",
Self::Dot => ". ",
Self::Colon => ": ",
}
}
}
impl Add for Punct {
type Output = Punct;
fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Self::Nothing, Self::Nothing) => Self::Nothing,
(Self::Nothing, Self::Space)
| (Self::Space, Self::Nothing)
| (Self::Space, Self::Space) => Self::Space,
(Self::Nothing, Self::Comma)
| (Self::Space, Self::Comma)
| (Self::Comma, Self::Nothing)
| (Self::Comma, Self::Space)
| (Self::Comma, Self::Comma)
| (Self::Comma, Self::Dot)
| (Self::Dot, Self::Comma) => Self::Comma,
(Self::Nothing, Self::Dot)
| (Self::Space, Self::Dot)
| (Self::Dot, Self::Nothing)
| (Self::Dot, Self::Space)
| (Self::Dot, Self::Dot) => Self::Dot,
(Self::Nothing, Self::Colon)
| (Self::Space, Self::Colon)
| (Self::Comma, Self::Colon)
| (Self::Dot, Self::Colon)
| (Self::Colon, Self::Nothing)
| (Self::Colon, Self::Space)
| (Self::Colon, Self::Comma)
| (Self::Colon, Self::Dot)
| (Self::Colon, Self::Colon) => Self::Colon,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum Inline {
Regular(String),
Italic(String),
Quoted(String),
Link { url: String, alt: String },
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Default)]
pub struct InlineBuilder {
items: Vec<(Inline, Punct)>,
}
impl InlineBuilder {
pub fn push(&mut self, inline: Inline, leading: Punct, trailing: Punct) {
if let Some((_, last)) = self.items.last_mut() {
*last = *last + leading;
}
self.items.push((inline, trailing));
}
pub fn finish(mut self) -> impl Iterator<Item = (Inline, Punct)> {
if let Some((_, last)) = self.items.last_mut() {
*last = Punct::Nothing;
}
self.items.into_iter()
}
}
|