diff options
author | Sam Scott <sam@osohq.com> | 2020-12-06 23:43:32 -0500 |
---|---|---|
committer | Sam Scott <sam@osohq.com> | 2020-12-06 23:43:48 -0500 |
commit | b7b2520107ecfca77520151c6dc3002442bf0f0b (patch) | |
tree | be49c18915bac69746fcbc2bfb3641b3994a83d1 | |
parent | 3f77d51edcc0575df5204152cb0dd960604244dc (diff) |
Serialize bytes as percent-encoded data.
Fixes #40.
Thanks to @faulesocke for reporting.
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/error.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 44 | ||||
-rw-r--r-- | src/ser.rs | 17 | ||||
-rw-r--r-- | tests/test_deserialize.rs | 3 | ||||
-rw-r--r-- | tests/test_serialize.rs | 24 |
6 files changed, 32 insertions, 61 deletions
@@ -19,7 +19,6 @@ repository = "samscott89/serde_qs" [dependencies] actix-web = { version = "3.3", optional = true } actix-web2 = { package = "actix-web", version = "2.0.0", optional = true } -data-encoding = "2.3.1" futures = { version = "0.3.8", optional = true } percent-encoding = "2.1.0" serde = "1.0.118" diff --git a/src/error.rs b/src/error.rs index 6aac25f..b7c2042 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,10 +21,6 @@ pub enum Error { #[error("unsupported type for serialization")] Unsupported, - /// Error decoding `BASE64URL` data - #[error(transparent)] - Decoding(#[from] data_encoding::DecodeError), - /// Error proessing UTF-8 for a `String` #[error(transparent)] FromUtf8(#[from] string::FromUtf8Error), @@ -160,50 +160,6 @@ //! //! Support for `actix-web 2.0.0` is available via the `actix2` feature. -#![allow()] -#![deny( - arithmetic_overflow, - const_err, - dead_code, - deprecated, - improper_ctypes, - missing_docs, - mutable_transmutes, - no_mangle_const_items, - non_camel_case_types, - non_shorthand_field_patterns, - non_snake_case, - non_upper_case_globals, - overflowing_literals, - path_statements, - stable_features, - trivial_casts, - trivial_numeric_casts, - unconditional_recursion, - unknown_crate_types, - unknown_lints, - unreachable_code, - unsafe_code, - unstable_features, - unused_allocation, - unused_assignments, - unused_attributes, - unused_comparisons, - unused_extern_crates, - unused_features, - unused_imports, - unused_import_braces, - unused_must_use, - unused_mut, - unused_parens, - unused_qualifications, - unused_results, - unused_unsafe, - unused_variables, - variant_size_differences, - while_true -)] - #[macro_use] extern crate serde; @@ -1,6 +1,5 @@ //! Serialization support for querystrings. -use data_encoding::BASE64URL_NOPAD as BASE64; use percent_encoding::{percent_encode, AsciiSet, NON_ALPHANUMERIC}; use serde::ser; @@ -132,7 +131,7 @@ impl<'a, W: 'a + Write> QsSerializer<'a, W> { self.key = Some(key) } - fn write_value(&mut self, value: &str) -> Result<()> { + fn write_value(&mut self, value: &[u8]) -> Result<()> { if let Some(ref key) = self.key { write!( self.writer, @@ -144,7 +143,7 @@ impl<'a, W: 'a + Write> QsSerializer<'a, W> { "&" }, key, - percent_encode(value.as_bytes(), QS_ENCODE_SET) + percent_encode(value, QS_ENCODE_SET) .map(replace_space) .collect::<String>() ) @@ -176,7 +175,7 @@ macro_rules! serialize_as_string { (Qs $($ty:ty => $meth:ident,)*) => { $( fn $meth(self, v: $ty) -> Result<Self::Ok> { - self.write_value(&v.to_string()) + self.write_value(&v.to_string().as_bytes()) } )* }; @@ -218,16 +217,16 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { } fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok> { - self.write_value(&BASE64.encode(value)) + self.write_value(&value) } fn serialize_unit(self) -> Result<Self::Ok> { - self.write_value("") + self.write_value(&[]) } /// Returns an error. fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> { - self.write_value(name) + self.write_value(name.as_bytes()) } /// Returns an error. @@ -237,7 +236,7 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { _variant_index: u32, variant: &'static str, ) -> Result<Self::Ok> { - self.write_value(variant) + self.write_value(variant.as_bytes()) } /// Returns an error. @@ -510,7 +509,7 @@ impl ser::Serializer for StringSerializer { } fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok> { - Ok(BASE64.encode(value)) + Ok(String::from_utf8_lossy(value).to_string()) } /// Returns an error. diff --git a/tests/test_deserialize.rs b/tests/test_deserialize.rs index 68395d5..a620828 100644 --- a/tests/test_deserialize.rs +++ b/tests/test_deserialize.rs @@ -639,9 +639,6 @@ fn deserialize_plus() { } #[test] -fn deserialize_vec_of_structs() {} - -#[test] fn deserialize_map_with_unit_enum_keys() { #[derive(Deserialize, Eq, PartialEq, Hash)] enum Operator { diff --git a/tests/test_serialize.rs b/tests/test_serialize.rs index 8aecfe3..c3c63fd 100644 --- a/tests/test_serialize.rs +++ b/tests/test_serialize.rs @@ -156,3 +156,27 @@ fn serialize_map_with_unit_enum_keys() { assert!(query == "point[Lt]=321&point[Gt]=123" || query == "point[Gt]=123&point[Lt]=321"); } + +#[test] +fn serialize_bytes() { + struct Bytes(&'static [u8]); + + #[derive(Serialize)] + struct Query { + bytes: Bytes, + } + + + impl serde::Serialize for Bytes + { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: serde::Serializer, + { + serializer.serialize_bytes(self.0) + } + } + let bytes = Bytes(b"hello, world!"); + let s = qs::to_string(&Query { bytes }).unwrap(); + assert_eq!(s, "bytes=hello%2C+world%21"); +}
\ No newline at end of file |