gdritter repos rrecutils / 5d37711
Add some utility functions/iterators Getty Ritter 6 years ago
1 changed file(s) with 37 addition(s) and 0 deletion(s). Collapse all Expand all
3030 pub fn size(&self) -> usize {
3131 self.fields.len()
3232 }
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 }
3339 }
3440
3541
5460 Some(ref t) => t == type_name,
5561 None => false,
5662 });
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;
5793 }
5894 }
5995
122158 val[1..].trim_left().to_owned()));
123159 if key == "%rec" {
124160 ctx.current_record_type = Some(val[1..].trim_left().to_owned());
161 current.rec_type = None;
125162 }
126163 } else {
127164 return Err(RecError::InvalidLine { ln: ln.to_owned() });