From 7f4764dd6b63c0c0cf0d27268dfa812c0f2334fc Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Tue, 25 Apr 2017 11:11:39 +0100 Subject: Implement deserializing primitives properly. This finishes the work to upgrade to serde 1.0. Thanks to @kardeiz for starting the work in issue #3. This also clairifies how enums work with serde_qs: only with adjacently tagged enums for the time being. --- examples/introduction.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'examples') diff --git a/examples/introduction.rs b/examples/introduction.rs index 66283d3..a8e984f 100644 --- a/examples/introduction.rs +++ b/examples/introduction.rs @@ -127,4 +127,29 @@ fn main() { user_ids[512]=4"; let params: QueryParams = qs::from_str(encoded).unwrap(); assert_eq!(params, example_params); + + // Enums are supported, but only adjacently tagged enums + // (see https://serde.rs/enum-representations.html for more information). + #[derive(Deserialize, Debug, PartialEq, Serialize)] + #[serde(tag = "type", content = "value")] + enum AdjTaggedEnum { + B(bool), + S(String), + V { id: u8, v: String }, + } + + #[derive(Deserialize, Debug, PartialEq, Serialize)] + struct EnumQuery { + e: AdjTaggedEnum, + } + + let example_params = EnumQuery { + e: AdjTaggedEnum::B(false), + }; + // encodes as: + // "e[type]=B&e[value]=false" + let encoded = qs::to_string(&example_params).unwrap(); + println!("`serde_qs` to_string for enum:\n\t{:?}", encoded); + let params: EnumQuery = qs::from_str(&encoded).unwrap(); + println!("`serde_qs` from_str for enum:\n\t{:?}", params); } -- cgit v1.2.3