From bd88c5c7cab0639439708ddc309afc7d43e490a5 Mon Sep 17 00:00:00 2001 From: peizhe <472708703@qq.com> Date: Fri, 14 Apr 2023 10:07:10 +0800 Subject: [PATCH] upgrade serde_urlencoded third party rust to version 7.1.0 Signed-off-by: peizhe <472708703@qq.com> --- .travis.yml | 8 ----- Cargo.toml | 16 +++++---- README.md | 5 ++- rustfmt.toml | 2 +- src/de.rs | 9 ++--- src/lib.rs | 11 ++---- src/ser/key.rs | 6 ++-- src/ser/mod.rs | 73 ++++++++++++++++++++++++--------------- src/ser/pair.rs | 32 +++++++++-------- src/ser/part.rs | 30 +++++++++------- src/ser/value.rs | 18 +++++----- tests/test_deserialize.rs | 9 +++-- tests/test_serialize.rs | 27 +++++++++++++-- 13 files changed, 143 insertions(+), 103 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 088c2e4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: rust -rust: - - nightly - - beta - - stable -branches: - except: - - staging.tmp diff --git a/Cargo.toml b/Cargo.toml index 383d568..8b1740b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,15 @@ [package] name = "serde_urlencoded" -version = "0.6.1" +version = "0.7.1" # bump in documentation link and in README on update authors = ["Anthony Ramine "] license = "MIT/Apache-2.0" repository = "https://github.com/nox/serde_urlencoded" -documentation = "https://docs.rs/serde_urlencoded" +documentation = "https://docs.rs/serde_urlencoded/0.7.1/serde_urlencoded/" description = "`x-www-form-urlencoded` meets Serde" categories = ["encoding", "web-programming"] keywords = ["serde", "serialization", "urlencoded"] +exclude = ["/.travis.yml", "/bors.toml"] +edition = "2018" [badges] travis-ci = {repository = "nox/serde_urlencoded"} @@ -16,10 +18,10 @@ travis-ci = {repository = "nox/serde_urlencoded"} test = false [dependencies] -dtoa = "0.4.0" -itoa = "0.4.0" -serde = "1.0.0" -url = "2.0.0" +form_urlencoded = "1" +itoa = "1" +ryu = "1" +serde = "1.0.69" [dev-dependencies] -serde_derive = "1.0" +serde_derive = "1" diff --git a/README.md b/README.md index e55a86c..92cacb6 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,13 @@ This crate works with Cargo and can be found on ```toml [dependencies] -serde_urlencoded = "0.5.1" +serde_urlencoded = "0.7" ``` +The documentation is available on [docs.rs]. + [crates.io]: https://crates.io/crates/serde_urlencoded +[docs.rs]: https://docs.rs/serde_urlencoded/0.7.1/serde_urlencoded/ ## Getting help diff --git a/rustfmt.toml b/rustfmt.toml index 2b7608b..f80ce4e 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,4 @@ -match_block_trailing_comma = true +match_block_trailing_comma = false max_width = 80 newline_style = "Unix" reorder_imports = true diff --git a/src/de.rs b/src/de.rs index 3558f3e..d906eaa 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1,17 +1,18 @@ //! Deserialization support for the `application/x-www-form-urlencoded` format. +use form_urlencoded::parse; +use form_urlencoded::Parse as UrlEncodedParse; use serde::de::value::MapDeserializer; use serde::de::Error as de_Error; use serde::de::{self, IntoDeserializer}; +use serde::forward_to_deserialize_any; use std::borrow::Cow; use std::io::Read; -use url::form_urlencoded::parse; -use url::form_urlencoded::Parse as UrlEncodedParse; #[doc(inline)] pub use serde::de::value::Error; -/// Deserializes a `application/x-wwww-url-encoded` value from a `&[u8]`. +/// Deserializes a `application/x-www-form-urlencoded` value from a `&[u8]`. /// /// ``` /// let meal = vec![ @@ -33,7 +34,7 @@ where T::deserialize(Deserializer::new(parse(input))) } -/// Deserializes a `application/x-wwww-url-encoded` value from a `&str`. +/// Deserializes a `application/x-www-form-urlencoded` value from a `&str`. /// /// ``` /// let meal = vec![ diff --git a/src/lib.rs b/src/lib.rs index 776ae54..b7ccc78 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,12 @@ //! `x-www-form-urlencoded` meets Serde #![warn(unused_extern_crates)] - -extern crate dtoa; -extern crate itoa; -#[macro_use] -extern crate serde; -extern crate url; +#![forbid(unsafe_code)] pub mod de; pub mod ser; #[doc(inline)] -pub use de::{from_bytes, from_reader, from_str, Deserializer}; +pub use crate::de::{from_bytes, from_reader, from_str, Deserializer}; #[doc(inline)] -pub use ser::{to_string, Serializer}; +pub use crate::ser::{to_string, Serializer}; diff --git a/src/ser/key.rs b/src/ser/key.rs index 2a2e63a..8128a64 100644 --- a/src/ser/key.rs +++ b/src/ser/key.rs @@ -1,5 +1,5 @@ -use ser::part::Sink; -use ser::Error; +use crate::ser::part::Sink; +use crate::ser::Error; use serde::Serialize; use std::borrow::Cow; use std::ops::Deref; @@ -38,7 +38,7 @@ where End: for<'key> FnOnce(Key<'key>) -> Result, { pub fn new(end: End) -> Self { - KeySink { end: end } + KeySink { end } } } diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 598b3a4..d75b902 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -5,15 +5,15 @@ mod pair; mod part; mod value; +use form_urlencoded::Serializer as UrlEncodedSerializer; +use form_urlencoded::Target as UrlEncodedTarget; use serde::ser; use std::borrow::Cow; use std::error; use std::fmt; use std::str; -use url::form_urlencoded::Serializer as UrlEncodedSerializer; -use url::form_urlencoded::Target as UrlEncodedTarget; -/// Serializes a value into a `application/x-wwww-url-encoded` `String` buffer. +/// Serializes a value into a `application/x-www-form-urlencoded` `String` buffer. /// /// ``` /// let meal = &[ @@ -42,16 +42,18 @@ pub fn to_string(input: T) -> Result { /// unit structs and unit variants. /// /// * Newtype structs defer to their inner values. -pub struct Serializer<'input, 'output, Target: 'output + UrlEncodedTarget> { +pub struct Serializer<'input, 'output, Target: UrlEncodedTarget> { urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, } -impl<'input, 'output, Target: 'output + UrlEncodedTarget> Serializer<'input, 'output, Target> { +impl<'input, 'output, Target: 'output + UrlEncodedTarget> + Serializer<'input, 'output, Target> +{ /// Returns a new `Serializer`. - pub fn new(urlencoder: &'output mut UrlEncodedSerializer<'input, Target>) -> Self { - Serializer { - urlencoder: urlencoder, - } + pub fn new( + urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, + ) -> Self { + Serializer { urlencoder } } } @@ -63,7 +65,7 @@ pub enum Error { } impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { Error::Custom(ref msg) => msg.fmt(f), Error::Utf8(ref err) => write!(f, "invalid UTF-8: {}", err), @@ -80,7 +82,15 @@ impl error::Error for Error { } /// The lower-level cause of this error, in the case of a `Utf8` error. - fn cause(&self) -> Option<&error::Error> { + fn cause(&self) -> Option<&dyn error::Error> { + match *self { + Error::Custom(_) => None, + Error::Utf8(ref err) => Some(err), + } + } + + /// The lower-level source of this error, in the case of a `Utf8` error. + fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { Error::Custom(_) => None, Error::Utf8(ref err) => Some(err), @@ -95,50 +105,51 @@ impl ser::Error for Error { } /// Sequence serializer. -pub struct SeqSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> { +pub struct SeqSerializer<'input, 'output, Target: UrlEncodedTarget> { urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, } /// Tuple serializer. /// /// Mostly used for arrays. -pub struct TupleSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> { +pub struct TupleSerializer<'input, 'output, Target: UrlEncodedTarget> { urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, } /// Tuple struct serializer. /// /// Never instantiated, tuple structs are not supported. -pub struct TupleStructSerializer<'input, 'output, T: 'output + UrlEncodedTarget> { +pub struct TupleStructSerializer<'input, 'output, T: UrlEncodedTarget> { inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>, } /// Tuple variant serializer. /// /// Never instantiated, tuple variants are not supported. -pub struct TupleVariantSerializer<'input, 'output, T: 'output + UrlEncodedTarget> { +pub struct TupleVariantSerializer<'input, 'output, T: UrlEncodedTarget> { inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>, } /// Map serializer. -pub struct MapSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> { +pub struct MapSerializer<'input, 'output, Target: UrlEncodedTarget> { urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, key: Option>, } /// Struct serializer. -pub struct StructSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> { +pub struct StructSerializer<'input, 'output, Target: UrlEncodedTarget> { urlencoder: &'output mut UrlEncodedSerializer<'input, Target>, } /// Struct variant serializer. /// /// Never instantiated, struct variants are not supported. -pub struct StructVariantSerializer<'input, 'output, T: 'output + UrlEncodedTarget> { +pub struct StructVariantSerializer<'input, 'output, T: UrlEncodedTarget> { inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>, } -impl<'input, 'output, Target> ser::Serializer for Serializer<'input, 'output, Target> +impl<'input, 'output, Target> ser::Serializer + for Serializer<'input, 'output, Target> where Target: 'output + UrlEncodedTarget, { @@ -147,10 +158,12 @@ where type SerializeSeq = SeqSerializer<'input, 'output, Target>; type SerializeTuple = TupleSerializer<'input, 'output, Target>; type SerializeTupleStruct = TupleStructSerializer<'input, 'output, Target>; - type SerializeTupleVariant = TupleVariantSerializer<'input, 'output, Target>; + type SerializeTupleVariant = + TupleVariantSerializer<'input, 'output, Target>; type SerializeMap = MapSerializer<'input, 'output, Target>; type SerializeStruct = StructSerializer<'input, 'output, Target>; - type SerializeStructVariant = StructVariantSerializer<'input, 'output, Target>; + type SerializeStructVariant = + StructVariantSerializer<'input, 'output, Target>; /// Returns an error. fn serialize_bool(self, _v: bool) -> Result { @@ -222,9 +235,9 @@ where Err(Error::top_level()) } - /// Returns an error. + /// Returns `Ok`. fn serialize_unit(self) -> Result { - Err(Error::top_level()) + Ok(self.urlencoder) } /// Returns `Ok`. @@ -352,7 +365,8 @@ where } } -impl<'input, 'output, Target> ser::SerializeSeq for SeqSerializer<'input, 'output, Target> +impl<'input, 'output, Target> ser::SerializeSeq + for SeqSerializer<'input, 'output, Target> where Target: 'output + UrlEncodedTarget, { @@ -371,7 +385,8 @@ where } } -impl<'input, 'output, Target> ser::SerializeTuple for TupleSerializer<'input, 'output, Target> +impl<'input, 'output, Target> ser::SerializeTuple + for TupleSerializer<'input, 'output, Target> where Target: 'output + UrlEncodedTarget, { @@ -430,7 +445,8 @@ where } } -impl<'input, 'output, Target> ser::SerializeMap for MapSerializer<'input, 'output, Target> +impl<'input, 'output, Target> ser::SerializeMap + for MapSerializer<'input, 'output, Target> where Target: 'output + UrlEncodedTarget, { @@ -470,7 +486,7 @@ where value: &T, ) -> Result<(), Error> { { - let key = self.key.as_ref().ok_or_else(|| Error::no_key())?; + let key = self.key.as_ref().ok_or_else(Error::no_key)?; let value_sink = value::ValueSink::new(self.urlencoder, &key); value.serialize(part::PartSerializer::new(value_sink))?; } @@ -483,7 +499,8 @@ where } } -impl<'input, 'output, Target> ser::SerializeStruct for StructSerializer<'input, 'output, Target> +impl<'input, 'output, Target> ser::SerializeStruct + for StructSerializer<'input, 'output, Target> where Target: 'output + UrlEncodedTarget, { diff --git a/src/ser/pair.rs b/src/ser/pair.rs index e7235e4..429ce4b 100644 --- a/src/ser/pair.rs +++ b/src/ser/pair.rs @@ -1,14 +1,14 @@ -use ser::key::KeySink; -use ser::part::PartSerializer; -use ser::value::ValueSink; -use ser::Error; +use crate::ser::key::KeySink; +use crate::ser::part::PartSerializer; +use crate::ser::value::ValueSink; +use crate::ser::Error; +use form_urlencoded::Serializer as UrlEncodedSerializer; +use form_urlencoded::Target as UrlEncodedTarget; use serde::ser; use std::borrow::Cow; use std::mem; -use url::form_urlencoded::Serializer as UrlEncodedSerializer; -use url::form_urlencoded::Target as UrlEncodedTarget; -pub struct PairSerializer<'input, 'target, Target: 'target + UrlEncodedTarget> { +pub struct PairSerializer<'input, 'target, Target: UrlEncodedTarget> { urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, state: PairState, } @@ -17,15 +17,18 @@ impl<'input, 'target, Target> PairSerializer<'input, 'target, Target> where Target: 'target + UrlEncodedTarget, { - pub fn new(urlencoder: &'target mut UrlEncodedSerializer<'input, Target>) -> Self { + pub fn new( + urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, + ) -> Self { PairSerializer { - urlencoder: urlencoder, + urlencoder, state: PairState::WaitingForKey, } } } -impl<'input, 'target, Target> ser::Serializer for PairSerializer<'input, 'target, Target> +impl<'input, 'target, Target> ser::Serializer + for PairSerializer<'input, 'target, Target> where Target: 'target + UrlEncodedTarget, { @@ -200,7 +203,8 @@ where } } -impl<'input, 'target, Target> ser::SerializeTuple for PairSerializer<'input, 'target, Target> +impl<'input, 'target, Target> ser::SerializeTuple + for PairSerializer<'input, 'target, Target> where Target: 'target + UrlEncodedTarget, { @@ -219,7 +223,7 @@ where key: value.serialize(key_serializer)?, }; Ok(()) - }, + } PairState::WaitingForValue { key } => { let result = { let value_sink = ValueSink::new(self.urlencoder, &key); @@ -229,10 +233,10 @@ where if result.is_ok() { self.state = PairState::Done; } else { - self.state = PairState::WaitingForValue { key: key }; + self.state = PairState::WaitingForValue { key }; } result - }, + } PairState::Done => Err(Error::done()), } } diff --git a/src/ser/part.rs b/src/ser/part.rs index f72846c..1deffa5 100644 --- a/src/ser/part.rs +++ b/src/ser/part.rs @@ -1,6 +1,4 @@ -use dtoa; -use itoa; -use ser::Error; +use crate::ser::Error; use serde::ser; use std::str; @@ -10,7 +8,7 @@ pub struct PartSerializer { impl PartSerializer { pub fn new(sink: S) -> Self { - PartSerializer { sink: sink } + PartSerializer { sink } } } @@ -82,6 +80,14 @@ impl ser::Serializer for PartSerializer { self.serialize_integer(v) } + fn serialize_u128(self, v: u128) -> Result { + self.serialize_integer(v) + } + + fn serialize_i128(self, v: i128) -> Result { + self.serialize_integer(v) + } + fn serialize_f32(self, v: f32) -> Result { self.serialize_floating(v) } @@ -110,7 +116,7 @@ impl ser::Serializer for PartSerializer { } fn serialize_unit_struct(self, name: &'static str) -> Result { - self.sink.serialize_static_str(name.into()) + self.sink.serialize_static_str(name) } fn serialize_unit_variant( @@ -119,7 +125,7 @@ impl ser::Serializer for PartSerializer { _variant_index: u32, variant: &'static str, ) -> Result { - self.sink.serialize_static_str(variant.into()) + self.sink.serialize_static_str(variant) } fn serialize_newtype_struct( @@ -214,19 +220,17 @@ impl PartSerializer { where I: itoa::Integer, { - let mut buf = [b'\0'; 20]; - let len = itoa::write(&mut buf[..], value).unwrap(); - let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; + let mut buf = itoa::Buffer::new(); + let part = buf.format(value); ser::Serializer::serialize_str(self, part) } fn serialize_floating(self, value: F) -> Result where - F: dtoa::Floating, + F: ryu::Float, { - let mut buf = [b'\0'; 24]; - let len = dtoa::write(&mut buf[..], value).unwrap(); - let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) }; + let mut buf = ryu::Buffer::new(); + let part = buf.format(value); ser::Serializer::serialize_str(self, part) } } diff --git a/src/ser/value.rs b/src/ser/value.rs index fc12076..e8823ce 100644 --- a/src/ser/value.rs +++ b/src/ser/value.rs @@ -1,13 +1,13 @@ -use ser::part::{PartSerializer, Sink}; -use ser::Error; +use crate::ser::part::{PartSerializer, Sink}; +use crate::ser::Error; +use form_urlencoded::Serializer as UrlEncodedSerializer; +use form_urlencoded::Target as UrlEncodedTarget; use serde::ser::Serialize; use std::str; -use url::form_urlencoded::Serializer as UrlEncodedSerializer; -use url::form_urlencoded::Target as UrlEncodedTarget; pub struct ValueSink<'input, 'key, 'target, Target> where - Target: 'target + UrlEncodedTarget, + Target: UrlEncodedTarget, { urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, key: &'key str, @@ -21,14 +21,12 @@ where urlencoder: &'target mut UrlEncodedSerializer<'input, Target>, key: &'key str, ) -> Self { - ValueSink { - urlencoder: urlencoder, - key: key, - } + ValueSink { urlencoder, key } } } -impl<'input, 'key, 'target, Target> Sink for ValueSink<'input, 'key, 'target, Target> +impl<'input, 'key, 'target, Target> Sink + for ValueSink<'input, 'key, 'target, Target> where Target: 'target + UrlEncodedTarget, { diff --git a/tests/test_deserialize.rs b/tests/test_deserialize.rs index 6910599..00700d3 100644 --- a/tests/test_deserialize.rs +++ b/tests/test_deserialize.rs @@ -1,6 +1,4 @@ -extern crate serde_urlencoded; -#[macro_use] -extern crate serde_derive; +use serde_derive::Deserialize; #[derive(Deserialize, Debug, PartialEq)] struct NewType(T); @@ -83,3 +81,8 @@ fn deserialize_unit_enum() { Ok(result) ); } + +#[test] +fn deserialize_unit_type() { + assert_eq!(serde_urlencoded::from_str(""), Ok(())); +} diff --git a/tests/test_serialize.rs b/tests/test_serialize.rs index b0276d2..abb4907 100644 --- a/tests/test_serialize.rs +++ b/tests/test_serialize.rs @@ -1,6 +1,4 @@ -extern crate serde_urlencoded; -#[macro_use] -extern crate serde_derive; +use serde_derive::Serialize; #[derive(Serialize)] struct NewType(T); @@ -14,6 +12,24 @@ fn serialize_newtype_i32() { ); } +#[test] +fn serialize_newtype_u128() { + let params = &[("field", Some(NewType(u128::MAX)))]; + assert_eq!( + serde_urlencoded::to_string(params), + Ok(format!("field={}", u128::MAX)) + ); +} + +#[test] +fn serialize_newtype_i128() { + let params = &[("field", Some(NewType(i128::MIN)))]; + assert_eq!( + serde_urlencoded::to_string(params), + Ok(format!("field={}", i128::MIN)) + ); +} + #[test] fn serialize_option_map_int() { let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))]; @@ -81,3 +97,8 @@ struct Unit; fn serialize_unit_struct() { assert_eq!(serde_urlencoded::to_string(Unit), Ok("".to_owned())); } + +#[test] +fn serialize_unit_type() { + assert_eq!(serde_urlencoded::to_string(()), Ok("".to_owned())); +}