| @@ -29,19 +29,34 @@ impl<'event> File<'event> { · 28 unmodified lines |
| 29 | 29 | .expect("BUG: Section did not have id from lookup") |
| 30 | 30 | .to_mut(nl)) |
| 31 | 31 | } |
| 32 | | − |
| 33 | | − /// Returns an mutable section with a given `name` and optional `subsection_name`, _if it exists_. |
| 32 | + /// Returns an mutable section with a given `name` and optional `subsection_name`, _if it exists_, or create a new section. |
| 34 | 33 | pub fn section_mut_or_create_new<'a>( |
| 35 | 34 | &'a mut self, |
| 36 | 35 | name: impl AsRef<str>, |
| 37 | 36 | subsection_name: Option<&str>, |
| 37 | + ) -> Result<SectionMut<'a, 'event>, section::header::Error> { |
| 38 | + self.section_mut_or_create_new_filter(name, subsection_name, &mut |_| true) |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns an mutable section with a given `name` and optional `subsection_name`, _if it exists_ **and** passes `filter`, or create |
| 42 | + /// a new section. |
| 43 | + pub fn section_mut_or_create_new_filter<'a>( |
| 44 | + &'a mut self, |
| 45 | + name: impl AsRef<str>, |
| 46 | + subsection_name: Option<&str>, |
| 47 | + filter: &mut MetadataFilter, |
| 38 | 48 | ) -> Result<SectionMut<'a, 'event>, section::header::Error> { |
| 39 | 49 | let name = name.as_ref(); |
| 40 | 50 | match self |
| 41 | 51 | .section_ids_by_name_and_subname(name.as_ref(), subsection_name) |
| 42 | | − .map(|it| it.rev().next().expect("BUG: Section lookup vec was empty")) |
| 43 | | − { |
| 44 | | − Ok(id) => { |
| 52 | + .ok() |
| 53 | + .and_then(|it| { |
| 54 | + it.rev().find(|id| { |
| 55 | + let s = &self.sections[id]; |
| 56 | + filter(s.meta()) |
| 57 | + }) |
| 58 | + }) { |
| 59 | + Some(id) => { |
| 45 | 60 | let nl = self.detect_newline_style_smallvec(); |
| 46 | 61 | Ok(self |
| 47 | 62 | .sections |
| @@ -49,7 +64,7 @@ impl<'event> File<'event> { · 1 unmodified line |
| 49 | 64 | .expect("BUG: Section did not have id from lookup") |
| 50 | 65 | .to_mut(nl)) |
| 51 | 66 | } |
| 52 | | − Err(_) => self.new_section(name.to_owned(), subsection_name.map(|n| Cow::Owned(n.to_owned()))), |
| 67 | + None => self.new_section(name.to_owned(), subsection_name.map(|n| Cow::Owned(n.to_owned()))), |
| 53 | 68 | } |
| 54 | 69 | } |
| 55 | 70 | |