Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ license = "MPL-2.0"
repository = "https://github.com/EphyraSoftware/aetolia"

[dependencies]
nom = "7.1"
nom = "8.0"
nom-language = "0.1.0"
base64 = "0.22"
hex = "0.4"
lazy_static = "1.4"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.80.0"
channel = "1.84.1"
components = ["rustfmt", "clippy", "llvm-tools-preview"]
profile = "minimal"
50 changes: 25 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::parser::Error;
use nom::branch::alt;
use nom::combinator::recognize;
use nom::error::ParseError;
use nom::sequence::tuple;
use nom::{IResult, InputIter, InputLength, InputTake};
use nom::{IResult, Input, Parser};
use std::num::NonZeroUsize;

/// Common types.
Expand Down Expand Up @@ -41,15 +40,15 @@ pub mod prelude {
}

/// Streaming, single character matching the predicate
pub(crate) fn single<F, Input, Output, Error: ParseError<Input>>(
pub(crate) fn single<F, In, Output, Error: ParseError<In>>(
cond: F,
) -> impl Fn(Input) -> IResult<Input, Output, Error>
) -> impl Fn(In) -> IResult<In, Output, Error>
where
Input: InputIter<Item = Output> + InputLength + InputTake,
F: Fn(<Input as InputIter>::Item) -> bool,
In: Input<Item = Output>,
F: Fn(<In as Input>::Item) -> bool,
Output: Copy,
{
move |i: Input| {
move |i: In| {
match i.iter_elements().next() {
Some(c) if cond(c) => {
let (input, v) = i.take_split(1);
Expand All @@ -73,55 +72,56 @@ where
{
let (input, seq) = alt((
// Utf-8 2-byte sequence
recognize(tuple((
recognize((
single(|b| matches!(b, b'\xC2'..=b'\xDF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
)),
// Utf-8 3-byte sequence
alt((
recognize(tuple((
recognize((
single(|b| b == b'\xE0'),
single(|b| matches!(b, b'\xA0'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
recognize(tuple((
)),
recognize((
single(|b| matches!(b, b'\xE1'..=b'\xEC')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
recognize(tuple((
)),
recognize((
single(|b| b == b'\xED'),
single(|b| matches!(b, b'\x80'..=b'\x9F')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
recognize(tuple((
)),
recognize((
single(|b| matches!(b, b'\xEE'..=b'\xEF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
)),
)),
// Utf-8 4-byte sequence
alt((
recognize(tuple((
recognize((
single(|b| b == b'\xF0'),
single(|b| matches!(b, b'\x90'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
recognize(tuple((
)),
recognize((
single(|b| matches!(b, b'\xF1'..=b'\xF3')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
recognize(tuple((
)),
recognize((
single(|b| b == b'\xF4'),
single(|b| matches!(b, b'\x80'..=b'\x8F')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
single(|b| matches!(b, b'\x80'..=b'\xBF')),
))),
)),
)),
))(input)?;
))
.parse(input)?;

Ok((input, seq))
}
Expand All @@ -142,7 +142,7 @@ mod tests {
fn invalid_utf8() {
let mut input = "👍👌".as_bytes().to_vec();
input.extend_from_slice(&[1, 3, 4, 5, 2, 1]);
let (rem, seq) = many1(utf8_seq::<Error>)(input.as_slice()).unwrap();
let (rem, seq) = many1(utf8_seq::<Error>).parse(input.as_slice()).unwrap();
test_utils::check_rem(rem, 6);
assert_eq!(
seq.into_iter().flatten().cloned().collect::<Vec<_>>(),
Expand Down
37 changes: 22 additions & 15 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use nom::bytes::complete::{take_while, take_while1, take_while_m_n};
use nom::bytes::streaming::tag_no_case;
use nom::character::streaming::{char, crlf};
use nom::combinator::{cut, recognize};
use nom::error::{ErrorKind, FromExternalError, ParseError, VerboseError, VerboseErrorKind};
use nom::error::{ErrorKind, FromExternalError, ParseError};
use nom::multi::{fold_many0, many0, separated_list1};
use nom::sequence::{separated_pair, tuple};
use nom::sequence::separated_pair;
use nom::{AsChar, IResult, Parser};
use nom_language::error::{VerboseError, VerboseErrorKind};
use std::str::FromStr;
use std::sync::Mutex;

Expand Down Expand Up @@ -151,7 +152,7 @@ fn quoted_string<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
let (input, (_, content, _)) = tuple((char('"'), cut(safe_char), char('"')))(input)?;
let (input, (_, content, _)) = (char('"'), cut(safe_char), char('"')).parse(input)?;

Ok((input, content))
}
Expand All @@ -160,7 +161,7 @@ fn param_value<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
let (input, value) = alt((quoted_string, param_text))(input)?;
let (input, value) = alt((quoted_string, param_text)).parse(input)?;

Ok((input, value))
}
Expand All @@ -183,10 +184,11 @@ fn x_name<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
let (input, x_name) = recognize(tuple((
let (input, x_name) = recognize((
tag_no_case("X-"),
cut(take_while1(|c: u8| c.is_alphanum() || c == b'-')),
)))(input)?;
))
.parse(input)?;

Ok((input, x_name))
}
Expand All @@ -195,14 +197,14 @@ fn name<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
alt((iana_token, x_name))(input)
alt((iana_token, x_name)).parse(input)
}

fn param_name<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
alt((iana_token, x_name))(input)
alt((iana_token, x_name)).parse(input)
}

#[inline]
Expand Down Expand Up @@ -259,7 +261,8 @@ where
let (input, v) = fold_many0(value_char, Vec::new, |mut acc, item| {
acc.extend_from_slice(&item);
acc
})(input)?;
})
.parse(input)?;

Ok((input, v))
}
Expand All @@ -271,7 +274,8 @@ where
alt((
single(|b| matches!(b, b' ' | b'\t' | b'\x21'..=b'\x7E')).map(|c| vec![c]),
utf8_seq.map(|c| c.to_vec()),
))(input)
))
.parse(input)
}

fn value<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], Vec<u8>, E>
Expand All @@ -281,7 +285,8 @@ where
fold_many0(value_char, Vec::new, |mut acc, item| {
acc.extend_from_slice(&item);
acc
})(input)
})
.parse(input)
}

fn param<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], ParamValue<'a>, E>
Expand All @@ -292,7 +297,8 @@ where
param_name,
char('='),
cut(separated_list1(char(','), param_value)),
)(input)?;
)
.parse(input)?;

Ok((
input,
Expand All @@ -311,13 +317,14 @@ fn content_line<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], ContentLine<'a>, E>
where
E: ParseError<&'a [u8]> + From<Error<'a>>,
{
let (input, (property_name, params, _, value, _)) = tuple((
let (input, (property_name, params, _, value, _)) = (
name,
many0(tuple((char(';'), cut(param))).map(|(_, p)| p)),
many0((char(';'), cut(param))).map(|v| v.into_iter().map(|(_, p)| p).collect()),
char(':'),
cut(line_value),
crlf,
))(input)?;
)
.parse(input)?;

Ok((
input,
Expand Down
6 changes: 3 additions & 3 deletions src/parser/component/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use nom::branch::alt;
use nom::bytes::streaming::tag;
use nom::error::ParseError;
use nom::multi::many0;
use nom::sequence::tuple;
use nom::{IResult, Parser};

pub fn component_alarm<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], CalendarComponent<'a>, E>
Expand All @@ -18,7 +17,7 @@ where
+ nom::error::FromExternalError<&'a [u8], nom::Err<E>>
+ From<Error<'a>>,
{
let (input, (_, properties, _)) = tuple((
let (input, (_, properties, _)) = (
tag("BEGIN:VALARM\r\n"),
many0(alt((
alt((
Expand All @@ -35,7 +34,8 @@ where
prop_iana.map(ComponentProperty::IanaProperty),
))),
tag("END:VALARM\r\n"),
))(input)?;
)
.parse(input)?;

Ok((input, CalendarComponent::Alarm { properties }))
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/component/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use nom::bytes::streaming::tag;
use nom::combinator::cut;
use nom::error::ParseError;
use nom::multi::many0;
use nom::sequence::tuple;
use nom::{IResult, Parser};

pub fn component_event<'a, E>(input: &'a [u8]) -> IResult<&'a [u8], CalendarComponent<'a>, E>
Expand All @@ -25,7 +24,7 @@ where
+ nom::error::FromExternalError<&'a [u8], nom::Err<E>>
+ From<Error<'a>>,
{
let (input, (_, properties, alarms, _)) = tuple((
let (input, (_, properties, alarms, _)) = (
tag("BEGIN:VEVENT\r\n"),
cut(many0(alt((
alt((
Expand Down Expand Up @@ -67,7 +66,8 @@ where
)))),
many0(component_alarm),
tag("END:VEVENT\r\n"),
))(input)?;
)
.parse(input)?;

Ok((input, CalendarComponent::Event { properties, alarms }))
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/component/free_busy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use nom::bytes::streaming::tag;
use nom::combinator::cut;
use nom::error::ParseError;
use nom::multi::many0;
use nom::sequence::tuple;
use nom::IResult;
use nom::Parser;

Expand All @@ -21,7 +20,7 @@ where
+ nom::error::FromExternalError<&'a [u8], nom::Err<E>>
+ From<Error<'a>>,
{
let (input, (_, properties, _)) = tuple((
let (input, (_, properties, _)) = (
tag("BEGIN:VFREEBUSY\r\n"),
cut(many0(alt((
alt((
Expand All @@ -41,7 +40,8 @@ where
prop_iana.map(ComponentProperty::IanaProperty),
)))),
tag("END:VFREEBUSY\r\n"),
))(input)?;
)
.parse(input)?;

Ok((input, CalendarComponent::FreeBusy { properties }))
}
Expand Down
Loading