forked from mocobeta/rust99
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
65 lines (58 loc) · 2.04 KB
/
lib.rs
File metadata and controls
65 lines (58 loc) · 2.04 KB
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
use bintree_strrepr::Tree;
pub fn to_dotstring(tree: &Tree) -> String {
match tree {
Tree::Node { value, left, right } => {
format!("{}{}{}", value, to_dotstring(left), to_dotstring(right))
}
Tree::End => ".".to_string(),
}
}
pub fn from_dotstring(dotstr: &str) -> Tree {
// TODO: refactor this
fn from_dotstring_rec(s: &str) -> (Tree, &str) {
assert!(!s.is_empty(), "Unaccepted input!");
let (c, rem) = s.split_at(1);
let (_, v) = c.char_indices().next().unwrap();
if v == '.' {
(Tree::end(), rem)
} else {
let mut t = Tree::leaf(v);
let (left, rem1) = from_dotstring_rec(rem);
let (right, rem2) = from_dotstring_rec(rem1);
t.replace_left(left);
t.replace_right(right);
(t, rem2)
}
}
let (t, rem) = from_dotstring_rec(dotstr);
assert!(rem.is_empty(), "Unaceppted input!");
t
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_dotstring() {
assert_eq!(to_dotstring(&Tree::from_string("")), ".");
assert_eq!(to_dotstring(&Tree::from_string("a")), "a..");
assert_eq!(to_dotstring(&Tree::from_string("a(b,)")), "ab...");
assert_eq!(to_dotstring(&Tree::from_string("a(,b)")), "a.b..");
assert_eq!(to_dotstring(&Tree::from_string("a(b,c)")), "ab..c..");
assert_eq!(
to_dotstring(&Tree::from_string("a(b(d,e),c(,f(g,)))")),
"abd..e..c.fg..."
);
}
#[test]
fn test_from_dotstring() {
assert_eq!(from_dotstring("."), Tree::from_string(""));
assert_eq!(from_dotstring("a.."), Tree::from_string("a"));
assert_eq!(from_dotstring("ab..."), Tree::from_string("a(b,)"));
assert_eq!(from_dotstring("a.b.."), Tree::from_string("a(,b)"));
assert_eq!(from_dotstring("ab..c.."), Tree::from_string("a(b,c)"));
assert_eq!(
from_dotstring("abd..e..c.fg..."),
Tree::from_string("a(b(d,e),c(,f(g,)))")
);
}
}