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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::compound::Compound;
use crate::line_parser::LineParser;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CompositeStyle {
Paragraph,
Header(u8),
ListItem,
Code,
Quote,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Composite<'a> {
pub style: CompositeStyle,
pub compounds: Vec<Compound<'a>>,
}
impl<'a> From<Vec<Compound<'a>>> for Composite<'a> {
fn from(compounds: Vec<Compound<'a>>) -> Composite<'a> {
Composite {
style: CompositeStyle::Paragraph,
compounds,
}
}
}
impl<'a> Composite<'a> {
pub fn new() -> Composite<'a> {
Composite {
style: CompositeStyle::Paragraph,
compounds: Vec::new(),
}
}
pub fn from_inline(md: &'a str) -> Composite<'a> {
LineParser::from(md).inline()
}
#[inline(always)]
pub fn is_code(&self) -> bool {
match self.style {
CompositeStyle::Code { .. } => true,
_ => false,
}
}
#[inline(always)]
pub fn is_list_item(&self) -> bool {
match self.style {
CompositeStyle::ListItem { .. } => true,
_ => false,
}
}
#[inline(always)]
pub fn is_quote(&self) -> bool {
match self.style {
CompositeStyle::Quote { .. } => true,
_ => false,
}
}
#[inline(always)]
pub fn char_length(&self) -> usize {
self.compounds
.iter()
.fold(0, |sum, compound| sum + compound.as_str().chars().count())
}
pub fn trim_left_spaces(&mut self) {
loop {
if self.compounds.len() == 0 {
break;
}
if self.compounds[0].code {
break;
}
self.compounds[0].trim_left_spaces();
if self.compounds[0].is_empty() {
self.compounds.remove(0);
} else {
break;
}
}
}
pub fn trim_right_spaces(&mut self) {
loop {
if self.compounds.len() == 0 {
break;
}
let last = self.compounds.len() - 1;
if self.compounds[last].code {
break;
}
self.compounds[last].trim_right_spaces();
if self.compounds[last].is_empty() {
self.compounds.remove(last);
} else {
break;
}
}
}
pub fn trim_spaces(&mut self) {
self.trim_left_spaces();
self.trim_right_spaces();
}
pub fn is_empty(&self) -> bool {
self.compounds.len() == 0
}
}
#[cfg(test)]
mod tests {
use crate::composite::*;
use crate::compound::*;
#[test]
fn composite_trim() {
let mut left = Composite::from_inline(" *some* text ");
left.trim_spaces();
assert_eq!(
left,
Composite {
style: CompositeStyle::Paragraph,
compounds: vec![
Compound::raw_str("some").italic(),
Compound::raw_str(" text"),
]
}
);
}
#[test]
fn composite_trim_keep_code() {
let mut left = Composite::from_inline(" ` ` ");
left.trim_spaces();
assert_eq!(
left,
Composite {
style: CompositeStyle::Paragraph,
compounds: vec![
Compound::raw_str(" ").code(),
]
}
);
}
#[test]
fn empty_composite_trim() {
let mut left = Composite::from_inline(" * * ** `` ** ");
left.trim_left_spaces();
assert_eq!(left.compounds.len(), 0);
}
}