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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::fmt;
use std::io;
use util::{read_file_vec, write_file_vec};
#[derive(Clone, Debug, PartialEq)]
pub struct Org {
depth: usize,
heading: String,
content: Vec<String>,
subtrees: Vec<Org>,
}
impl Org {
pub fn new() -> Org {
Org {
depth: 0,
heading: String::new(),
content: Vec::new(),
subtrees: Vec::new(),
}
}
pub fn from_file(fname: &str) -> io::Result<Org> {
let file_contents: Vec<String> = match read_file_vec(fname) {
Ok(v) => v,
Err(e) => return Err(e),
};
Self::from_vec(&file_contents)
}
pub fn from_vec(contents: &[String]) -> io::Result<Org> {
let mut org = Default::default();
process_subtree(&mut org, contents, 0);
Ok(org)
}
pub fn to_file(&self, fname: &str) -> io::Result<()> {
let contents = self.to_vec();
write_file_vec(fname, &contents)
}
pub fn to_vec(&self) -> Vec<String> {
let mut contents = Vec::new();
if self.depth > 0 {
contents.push(self.full_heading());
}
for line in &self.content {
contents.push(line.clone());
}
for subtree in &self.subtrees {
contents.append(&mut subtree.to_vec());
}
contents
}
pub fn depth(&self) -> usize {
self.depth
}
pub fn heading(&self) -> &str {
&self.heading
}
pub fn set_heading(&mut self, heading: &str) {
self.heading = String::from(heading)
}
pub fn full_heading(&self) -> String {
if self.depth == 0 {
String::new()
} else {
format!("{} {}", "*".repeat(self.depth), self.heading)
}
}
pub fn content_as_ref(&self) -> &Vec<String> {
&self.content
}
pub fn content_as_mut(&mut self) -> &mut Vec<String> {
&mut self.content
}
pub fn subtrees_as_ref(&self) -> &Vec<Org> {
&self.subtrees
}
pub fn subtrees_as_mut(&mut self) -> &mut Vec<Org> {
&mut self.subtrees
}
}
impl Default for Org {
fn default() -> Org {
Org::new()
}
}
impl fmt::Display for Org {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let contents = self.to_vec();
let len = contents.len();
let mut res = String::new();
for (i, line) in contents.into_iter().enumerate() {
res += &line;
if i < len {
res += "\n";
}
}
write!(f, "{}", res)
}
}
fn process_subtree(org: &mut Org, contents: &[String], index: usize) -> usize {
let depth = org.depth;
let mut i = index;
while i < contents.len() {
let line = &contents[i];
let (heading, level) = get_heading(line);
if level == 0 {
org.content.push(line.clone());
i += 1;
} else if level <= depth {
return i;
} else {
let mut subtree = Org {
depth: depth + 1,
heading: heading,
content: Vec::new(),
subtrees: Vec::new(),
};
i = process_subtree(&mut subtree, contents, i + 1);
org.subtrees.push(subtree);
}
}
i
}
fn get_heading(line: &str) -> (String, usize) {
let mut level = 0;
for c in line.chars() {
if c == '*' {
level += 1;
} else {
break;
}
}
let heading = if level < line.chars().count() {
String::from(&line[level..])
} else {
String::new()
};
(heading.trim().to_string(), level)
}
#[cfg(test)]
mod tests {
use org::get_heading;
#[test]
fn test_get_heading() {
assert_eq!(get_heading(""), (String::from(""), 0));
assert_eq!(get_heading("Test"), (String::from("Test"), 0));
assert_eq!(get_heading("* Test"), (String::from("Test"), 1));
assert_eq!(get_heading("***Test"), (String::from("Test"), 3));
assert_eq!(get_heading("*****"), (String::new(), 5));
}
}