diff --git a/CHANGELOG.md b/CHANGELOG.md index dd2bf82..0669846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## [Unreleased] - ReleaseDate +### Changed + +- update [nom](https://crates.io/crates/nom) to v8 + ## [1.2.0] - 2024-03-21 ### Added diff --git a/Cargo.toml b/Cargo.toml index d1551a7..0868559 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,12 @@ exclude = [ ] [dependencies] -nom = "7" -nom_locate = "4.1" +nom = "8" +nom_locate = "5" serde = { version = "1.0", default-features = false } [dev-dependencies] -serde = { version = "1.0.194", features = ["derive"] } +serde = { version = "1.0", features = ["derive"] } [badges] maintenance = { status = "passively-maintained" } diff --git a/Justfile b/Justfile index 131d0e5..51d3da0 100644 --- a/Justfile +++ b/Justfile @@ -11,6 +11,10 @@ run *ARGS: test *ARGS: cargo test {{ARGS}} +check: + cargo clippy -- -D warnings + cargo test + doc: cargo doc --no-deps cargo readme > README.md @@ -18,8 +22,13 @@ doc: serve-doc port='8000': doc python3 -m http.server {{port}} --directory target/doc -release version execute='': - cargo release --sign --allow-branch master {{ if execute != "" { '-x' } else { '' } }} {{version}} +release version execute='': check build doc + git fetch --all + [ "$(git rev-parse master)" = "$(git rev-parse origin/master)" ] \ + || (echo "error: master and origin/master differ" >&2; exit 1) + git branch -f release master + git checkout release + cargo release --sign --allow-branch release {{ if execute != "" { '-x' } else { '' } }} {{version}} coverage *ARGS: RUSTFLAGS="-C instrument-coverage" cargo test --tests {{ARGS}} || true diff --git a/src/de.rs b/src/de.rs index b28e116..067ada1 100644 --- a/src/de.rs +++ b/src/de.rs @@ -81,7 +81,7 @@ where } } -impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { +impl<'de> serde::de::Deserializer<'de> for &mut Deserializer<'de> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result diff --git a/src/parser.rs b/src/parser.rs index 573d1ce..b471e31 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,8 +4,8 @@ use nom::character::complete::{char, digit1, none_of, not_line_ending, one_of}; use nom::combinator::{cut, eof, map, map_res, opt, recognize, value}; use nom::multi::many1_count; use nom::number::complete::double; -use nom::sequence::{delimited, preceded, terminated, tuple}; -use nom::{IResult, Slice}; +use nom::sequence::{delimited, preceded, terminated}; +use nom::{IResult, Input as _, Parser as _}; use nom_locate::LocatedSpan; pub(crate) type Span<'a> = LocatedSpan<&'a str>; @@ -35,23 +35,25 @@ fn whitespace(input: Span) -> IResult { } fn null(input: Span) -> IResult { - value((), tag("null"))(input) + value((), tag("null")).parse(input) } fn separator(input: Span) -> IResult { map(alt((tag(","), tag("\n"), tag("\r\n"))), |val: Span| { *val.fragment() - })(input) + }) + .parse(input) } fn bool(input: Span) -> IResult { - alt((value(true, tag("true")), value(false, tag("false"))))(input) + alt((value(true, tag("true")), value(false, tag("false")))).parse(input) } fn integer(input: Span) -> IResult { - map_res(recognize(tuple((opt(char('-')), digit1))), |val: Span| { + map_res(recognize((opt(char('-')), digit1)), |val: Span| { val.fragment().parse::() - })(input) + }) + .parse(input) } fn float(input: Span) -> IResult { @@ -61,14 +63,16 @@ fn float(input: Span) -> IResult { fn identifier(input: Span) -> IResult { map(recognize(many1_count(none_of("\" \t\n=:"))), |val: Span| { *val.fragment() - })(input) + }) + .parse(input) } fn literal_string(input: Span) -> IResult { map( delimited(tag("\"\"\""), take_until("\"\"\""), tag("\"\"\"")), |val: Span| *val.fragment(), - )(input) + ) + .parse(input) } fn string_content(input: Span) -> IResult { @@ -84,49 +88,51 @@ fn string_content(input: Span) -> IResult { } '\n' if !escaped => { let err = nom::error::Error { - input: input.slice(j..), + input: input.take_from(j), code: nom::error::ErrorKind::Char, }; return Err(nom::Err::Error(err)); } '"' if !escaped => { - return Ok((input.slice(j..), &buf[0..j])); + return Ok((input.take_from(j), &buf[0..j])); } _ => escaped = false, } } let err = nom::error::Error { - input: input.slice((i + 1)..), + input: input.take_from(i + 1), code: nom::error::ErrorKind::Char, }; Err(nom::Err::Failure(err)) } fn delimited_string(input: Span) -> IResult { - preceded(char('"'), cut(terminated(string_content, char('"'))))(input) + preceded(char('"'), cut(terminated(string_content, char('"')))).parse(input) } fn string(input: Span) -> IResult { - alt((identifier, literal_string, delimited_string))(input) + alt((identifier, literal_string, delimited_string)).parse(input) } fn line_comment(input: Span) -> IResult { map( preceded(tag("//"), alt((not_line_ending, eof))), |val: Span| *val.fragment(), - )(input) + ) + .parse(input) } fn block_comment(input: Span) -> IResult { map( delimited(tag("/*"), take_until("*/"), tag("*/")), |val: Span| *val.fragment(), - )(input) + ) + .parse(input) } fn comment(input: Span) -> IResult { - alt((line_comment, block_comment))(input) + alt((line_comment, block_comment)).parse(input) } fn optional(input: Span) -> IResult { @@ -135,7 +141,7 @@ fn optional(input: Span) -> IResult { let empty = value((), tag("")); let content = value((), many1_count(alt((whitespace, comment)))); - alt((content, empty))(input) + alt((content, empty)).parse(input) } pub(crate) fn parse_next_token(input: Span) -> IResult { @@ -159,45 +165,48 @@ pub(crate) fn parse_next_token(input: Span) -> IResult { map(float, Token::Float), map(string, |val| Token::String(val.to_string())), )), - )(input) + ) + .parse(input) } pub(crate) fn parse_trailing_characters(input: Span) -> IResult { - value((), optional)(input) + value((), optional).parse(input) } pub(crate) fn parse_null(input: Span) -> IResult { - preceded(optional, value(Token::Null, null))(input) + preceded(optional, value(Token::Null, null)).parse(input) } pub(crate) fn parse_separator(input: Span) -> IResult { preceded( opt(horizontal_whitespace), value(Token::Separator, separator), - )(input) + ) + .parse(input) } pub(crate) fn parse_bool(input: Span) -> IResult { - preceded(optional, map(bool, Token::Boolean))(input) + preceded(optional, map(bool, Token::Boolean)).parse(input) } pub(crate) fn parse_integer(input: Span) -> IResult { - preceded(optional, map(integer, Token::Integer))(input) + preceded(optional, map(integer, Token::Integer)).parse(input) } pub(crate) fn parse_float(input: Span) -> IResult { - preceded(optional, map(float, Token::Float))(input) + preceded(optional, map(float, Token::Float)).parse(input) } pub(crate) fn parse_identifier(input: Span) -> IResult { preceded( optional, map(identifier, |val| Token::String(val.to_string())), - )(input) + ) + .parse(input) } pub(crate) fn parse_string(input: Span) -> IResult { - preceded(optional, map(string, |val| Token::String(val.to_string())))(input) + preceded(optional, map(string, |val| Token::String(val.to_string()))).parse(input) } #[cfg(test)] diff --git a/src/ser.rs b/src/ser.rs index 7efd83b..b77799d 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -84,7 +84,7 @@ where } } -impl<'a, W> serde::ser::Serializer for &'a mut Serializer +impl serde::ser::Serializer for &mut Serializer where W: io::Write, { @@ -216,9 +216,9 @@ where self.serialize_unit() } - fn serialize_some(self, value: &T) -> Result + fn serialize_some(self, value: &T) -> Result where - T: serde::Serialize, + T: serde::Serialize + ?Sized, { self.ensure_top_level_struct()?; @@ -245,9 +245,9 @@ where self.serialize_str(variant) } - fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> Result + fn serialize_newtype_struct(self, _name: &'static str, value: &T) -> Result where - T: serde::Serialize, + T: serde::Serialize + ?Sized, { self.ensure_top_level_struct()?; @@ -255,7 +255,7 @@ where } // Serialize an externally tagged enum: `{ NAME = VALUE }`. - fn serialize_newtype_variant( + fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, @@ -263,7 +263,7 @@ where value: &T, ) -> Result where - T: serde::Serialize, + T: serde::Serialize + ?Sized, { self.ensure_top_level_struct()?; @@ -345,24 +345,24 @@ where Ok(self) } - fn collect_str(self, value: &T) -> Result + fn collect_str(self, value: &T) -> Result where - T: std::fmt::Display, + T: std::fmt::Display + ?Sized, { self.serialize_str(&value.to_string()) } } -impl<'a, W> serde::ser::SerializeSeq for &'a mut Serializer +impl serde::ser::SerializeSeq for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_element(&mut self, value: &T) -> Result<()> + fn serialize_element(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; value.serialize(&mut **self)?; @@ -376,16 +376,16 @@ where } } -impl<'a, W> serde::ser::SerializeTuple for &'a mut Serializer +impl serde::ser::SerializeTuple for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_element(&mut self, value: &T) -> Result<()> + fn serialize_element(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; value.serialize(&mut **self)?; @@ -399,16 +399,16 @@ where } } -impl<'a, W> serde::ser::SerializeTupleStruct for &'a mut Serializer +impl serde::ser::SerializeTupleStruct for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_field(&mut self, value: &T) -> Result<()> + fn serialize_field(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; value.serialize(&mut **self)?; @@ -422,16 +422,16 @@ where } } -impl<'a, W> serde::ser::SerializeTupleVariant for &'a mut Serializer +impl serde::ser::SerializeTupleVariant for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_field(&mut self, value: &T) -> Result<()> + fn serialize_field(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; value.serialize(&mut **self)?; @@ -454,24 +454,24 @@ where } } -impl<'a, W> serde::ser::SerializeMap for &'a mut Serializer +impl serde::ser::SerializeMap for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_key(&mut self, key: &T) -> Result<()> + fn serialize_key(&mut self, key: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; key.serialize(&mut **self) } - fn serialize_value(&mut self, value: &T) -> Result<()> + fn serialize_value(&mut self, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { // It doesn't make a difference where the `=` is added. But doing it here // means `serialize_key` is only a call to a different function, which should @@ -491,16 +491,16 @@ where } } -impl<'a, W> serde::ser::SerializeStruct for &'a mut Serializer +impl serde::ser::SerializeStruct for &mut Serializer where W: io::Write, { type Ok = (); type Error = Error; - fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> + fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; key.serialize(&mut **self)?; @@ -521,16 +521,16 @@ where } } -impl<'a, W> serde::ser::SerializeStructVariant for &'a mut Serializer +impl serde::ser::SerializeStructVariant for &mut Serializer where W: std::io::Write, { type Ok = (); type Error = Error; - fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> + fn serialize_field(&mut self, key: &'static str, value: &T) -> Result<()> where - T: Serialize, + T: Serialize + ?Sized, { self.add_indent()?; key.serialize(&mut **self)?;