#[cfg(test)] mod test { use serde::Deserialize; #[test] fn test_deserialize_flat() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct SearchParams { keyword: Option, limit: Option, } let expected = SearchParams { keyword: Some(String::from("foo")), limit: Some(20), }; let source = "keyword=foo&limit=20"; let actual: SearchParams = serde_structuredqs::from_str(source).unwrap(); assert_eq!(actual, expected) } #[test] fn test_deserialize_structured() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct SearchParams { keyword: Option, limit: Option, filter: Option, } #[derive(Debug, Deserialize, Eq, PartialEq)] struct FilteringParameter { category: Option, difficulty: Option>, } #[derive(Debug, Deserialize, Eq, PartialEq)] struct RangeFilteringParameter { from: Option, to: Option, } let expected = SearchParams { keyword: Some(String::from("foo")), limit: Some(20), filter: Some(FilteringParameter { category: Some(String::from("A")), difficulty: Some(RangeFilteringParameter { from: None, to: Some(800), }), }), }; let source = "keyword=foo&limit=20&filter.category=A&filter.difficulty.to=800"; let actual: SearchParams = serde_structuredqs::from_str(source).unwrap(); assert_eq!(actual, expected) } #[test] fn deserialize_vec() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct MyStruct { a: Vec, b: Option>, c: Vec, } let expected = MyStruct { a: vec![ String::from("foo"), String::from("bar"), String::from("baz"), ], b: Some(vec![String::from("foo")]), c: vec![100, 200], }; let source = "a=foo,bar,baz&b=foo&c=100,200"; let actual: MyStruct = serde_structuredqs::from_str(source).unwrap(); assert_eq!(actual, expected) } #[test] fn deserialize_vec_with_unwise_comma_separated() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct MyStruct { a: Vec, } let expected = MyStruct { a: vec![ String::from("foo"), String::from("bar"), String::from("baz"), ], }; let actual: MyStruct = serde_structuredqs::from_str("a=,,,,foo,,,,,,bar,baz,,,,").unwrap(); assert_eq!(actual, expected) } #[test] fn deserialize_hollow_vec() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct MyStruct { a: Vec, b: Option>, } let expected = MyStruct { a: vec![], b: Some(vec![]), }; let actual: MyStruct = serde_structuredqs::from_str("a=,,,,,&b=,,,,").unwrap(); assert_eq!(actual, expected) } #[test] fn deserialize_empty_vec() { #[derive(Debug, Deserialize, Eq, PartialEq)] struct MyStruct { a: Vec, b: Option>, } let expected = MyStruct { a: vec![], b: None }; let actual: MyStruct = serde_structuredqs::from_str("a=&b=").unwrap(); assert_eq!(actual, expected) } }