Add some utility functions/iterators
Getty Ritter
6 years ago
30 | 30 | pub fn size(&self) -> usize { |
31 | 31 | self.fields.len() |
32 | 32 | } |
33 | ||
34 | pub fn get<'a>(&'a self, name: &str) -> Option<&'a str> { | |
35 | self.fields.iter() | |
36 | .find(|&&(ref p, _)| p == name) | |
37 | .map(|&(_, ref q)| q.as_ref()) | |
38 | } | |
33 | 39 | } |
34 | 40 | |
35 | 41 | |
54 | 60 | Some(ref t) => t == type_name, |
55 | 61 | None => false, |
56 | 62 | }); |
63 | } | |
64 | ||
65 | pub fn iter_by_type<'a>(&'a self, type_name: &'a str) -> RecIterator<'a> { | |
66 | RecIterator { | |
67 | typ: type_name, | |
68 | rec: self.records.iter(), | |
69 | } | |
70 | } | |
71 | ||
72 | pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, Record> { | |
73 | self.records.iter() | |
74 | } | |
75 | } | |
76 | ||
77 | pub struct RecIterator<'a> { | |
78 | pub typ: &'a str, | |
79 | pub rec: std::slice::Iter<'a, Record>, | |
80 | } | |
81 | ||
82 | impl<'a> Iterator for RecIterator<'a> { | |
83 | type Item = &'a Record; | |
84 | ||
85 | fn next(&mut self) -> Option<&'a Record> { | |
86 | while let Some(r) = self.rec.next() { | |
87 | match r.rec_type { | |
88 | Some(ref n) if n == self.typ => return Some(r), | |
89 | _ => (), | |
90 | } | |
91 | } | |
92 | return None; | |
57 | 93 | } |
58 | 94 | } |
59 | 95 | |
122 | 158 | val[1..].trim_left().to_owned())); |
123 | 159 | if key == "%rec" { |
124 | 160 | ctx.current_record_type = Some(val[1..].trim_left().to_owned()); |
161 | current.rec_type = None; | |
125 | 162 | } |
126 | 163 | } else { |
127 | 164 | return Err(RecError::InvalidLine { ln: ln.to_owned() }); |