From 6ec689b3762a1f8090d97045a76d8d7282dd1fe3 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sun, 11 Sep 2016 13:26:21 +0200 Subject: x-www-form-urlencoded meets Serde --- .gitignore | 2 + .travis.yml | 5 + Cargo.toml | 13 ++ LICENSE-APACHE | 176 ++++++++++++++++++++++ LICENSE-MIT | 25 ++++ README.md | 50 +++++++ src/de.rs | 90 +++++++++++ src/lib.rs | 11 ++ src/ser/key.rs | 292 ++++++++++++++++++++++++++++++++++++ src/ser/mod.rs | 445 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ser/pair.rs | 330 +++++++++++++++++++++++++++++++++++++++++ src/ser/value.rs | 322 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 1761 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Cargo.toml create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 src/de.rs create mode 100644 src/lib.rs create mode 100644 src/ser/key.rs create mode 100644 src/ser/mod.rs create mode 100644 src/ser/pair.rs create mode 100644 src/ser/value.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4ecd9b6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: rust +rust: + - nightly + - beta + - stable diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a5098d9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "serde_urlencoded" +version = "0.1.0" +authors = ["Anthony Ramine "] +license = "MIT/Apache-2.0" +repository = "https://github.com/nox/serde_urlencoded" +documentation = "https://docs.rs/crate/serde_urlencoded" +description = "`x-www-form-urlencoded` meets Serde" +keywords = ["serde", "serialization", "urlencoded"] + +[dependencies] +serde = "0.8.7" +url = "1.0.0" diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..1b5ec8b --- /dev/null +++ b/LICENSE-APACHE @@ -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 diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..39f6303 --- /dev/null +++ b/LICENSE-MIT @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c9fe8f --- /dev/null +++ b/README.md @@ -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.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 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 by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. diff --git a/src/de.rs b/src/de.rs new file mode 100644 index 0000000..ec84ff2 --- /dev/null +++ b/src/de.rs @@ -0,0 +1,90 @@ +//! Deserialization support for the `application/x-www-form-urlencoded` format. + +use serde::de; +use serde::de::value::MapDeserializer; +use std::borrow::Cow; +use std::marker::PhantomData; +use url::form_urlencoded::Parse; + +pub use serde::de::value::Error; + +/// A deserializer for the `application/x-www-form-urlencoded` format. +/// +/// * Supported top-level outputs are structs, maps and sequences of pairs, +/// 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<'a, T> { + deserializer: MapDeserializer, Cow<'a, str>, Cow<'a, str>, Error>, + marker: PhantomData, +} + +impl<'a, T> de::Deserializer for Deserializer<'a, T> +{ + type Error = Error; + + fn deserialize( + &mut self, visitor: V) + -> Result + where V: de::Visitor, + { + self.deserialize_map(visitor) + } + + fn deserialize_map( + &mut self, mut visitor: V) + -> Result + where V: de::Visitor, + { + visitor.visit_map(&mut self.deserializer) + } + + fn deserialize_seq( + &mut self, mut visitor: V) + -> Result + where V: de::Visitor, + { + visitor.visit_seq(&mut self.deserializer) + } + + fn deserialize_seq_fixed_size( + &mut self, _len: usize, mut visitor: V) + -> Result + where V: de::Visitor + { + visitor.visit_seq(&mut self.deserializer) + } + + forward_to_deserialize! { + bool + usize + u8 + u16 + u32 + u64 + isize + i8 + i16 + i32 + i64 + f32 + f64 + char + str + string + unit + option + bytes + unit_struct + newtype_struct + tuple_struct + struct + struct_field + tuple + enum + ignored_any + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..95a0ad6 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +//! `x-www-form-urlencoded` meets Serde + +#[macro_use] +extern crate serde; +extern crate url; + +pub mod de; +pub mod ser; + +pub use de::Deserializer; +pub use ser::Serializer; diff --git a/src/ser/key.rs b/src/ser/key.rs new file mode 100644 index 0000000..a26c563 --- /dev/null +++ b/src/ser/key.rs @@ -0,0 +1,292 @@ +use serde::{Serialize, Serializer}; +use ser::Error; +use std::borrow::Cow; +use std::str; + +pub struct MapKeySerializer<'key>(&'key mut Option>); + +impl<'key> MapKeySerializer<'key> { + pub fn new(output: &'key mut Option>) -> Self { + MapKeySerializer(output) + } + + fn set_key(&mut self, key: T) -> Result<(), Error> + where T: Into> + { + *self.0 = Some(key.into()); + Ok(()) + } +} + +impl<'key> Serializer for MapKeySerializer<'key> { + type Error = Error; + type SeqState = (); + type TupleState = (); + type TupleStructState = (); + type TupleVariantState = (); + type MapState = (); + type StructState = (); + type StructVariantState = (); + + fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_char(&mut self, v: char) -> Result<(), Error> { + self.set_key(v.to_string()) + } + + fn serialize_str(&mut self, value: &str) -> Result<(), Error> { + self.set_key(String::from(value)) + } + + fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Error> { + match str::from_utf8(value) { + Ok(value) => self.set_key(String::from(value)), + Err(err) => Err(Error::Utf8(err)), + } + } + + fn serialize_unit(&mut self) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_unit_struct( + &mut self, name: &'static str) + -> Result<(), Error> { + self.set_key(name) + } + + fn serialize_unit_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + variant: &'static str) + -> Result<(), Error> { + self.set_key(variant) + } + + fn serialize_newtype_struct( + &mut self, _name: &'static str, value: T) + -> Result<(), Error> + where T: Serialize + { + value.serialize(self) + } + + fn serialize_newtype_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_none(&mut self) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_some(&mut self, _value: T) -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_seq(&mut self, _len: Option) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_seq_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple(&mut self, _len: usize) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_struct( + &mut self, _name: &'static str, _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_struct_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_variant_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_map(&mut self, _len: Option) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_map_key( + &mut self, _state: &mut (), _key: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_map_value( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_struct( + &mut self, _name: &'static str, _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_struct_elt( + &mut self, _state: &mut (), _key: &'static str, _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_key()) + } + + fn serialize_struct_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_struct_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_struct_variant_elt( + &mut self, _state: &mut (), _key: &'static str, _value: T) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } + + fn serialize_struct_variant_end( + &mut self, _state: ()) + -> Result<(), Error> { + Err(Error::unsupported_key()) + } +} + +impl Error { + fn unsupported_key() -> Self { + Error::Custom("unsupported key".into()) + } +} diff --git a/src/ser/mod.rs b/src/ser/mod.rs new file mode 100644 index 0000000..387766d --- /dev/null +++ b/src/ser/mod.rs @@ -0,0 +1,445 @@ +//! Serialization support for the `application/x-www-form-urlencoded` format. + +mod key; +mod pair; +mod value; + +use serde::ser; +use std::borrow::Cow; +use std::error; +use std::fmt; +use std::str; +use url::form_urlencoded; + +/// 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(form_urlencoded::Serializer) + where T: form_urlencoded::Target; + +/// Errors returned during serializing to `application/x-www-form-urlencoded`. +#[derive(Clone, Debug)] +pub enum Error { + Custom(Cow<'static, str>), + InvalidValue(Cow<'static, str>), + Utf8(str::Utf8Error), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match *self { + Error::Custom(ref msg) => msg.fmt(f), + Error::InvalidValue(ref msg) => write!(f, "invalid value: {}", msg), + Error::Utf8(ref err) => write!(f, "invalid UTF-8: {}", err), + } + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + match *self { + Error::Custom(ref msg) => msg, + Error::InvalidValue(ref msg) => msg, + Error::Utf8(ref err) => error::Error::description(err), + } + } + + /// The lower-level cause of this error, in the case of a `Utf8` error. + fn cause(&self) -> Option<&error::Error> { + match *self { + Error::Custom(_) | Error::InvalidValue(_) => None, + Error::Utf8(ref err) => Some(err), + } + } +} + +impl ser::Error for Error { + fn custom>(msg: T) -> Self { + Error::Custom(msg.into().into()) + } + + fn invalid_value(msg: &str) -> Self { + Error::InvalidValue(String::from(msg).into()) + } +} + +/// State used when serializing sequences. +pub struct SeqState(()); + +/// State used when serializing tuples. +pub struct TupleState(()); + +/// State used when serializing tuple structs. +pub struct TupleStructState(()); + +/// State used when serializing tuple variants. +pub struct TupleVariantState(()); + +/// State used when serializing maps. +pub struct MapState(Option>); + +/// State used when serializing structs. +pub struct StructState(()); + +/// State used when serializing struct variants. +pub struct StructVariantState(()); + +impl ser::Serializer for Serializer { + type Error = Error; + + /// State used when serializing sequences. + type SeqState = SeqState; + + /// State used when serializing tuples. + type TupleState = TupleState; + + /// State used when serializing tuple structs. + type TupleStructState = TupleStructState; + + /// State used when serializing tuple variants. + type TupleVariantState = TupleVariantState; + + /// State used when serializing maps. + type MapState = MapState; + + /// State used when serializing structs. + type StructState = StructState; + + /// State used when serializing struct variants. + type StructVariantState = StructVariantState; + + /// Returns an error. + fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_isize(&mut self, _v: isize) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_i8(&mut self, _v: i8) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_i16(&mut self, _v: i16) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_i32(&mut self, _v: i32) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_usize(&mut self, _v: usize) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_u8(&mut self, _v: u8) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_u16(&mut self, _v: u16) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_u32(&mut self, _v: u32) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_char(&mut self, _v: char) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_str(&mut self, _value: &str) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_bytes(&mut self, _value: &[u8]) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_unit(&mut self) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_unit_struct( + &mut self, _name: &'static str) + -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_unit_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str) + -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Serializes the inner value, ignoring the newtype name. + fn serialize_newtype_struct( + &mut self, _name: &'static str, value: T) + -> Result<(), Error> + where T: ser::Serialize + { + value.serialize(self) + } + + /// Returns an error. + fn serialize_newtype_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: T) + -> Result<(), Error> + where T: ser::Serialize + { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_none(&mut self) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_some(&mut self, _value: T) -> Result<(), Error> + where T: ser::Serialize + { + Err(Error::top_level()) + } + + /// Begins to serialize a sequence, given length (if any) is ignored. + fn serialize_seq( + &mut self, _len: Option) + -> Result { + Ok(SeqState(())) + } + + /// Serializes a sequence element. + fn serialize_seq_elt( + &mut self, _state: &mut SeqState, value: T) + -> Result<(), Error> + where T: ser::Serialize + { + value.serialize(&mut pair::PairSerializer::new(&mut self.0)) + } + + /// Finishes serializing a sequence. + fn serialize_seq_end(&mut self, _state: SeqState) -> Result<(), Error> { + Ok(()) + } + + /// Begins to serialize a sequence, given length is ignored. + fn serialize_seq_fixed_size( + &mut self, _length: usize) + -> Result { + Ok(SeqState(())) + } + + /// Returns an error. + fn serialize_tuple(&mut self, _len: usize) -> Result { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_elt( + &mut self, _state: &mut TupleState, _value: T) + -> Result<(), Error> + where T: ser::Serialize + { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_end(&mut self, _state: TupleState) -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_struct( + &mut self, _name: &'static str, _len: usize) + -> Result { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_struct_elt( + &mut self, _state: &mut TupleStructState, _value: T) + -> Result<(), Error> + where T: ser::Serialize + { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_struct_end( + &mut self, _state: TupleStructState) + -> Result<(), Error> + { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_variant_elt( + &mut self, _state: &mut TupleVariantState, _value: T) + -> Result<(), Error> + where T: ser::Serialize + { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_tuple_variant_end( + &mut self, _state: TupleVariantState) + -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Begins to serialize a map, given length (if any) is ignored. + fn serialize_map( + &mut self, _len: Option) + -> Result { + Ok(MapState(None)) + } + + /// Serializes a map key. + fn serialize_map_key( + &mut self, state: &mut MapState, key: T) + -> Result<(), Error> + where T: ser::Serialize + { + key.serialize(&mut key::MapKeySerializer::new(&mut state.0)) + } + + /// Serializes a map value. + fn serialize_map_value( + &mut self, state: &mut MapState, value: T) + -> Result<(), Error> + where T: ser::Serialize + { + let mut value_serializer = + try!(value::ValueSerializer::new(&mut state.0, &mut self.0)); + value.serialize(&mut value_serializer) + } + + /// Finishes serializing a map. + fn serialize_map_end(&mut self, _state: MapState) -> Result<(), Error> { + Ok(()) + } + + /// Begins to serialize a struct, given length is ignored. + fn serialize_struct( + &mut self, _name: &'static str, _len: usize) + -> Result { + Ok(StructState(())) + } + + /// Serializes a struct element. + fn serialize_struct_elt( + &mut self, + _state: &mut StructState, + key: &'static str, + value: T) + -> Result<(), Error> + where T: ser::Serialize + { + let mut key = Some(key.into()); + let mut value_serializer = + value::ValueSerializer::new(&mut key, &mut self.0).unwrap(); + value.serialize(&mut value_serializer) + } + + /// Finishes serializing a struct. + fn serialize_struct_end(&mut self, _state: StructState) + -> Result<(), Error> { + Ok(()) + } + + /// Returns an error. + fn serialize_struct_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_struct_variant_elt( + &mut self, + _state: &mut StructVariantState, + _key: &'static str, + _value: T) + -> Result<(), Error> { + Err(Error::top_level()) + } + + /// Returns an error. + fn serialize_struct_variant_end( + &mut self, _state: StructVariantState) + -> Result<(), Error> { + Err(Error::top_level()) + } +} + +impl Error { + fn top_level() -> Self { + Error::Custom( + "top-level serializer supports only maps and structs".into()) + } +} diff --git a/src/ser/pair.rs b/src/ser/pair.rs new file mode 100644 index 0000000..ae42db0 --- /dev/null +++ b/src/ser/pair.rs @@ -0,0 +1,330 @@ +use ser::{Error, key, value}; +use serde::{Serialize, Serializer}; +use std::borrow::Cow; +use url::form_urlencoded; + +pub struct PairSerializer<'target, Target>( + &'target mut form_urlencoded::Serializer) + where Target: 'target + form_urlencoded::Target; + +impl<'target, Target> PairSerializer<'target, Target> + where Target: 'target + form_urlencoded::Target +{ + pub fn new( + serializer: &'target mut form_urlencoded::Serializer) + -> Self { + PairSerializer(serializer) + } +} + +pub struct TupleState(Option>>); +pub struct TupleStructState(TupleState); + +impl<'target, Target> Serializer for PairSerializer<'target, Target> + where Target: 'target + form_urlencoded::Target +{ + type Error = Error; + type SeqState = (); + type TupleState = TupleState; + type TupleStructState = TupleStructState; + type TupleVariantState = (); + type MapState = (); + type StructState = (); + type StructVariantState = (); + + fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { + Err(Error::Custom("booleans are not supported values".into())) + } + + fn serialize_isize(&mut self, _v: isize) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_i8(&mut self, _v: i8) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_i16(&mut self, _v: i16) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_i32(&mut self, _v: i32) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_usize(&mut self, _v: usize) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_u8(&mut self, _v: u8) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_u16(&mut self, _v: u16) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_u32(&mut self, _v: u32) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_char(&mut self, _v: char) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_str(&mut self, _value: &str) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_bytes(&mut self, _value: &[u8]) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_unit(&mut self) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_unit_struct( + &mut self, _name: &'static str) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_unit_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_newtype_struct( + &mut self, _name: &'static str, value: T) + -> Result<(), Error> + where T: Serialize + { + value.serialize(self) + } + + fn serialize_newtype_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_none(&mut self) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_some(&mut self, _value: T) -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_seq(&mut self, _len: Option) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_seq_elt(&mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_seq_fixed_size(&mut self, _size: usize) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_tuple(&mut self, len: usize) -> Result { + if len == 2 { + Ok(TupleState(None)) + } else { + Err(Error::unsupported_pair()) + } + } + + fn serialize_tuple_elt( + &mut self, state: &mut TupleState, value: T) + -> Result<(), Error> + where T: Serialize + { + match state.0.take() { + None => { + let mut key = None; + { + let mut key_serializer = + key::MapKeySerializer::new(&mut key); + try!(value.serialize(&mut key_serializer)); + } + state.0 = Some(key); + Ok(()) + }, + Some(ref mut key) => { + { + let mut value_serializer = + value::ValueSerializer::new(key, &mut self.0).unwrap(); + try!(value.serialize(&mut value_serializer)); + } + state.0 = Some(None); + Ok(()) + } + } + } + + fn serialize_tuple_end(&mut self, _state: TupleState) -> Result<(), Error> { + Ok(()) + } + + fn serialize_tuple_struct( + &mut self, _name: &'static str, len: usize) + -> Result { + self.serialize_tuple(len).map(TupleStructState) + } + + fn serialize_tuple_struct_elt( + &mut self, state: &mut TupleStructState, value: T) + -> Result<(), Error> + where T: Serialize + { + self.serialize_tuple_elt(&mut state.0, value) + } + + fn serialize_tuple_struct_end( + &mut self, _state: TupleStructState) + -> Result<(), Error> { + Ok(()) + } + + fn serialize_tuple_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_tuple_variant_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_tuple_variant_end( + &mut self, _state: ()) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_map( + &mut self, _len: Option) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_map_key( + &mut self, _state: &mut (), _key: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_map_value( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_struct(&mut self, _name: &'static str, _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_struct_elt( + &mut self, + _state: &mut (), + _key: &'static str, + _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_pair()) + } + + fn serialize_struct_end( + &mut self, _state: ()) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_struct_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + fn serialize_struct_variant_elt( + &mut self, + _state: &mut (), + _key: &'static str, + _value: T) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } + + fn serialize_struct_variant_end( + &mut self, _state: ()) + -> Result<(), Error> { + Err(Error::unsupported_pair()) + } +} + +impl Error { + fn unsupported_pair() -> Self { + Error::Custom("unsupported pair".into()) + } +} diff --git a/src/ser/value.rs b/src/ser/value.rs new file mode 100644 index 0000000..39d3af0 --- /dev/null +++ b/src/ser/value.rs @@ -0,0 +1,322 @@ +use ser::Error; +use serde::{Serialize, Serializer}; +use std::borrow::Cow; +use std::str; +use url::form_urlencoded; + +pub struct ValueSerializer<'key, 'target, Target> + where Target: 'target + form_urlencoded::Target +{ + key: &'key mut Option>, + serializer: &'target mut form_urlencoded::Serializer +} + +impl<'key, 'target, Target> ValueSerializer<'key, 'target, Target> + where Target: 'target + form_urlencoded::Target +{ + pub fn new( + key: &'key mut Option>, + serializer: &'target mut form_urlencoded::Serializer) + -> Result { + if key.is_some() { + Ok(ValueSerializer { + key: key, + serializer: serializer, + }) + } else { + Err(Error::no_key()) + } + } + + fn append_pair(&mut self, value: &str) -> Result<(), Error> { + if let Some(key) = self.key.take() { + self.serializer.append_pair(&key, value); + Ok(()) + } else { + Err(Error::no_key()) + } + } +} + +impl<'key, 'target, Target> Serializer + for ValueSerializer<'key, 'target, Target> + where Target: 'target + form_urlencoded::Target +{ + type Error = Error; + type SeqState = (); + type TupleState = (); + type TupleStructState = (); + type TupleVariantState = (); + type MapState = (); + type StructState = (); + type StructVariantState = (); + + fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_isize(&mut self, v: isize) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_i8(&mut self, v: i8) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_i16(&mut self, v: i16) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_i32(&mut self, v: i32) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_i64(&mut self, v: i64) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_usize(&mut self, v: usize) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_u8(&mut self, v: u8) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_u16(&mut self, v: u16) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_u32(&mut self, v: u32) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_u64(&mut self, v: u64) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_f32(&mut self, v: f32) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_f64(&mut self, v: f64) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_char(&mut self, v: char) -> Result<(), Error> { + self.append_pair(&v.to_string()) + } + + fn serialize_str(&mut self, value: &str) -> Result<(), Error> { + self.append_pair(value) + } + + fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Error> { + match str::from_utf8(value) { + Ok(value) => self.append_pair(value), + Err(err) => Err(Error::Utf8(err)), + } + } + + fn serialize_unit(&mut self) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_unit_struct( + &mut self, name: &'static str) + -> Result<(), Error> { + self.append_pair(name) + } + + fn serialize_unit_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + variant: &'static str) + -> Result<(), Error> { + self.append_pair(variant) + } + + fn serialize_newtype_struct( + &mut self, _name: &'static str, value: T) + -> Result<(), Error> + where T: Serialize + { + value.serialize(self) + } + + fn serialize_newtype_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_none(&mut self) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_some(&mut self, _value: T) -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_seq(&mut self, _len: Option) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_seq_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_seq_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple(&mut self, _len: usize) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_struct( + &mut self, _name: &'static str, _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_struct_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_variant_elt( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_map(&mut self, _len: Option) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_map_key( + &mut self, _state: &mut (), _key: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_map_value( + &mut self, _state: &mut (), _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_map_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_struct( + &mut self, _name: &'static str, _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_struct_elt( + &mut self, + _state: &mut (), + _key: &'static str, + _value: T) + -> Result<(), Error> + where T: Serialize + { + Err(Error::unsupported_value()) + } + + fn serialize_struct_end(&mut self, _state: ()) -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_struct_variant( + &mut self, + _name: &'static str, + _variant_index: usize, + _variant: &'static str, + _len: usize) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } + fn serialize_struct_variant_elt( + &mut self, _state: &mut (), _key: &'static str, _value: T) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } + + fn serialize_struct_variant_end( + &mut self, _state: ()) + -> Result<(), Error> { + Err(Error::unsupported_value()) + } +} + +impl Error { + fn no_key() -> Self { + Error::Custom( + "tried to serialize a value before serializing key".into()) + } + + fn unsupported_value() -> Self { + Error::Custom("unsupported value".into()) + } +} -- cgit v1.2.3