Function git_config::parse::from_bytes
source · Expand description
Attempt to zero-copy parse the provided bytes, passing results to dispatch
.
Examples found in repository?
src/parse/events.rs (lines 298-322)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
fn from_bytes<'a, 'b>(
input: &'a [u8],
convert: impl Fn(Event<'a>) -> Event<'b>,
filter: Option<fn(&Event<'a>) -> bool>,
) -> Result<Events<'b>, parse::Error> {
let mut header = None;
let mut events = section::Events::default();
let mut frontmatter = FrontMatterEvents::default();
let mut sections = Vec::new();
parse::from_bytes(input, |e: Event<'_>| match e {
Event::SectionHeader(next_header) => {
match header.take() {
None => {
frontmatter = std::mem::take(&mut events).into_iter().collect();
}
Some(prev_header) => {
sections.push(parse::Section {
header: prev_header,
events: std::mem::take(&mut events),
});
}
};
header = match convert(Event::SectionHeader(next_header)) {
Event::SectionHeader(h) => h,
_ => unreachable!("BUG: convert must not change the event type, just the lifetime"),
}
.into();
}
event => {
if filter.map_or(true, |f| f(&event)) {
events.push(convert(event))
}
}
})?;
match header {
None => {
frontmatter = events.into_iter().collect();
}
Some(prev_header) => {
sections.push(parse::Section {
header: prev_header,
events: std::mem::take(&mut events),
});
}
}
Ok(Events { frontmatter, sections })
}