Skip to content

Commit

Permalink
fix: winnow deprecation warnings (#896)
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Wagner <[email protected]>
  • Loading branch information
nwagner84 authored Feb 5, 2025
1 parent d9fa5af commit c0a7ae3
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 99 deletions.
25 changes: 11 additions & 14 deletions src/fmt/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use winnow::combinator::{
alt, delimited, empty, opt, preceded, repeat, separated, terminated,
};
use winnow::error::ParserError;
use winnow::{PResult, Parser};
use winnow::{ModalResult, Parser};

use super::{Format, Fragments, Group, List, Modifier, Value};
use crate::matcher::occurrence::parse_occurrence_matcher;
use crate::matcher::subfield::parser::parse_subfield_matcher;
use crate::matcher::tag::parse_tag_matcher;
use crate::parser::{parse_string, parse_subfield_codes, ws};

pub(crate) fn parse_format(i: &mut &[u8]) -> PResult<Format> {
pub(crate) fn parse_format(i: &mut &[u8]) -> ModalResult<Format> {
(
parse_tag_matcher,
parse_occurrence_matcher,
Expand All @@ -42,7 +42,7 @@ pub(crate) fn parse_format(i: &mut &[u8]) -> PResult<Format> {
.parse_next(i)
}

fn parse_fragments(i: &mut &[u8]) -> PResult<Fragments> {
fn parse_fragments(i: &mut &[u8]) -> ModalResult<Fragments> {
alt((
ws(parse_list).map(Fragments::List),
ws(parse_group).map(Fragments::Group),
Expand All @@ -55,14 +55,11 @@ thread_local! {
pub static GROUP_LEVEL: RefCell<u32> = const { RefCell::new(0) };
}

fn group_level_inc(i: &mut &[u8]) -> PResult<()> {
fn group_level_inc(i: &mut &[u8]) -> ModalResult<()> {
GROUP_LEVEL.with(|level| {
*level.borrow_mut() += 1;
if *level.borrow() >= 32 {
Err(winnow::error::ErrMode::from_error_kind(
i,
winnow::error::ErrorKind::Many,
))
Err(winnow::error::ErrMode::from_input(i))
} else {
Ok(())
}
Expand All @@ -75,7 +72,7 @@ fn group_level_dec() {
})
}

fn parse_group(i: &mut &[u8]) -> PResult<Group> {
fn parse_group(i: &mut &[u8]) -> ModalResult<Group> {
(
terminated(ws('('), group_level_inc),
parse_modifier,
Expand Down Expand Up @@ -103,7 +100,7 @@ fn parse_group(i: &mut &[u8]) -> PResult<Group> {
.parse_next(i)
}

fn parse_value(i: &mut &[u8]) -> PResult<Value> {
fn parse_value(i: &mut &[u8]) -> ModalResult<Value> {
(
ws(parse_modifier),
opt(ws(parse_string).map(|s| {
Expand Down Expand Up @@ -139,11 +136,11 @@ fn parse_value(i: &mut &[u8]) -> PResult<Value> {
}

#[inline]
fn parse_list(i: &mut &[u8]) -> PResult<List> {
fn parse_list(i: &mut &[u8]) -> ModalResult<List> {
alt((parse_list_cons, parse_list_and_then)).parse_next(i)
}

fn parse_list_cons(i: &mut &[u8]) -> PResult<List> {
fn parse_list_cons(i: &mut &[u8]) -> ModalResult<List> {
separated(
2..,
alt((
Expand All @@ -157,7 +154,7 @@ fn parse_list_cons(i: &mut &[u8]) -> PResult<List> {
.parse_next(i)
}

fn parse_list_and_then(i: &mut &[u8]) -> PResult<List> {
fn parse_list_and_then(i: &mut &[u8]) -> ModalResult<List> {
separated(
2..,
alt((
Expand All @@ -170,7 +167,7 @@ fn parse_list_and_then(i: &mut &[u8]) -> PResult<List> {
.parse_next(i)
}

fn parse_modifier(i: &mut &[u8]) -> PResult<Option<Modifier>> {
fn parse_modifier(i: &mut &[u8]) -> ModalResult<Option<Modifier>> {
opt(preceded(
'?',
repeat(1.., alt(('l', 'u', 't', 'w'))).map(|codes: Vec<_>| {
Expand Down
53 changes: 27 additions & 26 deletions src/matcher/field/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winnow::combinator::{
alt, delimited, opt, preceded, repeat, terminated,
};
use winnow::error::ParserError;
use winnow::{PResult, Parser};
use winnow::{ModalResult, Parser};

use super::{
CardinalityMatcher, FieldMatcher, SingletonMatcher,
Expand All @@ -25,7 +25,7 @@ use crate::parser::ws;

pub(super) fn parse_exists_matcher(
i: &mut &[u8],
) -> PResult<ExistsMatcher> {
) -> ModalResult<ExistsMatcher> {
terminated(ws((parse_tag_matcher, parse_occurrence_matcher)), '?')
.map(|(t, o)| ExistsMatcher {
tag_matcher: t,
Expand All @@ -36,7 +36,7 @@ pub(super) fn parse_exists_matcher(

fn parse_subfields_matcher_dot(
i: &mut &[u8],
) -> PResult<SubfieldsMatcher> {
) -> ModalResult<SubfieldsMatcher> {
(
opt(ws(parse_quantifier)).map(Option::unwrap_or_default),
parse_tag_matcher,
Expand All @@ -61,7 +61,7 @@ fn parse_subfields_matcher_dot(
#[cfg(feature = "compat")]
fn parse_subfields_matcher_dollar(
i: &mut &[u8],
) -> PResult<SubfieldsMatcher> {
) -> ModalResult<SubfieldsMatcher> {
(
opt(ws(parse_quantifier)).map(Option::unwrap_or_default),
parse_tag_matcher,
Expand All @@ -85,7 +85,7 @@ fn parse_subfields_matcher_dollar(

fn parse_subfields_matcher_bracket(
i: &mut &[u8],
) -> PResult<SubfieldsMatcher> {
) -> ModalResult<SubfieldsMatcher> {
(
opt(ws(parse_quantifier)).map(Option::unwrap_or_default),
parse_tag_matcher,
Expand All @@ -110,7 +110,7 @@ fn parse_subfields_matcher_bracket(
#[inline]
pub(super) fn parse_subfields_matcher(
i: &mut &[u8],
) -> PResult<SubfieldsMatcher> {
) -> ModalResult<SubfieldsMatcher> {
alt((
parse_subfields_matcher_dot,
parse_subfields_matcher_bracket,
Expand All @@ -124,7 +124,7 @@ pub(super) fn parse_subfields_matcher(
#[inline]
pub(super) fn parse_singleton_matcher(
i: &mut &[u8],
) -> PResult<SingletonMatcher> {
) -> ModalResult<SingletonMatcher> {
alt((
parse_exists_matcher.map(SingletonMatcher::Exists),
parse_subfields_matcher.map(SingletonMatcher::Subfields),
Expand All @@ -135,7 +135,7 @@ pub(super) fn parse_singleton_matcher(
/// Parse a [CardinalityMatcher] expression.
pub(super) fn parse_cardinality_matcher(
i: &mut &[u8],
) -> PResult<CardinalityMatcher> {
) -> ModalResult<CardinalityMatcher> {
preceded(
ws('#'),
(
Expand Down Expand Up @@ -167,7 +167,7 @@ pub(super) fn parse_cardinality_matcher(
#[inline(always)]
fn parse_field_singleton_matcher(
i: &mut &[u8],
) -> PResult<FieldMatcher> {
) -> ModalResult<FieldMatcher> {
parse_singleton_matcher
.map(FieldMatcher::Singleton)
.parse_next(i)
Expand All @@ -176,14 +176,16 @@ fn parse_field_singleton_matcher(
#[inline(always)]
fn parse_field_cardinality_matcher(
i: &mut &[u8],
) -> PResult<FieldMatcher> {
) -> ModalResult<FieldMatcher> {
parse_cardinality_matcher
.map(FieldMatcher::Cardinality)
.parse_next(i)
}

#[inline]
fn parse_field_exists_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
fn parse_field_exists_matcher(
i: &mut &[u8],
) -> ModalResult<FieldMatcher> {
alt((
parse_exists_matcher.map(|matcher| {
FieldMatcher::Singleton(SingletonMatcher::Exists(matcher))
Expand Down Expand Up @@ -234,14 +236,11 @@ fn group_level_reset() {
FIELD_MATCHER_GROUP_LEVEL.with(|level| *level.borrow_mut() = 0);
}

fn group_level_inc(i: &mut &[u8]) -> PResult<()> {
fn group_level_inc(i: &mut &[u8]) -> ModalResult<()> {
FIELD_MATCHER_GROUP_LEVEL.with(|level| {
*level.borrow_mut() += 1;
if *level.borrow() >= 256 {
Err(winnow::error::ErrMode::from_error_kind(
i,
winnow::error::ErrorKind::Many,
))
Err(winnow::error::ErrMode::from_input(i))
} else {
Ok(())
}
Expand All @@ -255,7 +254,9 @@ fn group_level_dec() {
}

#[inline]
fn parse_field_group_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
fn parse_field_group_matcher(
i: &mut &[u8],
) -> ModalResult<FieldMatcher> {
delimited(
terminated(ws('('), group_level_inc),
alt((
Expand All @@ -272,7 +273,7 @@ fn parse_field_group_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
}

#[inline]
fn parse_field_not_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
fn parse_field_not_matcher(i: &mut &[u8]) -> ModalResult<FieldMatcher> {
preceded(
ws('!'),
alt((
Expand All @@ -288,8 +289,8 @@ fn parse_field_not_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
.parse_next(i)
}

fn parse_field_or_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> PResult<FieldMatcher> {
fn parse_field_or_matcher(i: &mut &[u8]) -> ModalResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> ModalResult<FieldMatcher> {
ws(alt((
parse_field_and_matcher,
parse_field_xor_matcher,
Expand All @@ -309,8 +310,8 @@ fn parse_field_or_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
.parse_next(i)
}

fn parse_field_xor_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> PResult<FieldMatcher> {
fn parse_field_xor_matcher(i: &mut &[u8]) -> ModalResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> ModalResult<FieldMatcher> {
ws(alt((
parse_field_and_matcher,
parse_field_group_matcher,
Expand All @@ -329,8 +330,8 @@ fn parse_field_xor_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
.parse_next(i)
}

fn parse_field_and_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> PResult<FieldMatcher> {
fn parse_field_and_matcher(i: &mut &[u8]) -> ModalResult<FieldMatcher> {
let atom = |i: &mut &[u8]| -> ModalResult<FieldMatcher> {
ws(alt((
parse_field_group_matcher,
parse_field_cardinality_matcher,
Expand All @@ -351,7 +352,7 @@ fn parse_field_and_matcher(i: &mut &[u8]) -> PResult<FieldMatcher> {
#[inline(always)]
fn parse_field_composite_matcher(
i: &mut &[u8],
) -> PResult<FieldMatcher> {
) -> ModalResult<FieldMatcher> {
alt((
parse_field_or_matcher,
parse_field_xor_matcher,
Expand All @@ -363,7 +364,7 @@ fn parse_field_composite_matcher(
/// Parse a [FieldMatcher] expression.
pub(crate) fn parse_field_matcher(
i: &mut &[u8],
) -> PResult<FieldMatcher> {
) -> ModalResult<FieldMatcher> {
ws(alt((
parse_field_composite_matcher,
parse_field_group_matcher,
Expand Down
8 changes: 4 additions & 4 deletions src/matcher/occurrence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ impl Display for OccurrenceMatcher {
#[inline]
fn parse_occurrence_matcher_inner(
i: &mut &[u8],
) -> PResult<Occurrence> {
) -> ModalResult<Occurrence> {
parse_occurrence_ref.map(Occurrence::from).parse_next(i)
}

#[inline]
fn parse_occurrence_matcher_exact(
i: &mut &[u8],
) -> PResult<OccurrenceMatcher> {
) -> ModalResult<OccurrenceMatcher> {
parse_occurrence_matcher_inner
.verify(|occurrence| occurrence.as_bytes() != b"00")
.map(OccurrenceMatcher::Exact)
Expand All @@ -127,7 +127,7 @@ fn parse_occurrence_matcher_exact(
#[inline]
fn parse_occurrence_matcher_range(
i: &mut &[u8],
) -> PResult<OccurrenceMatcher> {
) -> ModalResult<OccurrenceMatcher> {
separated_pair(
parse_occurrence_matcher_inner,
'-',
Expand All @@ -140,7 +140,7 @@ fn parse_occurrence_matcher_range(

pub(crate) fn parse_occurrence_matcher(
i: &mut &[u8],
) -> PResult<OccurrenceMatcher> {
) -> ModalResult<OccurrenceMatcher> {
alt((
preceded(
'/',
Expand Down
4 changes: 2 additions & 2 deletions src/matcher/operator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Display};

use winnow::combinator::alt;
use winnow::{PResult, Parser};
use winnow::{ModalResult, Parser};

/// Relational Operator
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -137,7 +137,7 @@ impl quickcheck::Arbitrary for RelationalOp {
#[inline]
pub(crate) fn parse_relational_operator(
i: &mut &[u8],
) -> PResult<RelationalOp> {
) -> ModalResult<RelationalOp> {
use RelationalOp::*;

alt((
Expand Down
6 changes: 4 additions & 2 deletions src/matcher/quantifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::{self, Display};

use winnow::combinator::alt;
use winnow::{PResult, Parser};
use winnow::{ModalResult, Parser};

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum Quantifier {
Expand Down Expand Up @@ -30,7 +30,9 @@ impl quickcheck::Arbitrary for Quantifier {
}

#[inline]
pub(crate) fn parse_quantifier(i: &mut &[u8]) -> PResult<Quantifier> {
pub(crate) fn parse_quantifier(
i: &mut &[u8],
) -> ModalResult<Quantifier> {
alt(("ALL".value(Quantifier::All), "ANY".value(Quantifier::Any)))
.parse_next(i)
}
Expand Down
Loading

0 comments on commit c0a7ae3

Please sign in to comment.