mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Implement 2022 day 1
This commit is contained in:
19
2022/src/common.rs
Normal file
19
2022/src/common.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
//! Common helper utilities to all days
|
||||
|
||||
use anyhow::Result;
|
||||
use nom::Finish;
|
||||
use nom::Parser;
|
||||
|
||||
/// Parse input from some nom parser and return as an anyhow result
|
||||
///
|
||||
/// This method exists as a convenience because nom's errors cannot otherwise be easily converted to
|
||||
/// an anyhow error, and I don't want to keep track of custom error implementations here.
|
||||
pub fn parse_input<'a, O>(
|
||||
input: &'a [u8],
|
||||
mut parser: impl Parser<&'a [u8], O, nom::error::Error<&'a [u8]>>,
|
||||
) -> Result<O> {
|
||||
match parser.parse(input).finish() {
|
||||
Ok((_, value)) => Ok(value),
|
||||
Err(err) => anyhow::bail!("Failed to parse at: {err:?}"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user