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
use std::ops;
#[derive(Debug)]
pub struct Lines(pub Vec<String>);
impl Lines {
pub fn new(v: Vec<String>) -> Lines {
Lines { 0: v }
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn update(&mut self, first: usize, last: usize, linedata: Vec<String>) {
let range = first..last;
let _v = self.0.splice(range, linedata);
}
}
impl ops::Index<usize> for Lines {
type Output = String;
fn index(&self, idx: usize) -> &String {
&self.0[idx]
}
}
impl ops::Deref for Lines {
type Target = [String];
fn deref(&self) -> &[String] {
&self.0
}
}
#[cfg(test)]
mod tests {
use lines::Lines;
const LINES: [&'static str; 8] =
["This", "is", "an", "example", "of", "some", "lines", "."];
#[test]
fn lines_can_delete() {
let v = LINES.iter().map(|s| s.to_string()).collect();
let mut l = Lines::new(v);
l.update(1, 7, Vec::new());
assert_eq!(l[0], "This");
assert_eq!(l[1], ".");
assert_eq!(l.len(), 2);
}
#[test]
fn lines_can_insert() {
let v = LINES.iter().map(|s| s.to_string()).collect();
let mut l = Lines::new(v);
let newlines = vec![
"haaargl".to_string(),
"waaarglll".to_string(),
"blaaargl".to_string(),
];
l.update(2, 2, newlines);
assert_eq!(l[2], "haaargl");
assert_eq!(l.len(), 11);
}
#[test]
fn lines_can_update() {
let v = LINES.iter().map(|s| s.to_string()).collect();
let mut l = Lines::new(v);
let newlines = vec![
"haaargl".to_string(),
"waaarglll".to_string(),
"blaaargl".to_string(),
];
l.update(1, 7, newlines);
assert_eq!(l[0], "This");
assert_eq!(l[3], "blaaargl");
assert_eq!(l[4], ".");
assert_eq!(l.len(), 5);
}
}