Add 'urlencoded/' from commit '93a7283991aa9c9b1087803c274953d3072c3dc0'

git-subtree-dir: urlencoded
git-subtree-mainline: 3e4cd7abd4fa3f87c7de3952a10c65928d93979d
git-subtree-split: 93a7283991aa9c9b1087803c274953d3072c3dc0
This commit is contained in:
Jonas Platte 2020-04-24 09:43:01 +02:00
commit afb39536eb
18 changed files with 2287 additions and 0 deletions

2
urlencoded/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target
Cargo.lock

8
urlencoded/.travis.yml Normal file
View File

@ -0,0 +1,8 @@
language: rust
rust:
- nightly
- beta
- stable
branches:
except:
- staging.tmp

24
urlencoded/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "serde_urlencoded"
version = "0.6.1"
authors = ["Anthony Ramine <n.oxyde@gmail.com>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/nox/serde_urlencoded"
documentation = "https://docs.rs/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"}
[lib]
test = false
[dependencies]
dtoa = "0.4.5"
itoa = "0.4.5"
serde = { version = "1.0.106", features = ["derive"] }
url = "2.1.1"

176
urlencoded/LICENSE-APACHE Normal file
View File

@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

25
urlencoded/LICENSE-MIT Normal file
View File

@ -0,0 +1,25 @@
Copyright (c) 2016 Anthony Ramine
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

50
urlencoded/README.md Normal file
View File

@ -0,0 +1,50 @@
`x-www-form-urlencoded` meets Serde
===================================
This crate is a Rust library for serialising to and deserialising from
the [`application/x-www-form-urlencoded`][urlencoded] format. It is built
upon [Serde], a high performance generic serialization framework and [rust-url],
a URL parser for Rust.
[rust-url]: https://github.com/servo/rust-url
[Serde]: https://github.com/serde-rs/serde
[urlencoded]: https://url.spec.whatwg.org/#application/x-www-form-urlencoded
Installation
============
This crate works with Cargo and can be found on
[crates.io] with a `Cargo.toml` like:
```toml
[dependencies]
serde_urlencoded = "0.5.1"
```
[crates.io]: https://crates.io/crates/serde_urlencoded
## Getting help
Serde developers live in the #serde channel on
[`irc.mozilla.org`](https://wiki.mozilla.org/IRC) and most rust-url developers
live in the #servo one. The #rust channel is also a good resource with generally
faster response time but less specific knowledge about Serde, rust-url or this
crate. If IRC is not your thing, we are happy to respond to [GitHub
issues](https://github.com/nox/serde_urlencoded/issues/new) as well.
## License
serde_urlencoded is licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in serde_urlencoded by you, as defined in the Apache-2.0 license,
shall be dual licensed as above, without any additional terms or conditions.

1
urlencoded/bors.toml Normal file
View File

@ -0,0 +1 @@
status = ["continuous-integration/travis-ci/push"]

5
urlencoded/rustfmt.toml Normal file
View File

@ -0,0 +1,5 @@
match_block_trailing_comma = true
max_width = 80
newline_style = "Unix"
reorder_imports = true
use_try_shorthand = true

358
urlencoded/src/de.rs Normal file
View File

@ -0,0 +1,358 @@
//! Deserialization support for the `application/x-www-form-urlencoded` format.
use std::{borrow::Cow, io::Read};
use serde::de::{
self,
value::{MapDeserializer, SeqDeserializer},
Error as de_Error, IntoDeserializer,
};
use url::form_urlencoded::{parse, Parse as UrlEncodedParse};
#[doc(inline)]
pub use serde::de::value::Error;
/// Deserializes a `application/x-www-form-urlencoded` value from a `&[u8]`.
///
/// ```
/// let meal = vec![
/// ("bread".to_owned(), "baguette".to_owned()),
/// ("cheese".to_owned(), "comté".to_owned()),
/// ("meat".to_owned(), "ham".to_owned()),
/// ("fat".to_owned(), "butter".to_owned()),
/// ];
///
/// assert_eq!(
/// serde_urlencoded::from_bytes::<Vec<(String, String)>>(
/// b"bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter"),
/// Ok(meal));
/// ```
pub fn from_bytes<'de, T>(input: &'de [u8]) -> Result<T, Error>
where
T: de::Deserialize<'de>,
{
T::deserialize(Deserializer::new(parse(input)))
}
/// Deserializes a `application/x-www-form-urlencoded` value from a `&str`.
///
/// ```
/// let meal = vec![
/// ("bread".to_owned(), "baguette".to_owned()),
/// ("cheese".to_owned(), "comté".to_owned()),
/// ("meat".to_owned(), "ham".to_owned()),
/// ("fat".to_owned(), "butter".to_owned()),
/// ];
///
/// assert_eq!(
/// serde_urlencoded::from_str::<Vec<(String, String)>>(
/// "bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter"),
/// Ok(meal));
/// ```
pub fn from_str<'de, T>(input: &'de str) -> Result<T, Error>
where
T: de::Deserialize<'de>,
{
from_bytes(input.as_bytes())
}
/// Convenience function that reads all bytes from `reader` and deserializes
/// them with `from_bytes`.
pub fn from_reader<T, R>(mut reader: R) -> Result<T, Error>
where
T: de::DeserializeOwned,
R: Read,
{
let mut buf = vec![];
reader.read_to_end(&mut buf).map_err(|e| {
de::Error::custom(format_args!("could not read input: {}", e))
})?;
from_bytes(&buf)
}
/// A deserializer for the Matrix query string format.
///
/// * Supported top-level outputs are structs, maps and sequences,
/// with or without a given length.
///
/// * Main `deserialize` methods defers to `deserialize_map`.
///
/// * Everything else but `deserialize_seq` and `deserialize_seq_fixed_size`
/// defers to `deserialize`.
pub struct Deserializer<'de> {
parser: UrlEncodedParse<'de>,
}
impl<'de> Deserializer<'de> {
pub fn new(parser: UrlEncodedParse<'de>) -> Self {
Self { parser }
}
}
impl<'de> de::Deserializer<'de> for Deserializer<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
self.deserialize_map(visitor)
}
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_map(MapDeserializer::new(PartIterator(self.parser)))
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_seq(MapDeserializer::new(PartIterator(self.parser)))
}
fn deserialize_struct<V>(
self,
_name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
let pairs = self.parser.collect::<Vec<_>>();
let mut map = std::collections::VecDeque::new();
for field in fields {
let values = pairs
.iter()
.filter(|(f, _)| f == field)
.map(|(_f, v)| v.clone())
.collect::<Vec<_>>();
map.push_back((*field, values));
}
let parts = fields
.into_iter()
.map(|f| Part(Cow::Borrowed(f), None))
.zip(PartIterator(self.parser).into_iter().map(|(_, mut v)| {
if let Some((_, val)) = map.pop_front() {
v.1 = Some(val);
}
v
}))
.map(|(field, value)| (field, value));
visitor.visit_map(MapDeserializer::new(parts))
}
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
MapDeserializer::new(PartIterator(self.parser)).end()?;
visitor.visit_unit()
}
serde::forward_to_deserialize_any! {
bool
u8
u16
u32
u64
i8
i16
i32
i64
f32
f64
char
str
string
option
bytes
byte_buf
unit_struct
newtype_struct
tuple_struct
identifier
tuple
enum
ignored_any
}
}
struct PartIterator<'de>(UrlEncodedParse<'de>);
impl<'de> Iterator for PartIterator<'de> {
type Item = (Part<'de>, Part<'de>);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(k, v)| (Part(k, None), Part(v, None)))
}
}
struct Part<'de>(Cow<'de, str>, Option<Vec<Cow<'de, str>>>);
impl<'de> IntoDeserializer<'de> for Part<'de> {
type Deserializer = Self;
fn into_deserializer(self) -> Self::Deserializer {
self
}
}
macro_rules! forward_parsed_value {
($($ty:ident => $method:ident,)*) => {
$(
fn $method<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: de::Visitor<'de>
{
match self.0.parse::<$ty>() {
Ok(val) => val.into_deserializer().$method(visitor),
Err(e) => Err(de::Error::custom(e))
}
}
)*
}
}
impl<'de> de::Deserializer<'de> for Part<'de> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
match self.0 {
Cow::Borrowed(value) => visitor.visit_borrowed_str(value),
Cow::Owned(value) => visitor.visit_string(value),
}
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_some(self)
}
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_enum(ValueEnumAccess(self.0))
}
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
visitor.visit_newtype_struct(self)
}
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
let iter = self.1.ok_or(Error::custom("expected sequence"))?;
visitor.visit_seq(SeqDeserializer::new(
iter.into_iter().map(|v| Part(v, None)),
))
}
serde::forward_to_deserialize_any! {
char
str
string
unit
bytes
byte_buf
unit_struct
tuple_struct
struct
identifier
tuple
ignored_any
map
}
forward_parsed_value! {
bool => deserialize_bool,
u8 => deserialize_u8,
u16 => deserialize_u16,
u32 => deserialize_u32,
u64 => deserialize_u64,
i8 => deserialize_i8,
i16 => deserialize_i16,
i32 => deserialize_i32,
i64 => deserialize_i64,
f32 => deserialize_f32,
f64 => deserialize_f64,
}
}
struct ValueEnumAccess<'de>(Cow<'de, str>);
impl<'de> de::EnumAccess<'de> for ValueEnumAccess<'de> {
type Error = Error;
type Variant = UnitOnlyVariantAccess;
fn variant_seed<V>(
self,
seed: V,
) -> Result<(V::Value, Self::Variant), Self::Error>
where
V: de::DeserializeSeed<'de>,
{
let variant = seed.deserialize(self.0.into_deserializer())?;
Ok((variant, UnitOnlyVariantAccess))
}
}
struct UnitOnlyVariantAccess;
impl<'de> de::VariantAccess<'de> for UnitOnlyVariantAccess {
type Error = Error;
fn unit_variant(self) -> Result<(), Self::Error> {
Ok(())
}
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
where
T: de::DeserializeSeed<'de>,
{
Err(Error::custom("expected unit variant"))
}
fn tuple_variant<V>(
self,
_len: usize,
_visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
Err(Error::custom("expected unit variant"))
}
fn struct_variant<V>(
self,
_fields: &'static [&'static str],
_visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
Err(Error::custom("expected unit variant"))
}
}

69
urlencoded/src/error.rs Normal file
View File

@ -0,0 +1,69 @@
use std::{borrow::Cow, error, fmt, str};
use serde::ser;
pub type Result<T> = std::result::Result<T, Error>;
/// Errors returned during serializing to `application/x-www-form-urlencoded`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
Custom(Cow<'static, str>),
Utf8(str::Utf8Error),
}
impl Error {
pub fn done() -> Self {
Error::Custom("this pair has already been serialized".into())
}
pub fn not_done() -> Self {
Error::Custom("this pair has not yet been serialized".into())
}
pub fn unsupported_pair() -> Self {
Error::Custom("unsupported pair".into())
}
pub fn top_level() -> Self {
let msg = "top-level serializer supports only maps and structs";
Error::Custom(msg.into())
}
pub fn no_key() -> Self {
let msg = "tried to serialize a value before serializing key";
Error::Custom(msg.into())
}
}
impl fmt::Display for Error {
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),
}
}
}
impl error::Error for Error {
/// The lower-level cause of this error, in the case of a `Utf8` 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),
}
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Custom(format!("{}", msg).into())
}
}

10
urlencoded/src/lib.rs Normal file
View File

@ -0,0 +1,10 @@
//! `x-www-form-urlencoded` meets Serde
pub mod de;
pub mod error;
pub mod ser;
#[doc(inline)]
pub use de::{from_bytes, from_reader, from_str, Deserializer};
#[doc(inline)]
pub use ser::{to_string, Serializer};

77
urlencoded/src/ser/key.rs Normal file
View File

@ -0,0 +1,77 @@
use std::{borrow::Cow, ops::Deref};
use serde::Serialize;
use crate::ser::{part::Sink, Error};
pub enum Key<'key> {
Static(&'static str),
Dynamic(Cow<'key, str>),
}
impl<'key> Deref for Key<'key> {
type Target = str;
fn deref(&self) -> &str {
match *self {
Key::Static(key) => key,
Key::Dynamic(ref key) => key,
}
}
}
impl<'key> From<Key<'key>> for Cow<'static, str> {
fn from(key: Key<'key>) -> Self {
match key {
Key::Static(key) => key.into(),
Key::Dynamic(key) => key.into_owned().into(),
}
}
}
pub struct KeySink<End> {
end: End,
}
impl<End, Ok> KeySink<End>
where
End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>,
{
pub fn new(end: End) -> Self {
KeySink { end: end }
}
}
impl<End, Ok> Sink for KeySink<End>
where
End: for<'key> FnOnce(Key<'key>) -> Result<Ok, Error>,
{
type Ok = Ok;
fn serialize_static_str(self, value: &'static str) -> Result<Ok, Error> {
(self.end)(Key::Static(value))
}
fn serialize_str(self, value: &str) -> Result<Ok, Error> {
(self.end)(Key::Dynamic(value.into()))
}
fn serialize_string(self, value: String) -> Result<Ok, Error> {
(self.end)(Key::Dynamic(value.into()))
}
fn serialize_none(self) -> Result<Ok, Error> {
Err(self.unsupported())
}
fn serialize_some<T: ?Sized + Serialize>(
self,
_value: &T,
) -> Result<Ok, Error> {
Err(self.unsupported())
}
fn unsupported(self) -> Error {
Error::Custom("unsupported key".into())
}
}

514
urlencoded/src/ser/mod.rs Normal file
View File

@ -0,0 +1,514 @@
//! Serialization support for the `application/x-www-form-urlencoded` format.
mod key;
mod pair;
mod part;
mod value;
use std::{borrow::Cow, str};
use serde::ser;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use crate::error::{Error, Result};
/// Serializes a value into a `application/x-www-form-urlencoded` `String` buffer.
///
/// ```
/// let meal = &[
/// ("bread", "baguette"),
/// ("cheese", "comté"),
/// ("meat", "ham"),
/// ("fat", "butter"),
/// ];
///
/// assert_eq!(
/// serde_urlencoded::to_string(meal),
/// Ok("bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter".to_owned()));
/// ```
pub fn to_string<T: ser::Serialize>(input: T) -> Result<String> {
let mut urlencoder = UrlEncodedSerializer::new("".to_owned());
input.serialize(Serializer::new(&mut urlencoder))?;
Ok(urlencoder.finish())
}
/// A serializer for the `application/x-www-form-urlencoded` format.
///
/// * Supported top-level inputs are structs, maps and sequences of pairs,
/// with or without a given length.
///
/// * Supported keys and values are integers, bytes (if convertible to strings),
/// unit structs and unit variants.
///
/// * Newtype structs defer to their inner values.
pub struct Serializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, 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,
}
}
}
/// Sequence serializer.
pub struct SeqSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
key: Option<Cow<'input, str>>,
count: usize,
}
/// Tuple serializer.
///
/// Mostly used for arrays.
pub struct TupleSerializer<'input, 'output, Target: 'output + 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>
{
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,
> {
inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>,
}
/// Map serializer.
pub struct MapSerializer<'input, 'output, Target: 'output + UrlEncodedTarget> {
urlencoder: &'output mut UrlEncodedSerializer<'input, Target>,
key: Option<Cow<'static, str>>,
}
/// Struct serializer.
pub struct StructSerializer<'input, 'output, Target: 'output + 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,
> {
inner: ser::Impossible<&'output mut UrlEncodedSerializer<'input, T>, Error>,
}
impl<'input, 'output, Target> ser::Serializer
for Serializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
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 SerializeMap = MapSerializer<'input, 'output, Target>;
type SerializeStruct = StructSerializer<'input, 'output, Target>;
type SerializeStructVariant =
StructVariantSerializer<'input, 'output, Target>;
/// Returns an error.
fn serialize_bool(self, _v: bool) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_i8(self, _v: i8) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_i16(self, _v: i16) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_i32(self, _v: i32) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_i64(self, _v: i64) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_u8(self, _v: u8) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_u16(self, _v: u16) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_u32(self, _v: u32) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_u64(self, _v: u64) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_f32(self, _v: f32) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_f64(self, _v: f64) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_char(self, _v: char) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_str(self, _value: &str) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns `Ok`.
fn serialize_unit(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
/// Returns `Ok`.
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
/// Returns an error.
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Serializes the inner value, ignoring the newtype name.
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok> {
value.serialize(self)
}
/// Returns an error.
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok> {
Err(Error::top_level())
}
/// Returns `Ok`.
fn serialize_none(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
/// Serializes the given value.
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<Self::Ok> {
value.serialize(self)
}
/// Serialize a sequence, given length (if any) is ignored.
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(SeqSerializer {
urlencoder: self.urlencoder,
key: None,
count: 0,
})
}
/// Returns an error.
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
Ok(TupleSerializer {
urlencoder: self.urlencoder,
})
}
/// Returns an error.
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct> {
Err(Error::top_level())
}
/// Returns an error.
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(Error::top_level())
}
/// Serializes a map, given length is ignored.
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Ok(MapSerializer {
urlencoder: self.urlencoder,
key: None,
})
}
/// Serializes a struct, given length is ignored.
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct> {
Ok(StructSerializer {
urlencoder: self.urlencoder,
})
}
/// Returns an error.
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Err(Error::top_level())
}
}
impl<'input, 'output, Target> ser::SerializeSeq
for SeqSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
value.serialize(pair::PairSerializer::new(
self.urlencoder,
self.key.as_ref(),
&mut self.count,
))
}
fn end(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
}
impl<'input, 'output, Target> ser::SerializeTuple
for TupleSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
value.serialize(pair::PairSerializer::new(
self.urlencoder,
None,
&mut 0,
))
}
fn end(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
}
impl<'input, 'output, Target> ser::SerializeTupleStruct
for TupleStructSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
self.inner.serialize_field(value)
}
fn end(self) -> Result<Self::Ok> {
self.inner.end()
}
}
impl<'input, 'output, Target> ser::SerializeTupleVariant
for TupleVariantSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
self.inner.serialize_field(value)
}
fn end(self) -> Result<Self::Ok> {
self.inner.end()
}
}
impl<'input, 'output, Target> ser::SerializeMap
for MapSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_entry<
K: ?Sized + ser::Serialize,
V: ?Sized + ser::Serialize,
>(
&mut self,
key: &K,
value: &V,
) -> Result<()> {
let key_sink = key::KeySink::new(|key| {
let value_sink = value::ValueSink::new(self.urlencoder, &key);
value.serialize(part::PartSerializer::new(value_sink))?;
self.key = None;
Ok(())
});
let entry_serializer = part::PartSerializer::new(key_sink);
key.serialize(entry_serializer)
}
fn serialize_key<T: ?Sized + ser::Serialize>(
&mut self,
key: &T,
) -> Result<()> {
let key_sink = key::KeySink::new(|key| Ok(key.into()));
let key_serializer = part::PartSerializer::new(key_sink);
self.key = Some(key.serialize(key_serializer)?);
Ok(())
}
fn serialize_value<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
{
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))?;
}
self.key = None;
Ok(())
}
fn end(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
}
impl<'input, 'output, Target> ser::SerializeStruct
for StructSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<()> {
let key = Cow::Borrowed(key);
let mut count = 0;
let value_sink =
pair::PairSerializer::new(self.urlencoder, Some(&key), &mut count);
value.serialize(value_sink)?;
Ok(())
}
fn end(self) -> Result<Self::Ok> {
Ok(self.urlencoder)
}
}
impl<'input, 'output, Target> ser::SerializeStructVariant
for StructVariantSerializer<'input, 'output, Target>
where
Target: 'output + UrlEncodedTarget,
{
type Ok = &'output mut UrlEncodedSerializer<'input, Target>;
type Error = Error;
fn serialize_field<T: ?Sized + ser::Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<()> {
self.inner.serialize_field(key, value)
}
fn end(self) -> Result<Self::Ok> {
self.inner.end()
}
}

275
urlencoded/src/ser/pair.rs Normal file
View File

@ -0,0 +1,275 @@
use std::borrow::Cow;
use std::mem;
use serde::ser;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use crate::{
error::{Error, Result},
ser::{key::KeySink, part::PartSerializer, value::ValueSink},
};
macro_rules! serialize_pair {
($($ty:ty => $name:ident,)*) => {
$(
fn $name(self, value: $ty) -> Result<()> {
let key = if let Some(key) = self.key {
key.clone()
} else {
return Err(Error::no_key());
};
let value_sink = ValueSink::new(self.urlencoder, &key);
let value_serializer = PartSerializer::new(value_sink);
value_serializer.$name(value)
}
)*
};
}
pub struct PairSerializer<'input, 'target, Target: 'target + UrlEncodedTarget> {
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
state: PairState,
key: Option<&'target Cow<'target, str>>,
count: &'target mut usize,
}
impl<'input, 'target, Target> PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
pub fn new(
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
key: Option<&'target Cow<'target, str>>,
count: &'target mut usize,
) -> Self {
PairSerializer {
urlencoder,
state: PairState::WaitingForKey,
key,
count,
}
}
}
impl<'input, 'target, Target> ser::Serializer
for PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
type SerializeSeq = Self;
type SerializeTuple = Self;
type SerializeTupleStruct = ser::Impossible<(), Error>;
type SerializeTupleVariant = ser::Impossible<(), Error>;
type SerializeMap = ser::Impossible<(), Error>;
type SerializeStruct = ser::Impossible<(), Error>;
type SerializeStructVariant = ser::Impossible<(), Error>;
serialize_pair! {
bool => serialize_bool,
u8 => serialize_u8,
u16 => serialize_u16,
u32 => serialize_u32,
u64 => serialize_u64,
i8 => serialize_i8,
i16 => serialize_i16,
i32 => serialize_i32,
i64 => serialize_i64,
f32 => serialize_f32,
f64 => serialize_f64,
char => serialize_char,
&str => serialize_str,
}
fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
Err(Error::unsupported_pair())
}
fn serialize_unit(self) -> Result<()> {
Err(Error::unsupported_pair())
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
Err(Error::unsupported_pair())
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<()> {
let key = if let Some(key) = self.key {
key.clone()
} else {
let key = Cow::Owned(self.count.to_string());
*self.count += 1;
key
};
let value_sink = ValueSink::new(self.urlencoder, &key);
let value_serializer = PartSerializer::new(value_sink);
value_serializer.serialize_str(variant)
}
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<()> {
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<()> {
Err(Error::unsupported_pair())
}
fn serialize_none(self) -> Result<()> {
Ok(())
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<()> {
value.serialize(self)
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(self)
}
fn serialize_tuple(self, len: usize) -> Result<Self> {
if len == 2 {
Ok(self)
} else {
Err(Error::unsupported_pair())
}
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct> {
Err(Error::unsupported_pair())
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(Error::unsupported_pair())
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
Err(Error::unsupported_pair())
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct> {
Err(Error::unsupported_pair())
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
Err(Error::unsupported_pair())
}
}
impl<'input, 'target, Target> ser::SerializeSeq
for PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
value.serialize(PairSerializer::new(
self.urlencoder,
self.key,
&mut self.count,
))
}
fn end(self) -> Result<Self::Ok> {
Ok(())
}
}
impl<'input, 'target, Target> ser::SerializeTuple
for PairSerializer<'input, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized + ser::Serialize>(
&mut self,
value: &T,
) -> Result<()> {
match mem::replace(&mut self.state, PairState::Done) {
PairState::WaitingForKey => {
let key_sink = KeySink::new(|key| Ok(key.into()));
let key_serializer = PartSerializer::new(key_sink);
self.state = PairState::WaitingForValue {
key: value.serialize(key_serializer)?,
};
Ok(())
},
PairState::WaitingForValue { key } => {
let result = {
let value_sink = ValueSink::new(self.urlencoder, &key);
let value_serializer = PartSerializer::new(value_sink);
value.serialize(value_serializer)
};
if result.is_ok() {
self.state = PairState::Done;
} else {
self.state = PairState::WaitingForValue { key: key };
}
result
},
PairState::Done => Err(Error::done()),
}
}
fn end(self) -> Result<()> {
if let PairState::Done = self.state {
Ok(())
} else {
Err(Error::not_done())
}
}
}
enum PairState {
WaitingForKey,
WaitingForValue { key: Cow<'static, str> },
Done,
}

233
urlencoded/src/ser/part.rs Normal file
View File

@ -0,0 +1,233 @@
use std::str;
use dtoa;
use itoa;
use serde::ser;
use crate::error::Error;
pub struct PartSerializer<S> {
sink: S,
}
impl<S: Sink> PartSerializer<S> {
pub fn new(sink: S) -> Self {
PartSerializer { sink: sink }
}
}
pub trait Sink: Sized {
type Ok;
fn serialize_static_str(
self,
value: &'static str,
) -> Result<Self::Ok, Error>;
fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>;
fn serialize_string(self, value: String) -> Result<Self::Ok, Error>;
fn serialize_none(self) -> Result<Self::Ok, Error>;
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Error>;
fn unsupported(self) -> Error;
}
impl<S: Sink> ser::Serializer for PartSerializer<S> {
type Ok = S::Ok;
type Error = Error;
type SerializeSeq = ser::Impossible<S::Ok, Error>;
type SerializeTuple = ser::Impossible<S::Ok, Error>;
type SerializeTupleStruct = ser::Impossible<S::Ok, Error>;
type SerializeTupleVariant = ser::Impossible<S::Ok, Error>;
type SerializeMap = ser::Impossible<S::Ok, Error>;
type SerializeStruct = ser::Impossible<S::Ok, Error>;
type SerializeStructVariant = ser::Impossible<S::Ok, Error>;
fn serialize_bool(self, v: bool) -> Result<S::Ok, Error> {
self.sink
.serialize_static_str(if v { "true" } else { "false" })
}
fn serialize_i8(self, v: i8) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_i16(self, v: i16) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_i32(self, v: i32) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_i64(self, v: i64) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_u8(self, v: u8) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_u16(self, v: u16) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_u32(self, v: u32) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_u64(self, v: u64) -> Result<S::Ok, Error> {
self.serialize_integer(v)
}
fn serialize_f32(self, v: f32) -> Result<S::Ok, Error> {
self.serialize_floating(v)
}
fn serialize_f64(self, v: f64) -> Result<S::Ok, Error> {
self.serialize_floating(v)
}
fn serialize_char(self, v: char) -> Result<S::Ok, Error> {
self.sink.serialize_string(v.to_string())
}
fn serialize_str(self, value: &str) -> Result<S::Ok, Error> {
self.sink.serialize_str(value)
}
fn serialize_bytes(self, value: &[u8]) -> Result<S::Ok, Error> {
match str::from_utf8(value) {
Ok(value) => self.sink.serialize_str(value),
Err(err) => Err(Error::Utf8(err)),
}
}
fn serialize_unit(self) -> Result<S::Ok, Error> {
Err(self.sink.unsupported())
}
fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, Error> {
self.sink.serialize_static_str(name.into())
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<S::Ok, Error> {
self.sink.serialize_static_str(variant.into())
}
fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<S::Ok, Error> {
value.serialize(self)
}
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<S::Ok, Error> {
Err(self.sink.unsupported())
}
fn serialize_none(self) -> Result<S::Ok, Error> {
self.sink.serialize_none()
}
fn serialize_some<T: ?Sized + ser::Serialize>(
self,
value: &T,
) -> Result<S::Ok, Error> {
self.sink.serialize_some(value)
}
fn serialize_seq(
self,
_len: Option<usize>,
) -> Result<Self::SerializeSeq, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple(
self,
_len: usize,
) -> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTuple, Error> {
Err(self.sink.unsupported())
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant, Error> {
Err(self.sink.unsupported())
}
fn serialize_map(
self,
_len: Option<usize>,
) -> Result<Self::SerializeMap, Error> {
Err(self.sink.unsupported())
}
fn serialize_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeStruct, Error> {
Err(self.sink.unsupported())
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant, Error> {
Err(self.sink.unsupported())
}
}
impl<S: Sink> PartSerializer<S> {
fn serialize_integer<I>(self, value: I) -> Result<S::Ok, Error>
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]) };
ser::Serializer::serialize_str(self, part)
}
fn serialize_floating<F>(self, value: F) -> Result<S::Ok, Error>
where
F: dtoa::Floating,
{
let mut buf = [b'\0'; 24];
let len = dtoa::write(&mut buf[..], value).unwrap();
let part = unsafe { str::from_utf8_unchecked(&buf[0..len]) };
ser::Serializer::serialize_str(self, part)
}
}

View File

@ -0,0 +1,67 @@
use std::str;
use serde::Serialize;
use url::form_urlencoded::{
Serializer as UrlEncodedSerializer, Target as UrlEncodedTarget,
};
use crate::{
error::Error,
ser::part::{PartSerializer, Sink},
};
pub struct ValueSink<'input, 'key, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
key: &'key str,
}
impl<'input, 'key, 'target, Target> ValueSink<'input, 'key, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
pub fn new(
urlencoder: &'target mut UrlEncodedSerializer<'input, Target>,
key: &'key str,
) -> Self {
ValueSink { urlencoder, key }
}
}
impl<'input, 'key, 'target, Target> Sink
for ValueSink<'input, 'key, 'target, Target>
where
Target: 'target + UrlEncodedTarget,
{
type Ok = ();
fn serialize_str(self, value: &str) -> Result<(), Error> {
self.urlencoder.append_pair(self.key, value);
Ok(())
}
fn serialize_static_str(self, value: &'static str) -> Result<(), Error> {
self.serialize_str(value)
}
fn serialize_string(self, value: String) -> Result<(), Error> {
self.serialize_str(&value)
}
fn serialize_none(self) -> Result<Self::Ok, Error> {
Ok(())
}
fn serialize_some<T: ?Sized + Serialize>(
self,
value: &T,
) -> Result<Self::Ok, Error> {
value.serialize(PartSerializer::new(self))
}
fn unsupported(self) -> Error {
Error::Custom("unsupported value".into())
}
}

View File

@ -0,0 +1,199 @@
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq)]
struct NewType<T>(T);
#[test]
fn deserialize_newtype_i32() {
let result = vec![("field".to_owned(), NewType(11))];
assert_eq!(serde_urlencoded::from_str("field=11"), Ok(result));
}
#[test]
fn deserialize_bytes() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(
serde_urlencoded::from_bytes(b"first=23&last=42"),
Ok(result)
);
}
#[test]
fn deserialize_str() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(serde_urlencoded::from_str("first=23&last=42"), Ok(result));
}
#[test]
fn deserialize_borrowed_str() {
let result = vec![("first", 23), ("last", 42)];
assert_eq!(serde_urlencoded::from_str("first=23&last=42"), Ok(result));
}
#[test]
fn deserialize_reader() {
let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];
assert_eq!(
serde_urlencoded::from_reader(b"first=23&last=42" as &[_]),
Ok(result)
);
}
#[test]
fn deserialize_option() {
let result = vec![
("first".to_owned(), Some(23)),
("last".to_owned(), Some(42)),
];
assert_eq!(serde_urlencoded::from_str("first=23&last=42"), Ok(result));
}
#[test]
fn deserialize_unit() {
assert_eq!(serde_urlencoded::from_str(""), Ok(()));
assert_eq!(serde_urlencoded::from_str("&"), Ok(()));
assert_eq!(serde_urlencoded::from_str("&&"), Ok(()));
assert!(serde_urlencoded::from_str::<()>("first=23").is_err());
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
enum X {
A,
B,
C,
}
#[test]
fn deserialize_unit_enum() {
let result = vec![
("one".to_owned(), X::A),
("two".to_owned(), X::B),
("three".to_owned(), X::C),
];
assert_eq!(
serde_urlencoded::from_str("one=A&two=B&three=C"),
Ok(result)
);
}
#[test]
fn deserialize_unit_type() {
assert_eq!(serde_urlencoded::from_str(""), Ok(()));
}
#[derive(Debug, Deserialize, PartialEq)]
struct Wrapper<T> {
item: T,
}
#[derive(Debug, PartialEq, Deserialize)]
struct NewStruct {
list: Vec<String>,
}
#[derive(Debug, PartialEq, Deserialize)]
struct Struct {
list: Vec<Option<String>>,
}
#[derive(Debug, PartialEq, Deserialize)]
struct NumList {
list: Vec<u8>,
}
#[derive(Debug, PartialEq, Deserialize)]
struct ListStruct {
list: Vec<NewType<usize>>,
}
#[derive(Debug, PartialEq, Deserialize)]
struct MapStruct {
a: usize,
b: String,
c: Option<u8>,
}
#[test]
fn deserialize_mapstruct() {
let de = MapStruct {
a: 10,
b: "Hello".into(),
c: None,
};
assert_eq!(
de,
serde_urlencoded::from_str::<MapStruct>("a=10&b=Hello").unwrap()
);
}
#[test]
fn deserialize_newstruct() {
let de = NewStruct {
list: vec!["hello".into(), "world".into()],
};
assert_eq!(
de,
serde_urlencoded::from_str::<NewStruct>("list=hello&list=world")
.unwrap()
);
}
#[test]
fn deserialize_numlist() {
let de = NumList {
list: vec![1, 2, 3, 4],
};
assert_eq!(
de,
serde_urlencoded::from_str::<NumList>("list=1&list=2&list=3&list=4")
.unwrap()
);
}
#[test]
fn deserialize_vec_bool() {
assert_eq!(
Wrapper {
item: vec![true, false, false]
},
serde_urlencoded::from_str::<Wrapper<_>>(
"item=true&item=false&item=false"
)
.unwrap()
);
}
#[test]
fn deserialize_vec_string() {
assert_eq!(
Wrapper {
item: vec![
"hello".to_string(),
"matrix".to_string(),
"hello".to_string()
],
},
serde_urlencoded::from_str::<Wrapper<_>>(
"item=hello&item=matrix&item=hello"
)
.unwrap()
);
}
#[test]
fn deserialize_struct_unit_enum() {
let result = Wrapper {
item: vec![X::A, X::B, X::C],
};
assert_eq!(
serde_urlencoded::from_str("item=A&item=B&item=C"),
Ok(result)
);
}

View File

@ -0,0 +1,194 @@
use serde::Serialize;
#[derive(Serialize)]
struct NewType<T>(T);
#[test]
fn serialize_newtype_i32() {
let params = &[("field", Some(NewType(11)))];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("field=11".to_owned())
);
}
#[test]
fn serialize_option_map_int() {
let params = &[("first", Some(23)), ("middle", None), ("last", Some(42))];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("first=23&last=42".to_owned())
);
}
#[test]
fn serialize_option_map_string() {
let params = &[
("first", Some("hello")),
("middle", None),
("last", Some("world")),
];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("first=hello&last=world".to_owned())
);
}
#[test]
fn serialize_option_map_bool() {
let params = &[("one", Some(true)), ("two", Some(false))];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("one=true&two=false".to_owned())
);
}
#[test]
fn serialize_map_bool() {
let params = &[("one", true), ("two", false)];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("one=true&two=false".to_owned())
);
}
#[derive(Serialize)]
enum X {
A,
B,
C,
}
#[test]
fn serialize_unit_enum() {
let params = &[("one", X::A), ("two", X::B), ("three", X::C)];
assert_eq!(
serde_urlencoded::to_string(params),
Ok("one=A&two=B&three=C".to_owned())
);
}
#[derive(Serialize)]
struct Unit;
#[test]
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()));
}
#[derive(Serialize)]
struct Wrapper<T> {
item: T,
}
#[derive(Serialize)]
struct NewStruct {
list: Vec<String>,
}
#[derive(Serialize)]
struct Struct {
list: Vec<Option<String>>,
}
#[derive(Serialize)]
struct ListStruct {
list: Vec<NewType<usize>>,
}
#[test]
fn serialize_newstruct() {
let s = NewStruct {
list: vec!["hello".into(), "world".into()],
};
assert_eq!(
"list=hello&list=world".to_string(),
serde_urlencoded::to_string(s).unwrap()
);
}
#[test]
fn serialize_vec_bool() {
let params = Wrapper {
item: vec![true, false, false],
};
assert_eq!(
serde_urlencoded::to_string(params).unwrap(),
"item=true&item=false&item=false".to_owned()
);
}
#[test]
fn serialize_vec_num() {
let params = Wrapper {
item: vec![0, 1, 2],
};
assert_eq!(
serde_urlencoded::to_string(params).unwrap(),
"item=0&item=1&item=2".to_owned()
);
}
#[test]
fn serialize_vec_str() {
let params = Wrapper {
item: vec!["hello", "world", "hello"],
};
assert_eq!(
serde_urlencoded::to_string(params).unwrap(),
"item=hello&item=world&item=hello".to_owned()
);
}
#[test]
fn serialize_struct_opt() {
let s = Struct {
list: vec![Some("hello".into()), Some("world".into())],
};
assert_eq!(
"list=hello&list=world".to_string(),
serde_urlencoded::to_string(s).unwrap()
);
}
#[test]
fn serialize_struct_newtype() {
let s = ListStruct {
list: vec![NewType(0), NewType(1)],
};
assert_eq!(
"list=0&list=1".to_string(),
serde_urlencoded::to_string(s).unwrap()
);
}
#[test]
fn serialize_struct_unit_enum() {
let params = Wrapper {
item: vec![X::A, X::B, X::C],
};
assert_eq!(
serde_urlencoded::to_string(params),
Ok("item=A&item=B&item=C".to_owned())
);
}
#[test]
fn serialize_map() {
let mut s = std::collections::BTreeMap::new();
s.insert("hello", "world");
s.insert("seri", "alize");
s.insert("matrix", "ruma");
let encoded = serde_urlencoded::to_string(s).unwrap();
assert_eq!("hello=world&matrix=ruma&seri=alize", encoded);
}