2021 day16/rust: make generic on error type
This commit is contained in:
parent
8e29118629
commit
5a8d17a4e7
1 changed files with 8 additions and 8 deletions
|
@ -7,7 +7,7 @@ use std::{
|
|||
|
||||
use nom::{
|
||||
bits::complete as bits,
|
||||
multi::{many0, many_m_n}, Offset,
|
||||
multi::{many0, many_m_n}, error::ParseError, Offset,
|
||||
};
|
||||
use nom::{combinator::map, sequence::pair};
|
||||
use parsers::fold_till;
|
||||
|
@ -15,7 +15,7 @@ use parsers::fold_till;
|
|||
mod parsers;
|
||||
|
||||
type Input<'a> = (&'a [u8], usize);
|
||||
type IResult<'a, T> = nom::IResult<Input<'a>, T>;
|
||||
type IResult<'a, T, E> = nom::IResult<Input<'a>, T, E>;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Packet {
|
||||
|
@ -24,7 +24,7 @@ struct Packet {
|
|||
}
|
||||
|
||||
impl Packet {
|
||||
pub fn parse(i: Input) -> IResult<Packet> {
|
||||
pub fn parse<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, Packet, E> {
|
||||
map(
|
||||
pair(bits::take(3_usize), PacketType::parse),
|
||||
|(version, typ)| Packet { version, typ },
|
||||
|
@ -52,7 +52,7 @@ enum PacketType {
|
|||
}
|
||||
|
||||
impl PacketType {
|
||||
pub fn parse(i: Input) -> IResult<PacketType> {
|
||||
pub fn parse<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, PacketType, E> {
|
||||
let (i, operator) = map(bits::take(3_usize), |type_id: u8| {
|
||||
Operator::try_from(type_id)
|
||||
})(i)?;
|
||||
|
@ -68,14 +68,14 @@ impl PacketType {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_sub_packets(i: Input) -> IResult<Vec<Packet>> {
|
||||
fn parse_sub_packets<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, Vec<Packet>, E> {
|
||||
enum LengthType {
|
||||
Bits(usize),
|
||||
Packets(usize),
|
||||
}
|
||||
|
||||
impl LengthType {
|
||||
pub fn parse(i: Input) -> IResult<Self> {
|
||||
pub fn parse<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, Self, E> {
|
||||
let (i, length_type_id) = bits::take(1_usize)(i)?;
|
||||
match length_type_id {
|
||||
0 => map(bits::take(15_usize), LengthType::Bits)(i),
|
||||
|
@ -110,7 +110,7 @@ impl PacketType {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_literal_value(i: Input) -> IResult<usize> {
|
||||
fn parse_literal_value<'a, E: ParseError<Input<'a>>>(i: Input<'a>) -> IResult<'a, usize, E> {
|
||||
fold_till(
|
||||
pair(bits::take(1_usize), bits::take(4_usize)),
|
||||
|| 0,
|
||||
|
@ -199,7 +199,7 @@ fn main() {
|
|||
.flatten()
|
||||
.collect();
|
||||
|
||||
let packet = Packet::parse((&bytes, 0)).unwrap().1;
|
||||
let packet = Packet::parse::<nom::error::Error<_>>((&bytes, 0)).unwrap().1;
|
||||
println!("{}", packet.version_sum());
|
||||
println!("{}", packet.typ.evaluate());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue