summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSam Scott <sam.scott89@gmail.com>2017-04-25 11:11:39 +0100
committerSam Scott <sam.scott89@gmail.com>2017-04-25 11:14:43 +0100
commit7f4764dd6b63c0c0cf0d27268dfa812c0f2334fc (patch)
tree7196fad8b398c5e235bf149739285c9102d02759 /examples
parent53b0c87687fc6dd2f8bca5c4277a41e3e281025d (diff)
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/introduction.rs25
1 files changed, 25 insertions, 0 deletions
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);
}