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
use regex::Regex;
use std::fmt;
#[derive(Debug, PartialEq, Eq)]
pub struct Bread {
pub(super) file_path: String,
pub(super) crumbs: Vec<Crumb>,
}
impl Bread {
pub fn new(f: String, crumbs: Vec<Crumb>) -> Self {
Bread {
file_path: f,
crumbs,
}
}
pub fn to_org(&self) -> Result<String, !> {
let mut content = format!("* {}\n", self.file_path);
self.crumbs
.iter()
.filter_map(|c| c.to_org())
.for_each(|org_inside| {
content += "** ";
content += &org_inside;
content += "\n"
});
Ok(content)
}
}
impl fmt::Display for Bread {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "|-- {}\n", self.file_path)?;
for c in &self.crumbs {
write!(f, " |-- {}\n", c)?;
}
Ok(())
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Crumb {
pub(crate) line_num: usize,
tails_line_num: Vec<usize>,
pub(crate) keyword: Option<String>,
pub(crate) content: String,
}
impl Crumb {
pub fn filter_keywords(&mut self, re: &Regex) -> bool {
match re.captures(&self.content) {
Some(a) => {
self.keyword = Some(a[1].to_string());
self.content = a[2].to_string();
true
}
None => false,
}
}
pub fn has_tail(&self) -> bool {
self.content.ends_with("...")
}
pub fn add_tail(&mut self, tail: Self) {
self.content = self.content.trim_end().trim_end_matches("...").to_string();
self.content.push(' ');
self.content.push_str(&tail.content);
self.tails_line_num.push(tail.line_num);
}
pub fn new(line_num: usize, keyword: Option<String>, content: String) -> Self {
Self {
line_num,
keyword,
tails_line_num: vec![],
content,
}
}
pub fn to_org(&self) -> Option<String> {
match &self.keyword {
Some(k) => Some(format!("{} {}", k, self.content)),
None => None,
}
}
pub fn all_lines_num(&self) -> Vec<usize> {
let mut a = vec![self.line_num];
a.append(&mut self.tails_line_num.clone());
a
}
}
impl fmt::Display for Crumb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let a = match self.keyword {
Some(ref k) => {
let mut c = String::from(k);
c.push_str(": ");
c
}
None => "".to_string(),
};
write!(f, "Line {}: {}{}", self.line_num, a, self.content)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_keyowrds() {
let mut a: Crumb = Default::default();
a.content = "TODO: test1".to_string();
assert!(a.filter_keywords(&Regex::new(&format!("({}):\\s*(.*)", "TODO")).unwrap()));
assert_eq!(a.keyword, Some("TODO".to_string()));
a.content = "TODO: test1".to_string();
assert!(
a.filter_keywords(&Regex::new(&format!("({}|{}):\\s*(.*)", "TODO", "MARK")).unwrap())
);
assert_eq!(a.keyword, Some("TODO".to_string()));
assert_eq!(a.content, "test1");
let mut a: Crumb = Default::default();
a.content = "test1".to_string();
assert!(!a.filter_keywords(&Regex::new(&format!("({}):\\s*(.*)", "TODO")).unwrap()));
assert_eq!(a.keyword, None);
}
#[test]
fn test_to_org() {
let b0 = Bread::new("a".into(), vec![]);
assert_eq!(b0.to_org().unwrap(), "* a\n".to_string());
let b1 = Bread::new(
"a".into(),
vec![
Crumb::new(1, None, "1".to_string()),
Crumb::new(2, Some("TODO".to_string()), "2".to_string()),
],
);
assert_eq!(b1.to_org().unwrap(), "* a\n** TODO 2\n".to_string());
}
}