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
use std::{iter, slice};
#[derive(Debug, PartialEq, Eq)]
pub struct ConfigBlock {
name: String,
params: Vec<String>,
inner: Vec<ConfigBlock>
}
impl ConfigBlock {
pub fn new(name: String, params: Vec<String>, inner: Vec<ConfigBlock>) -> ConfigBlock {
ConfigBlock {
name: name,
params: params,
inner: inner
}
}
pub fn add_block(&mut self, block: ConfigBlock) {
self.inner.push(block);
}
pub fn matching<'a>(&'a self, name: &'a str) -> ConfigIter<'a> {
ConfigIter {
it: self.inner.iter(),
name: name
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn inner<'a>(&'a self) -> &'a Vec<ConfigBlock> {
&self.inner
}
pub fn len(&self) -> usize {
self.params.len()
}
pub fn get(&self, i: usize) -> &str {
&self.params[i]
}
pub fn get_opt(&self, i: usize) -> Option<&str> {
if i < self.params.len() {
Some(&self.params[i])
} else {
None
}
}
}
pub struct ConfigIter<'a> {
it: slice::Iter<'a, ConfigBlock>,
name: &'a str
}
impl <'a> iter::Iterator for ConfigIter <'a> {
type Item = &'a ConfigBlock;
fn next(&mut self) -> Option<&'a ConfigBlock> {
loop {
match self.it.next() {
Some(c) if c.name() == self.name => return Some(c),
Some(_) => {},
None => return None
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, self.it.size_hint().1)
}
}