Debian Codegraph @

third_party/rust/serde_with/tests/serde_as/pickfirst.rs

raw
1 use super::*;
2 use serde_with::{formats::SpaceSeparator, PickFirst};
3
4 #[test]
5 fn test_pick_first_two() {
6 #[serde_as]
7 #[derive(Debug, Serialize, Deserialize, PartialEq)]
8 struct S(#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")] u32);
9
10 is_equal(S(123), expect![[r#"123"#]]);
11 check_deserialization(S(123), r#""123""#);
12 check_error_deserialization::<S>(
13 r#""Abc""#,
14 expect![[r#"
15 PickFirst could not deserialize any variant:
16 First: invalid type: string "Abc", expected u32
17 Second: invalid digit found in string"#]],
18 );
19
20 #[serde_as]
21 #[derive(Debug, Serialize, Deserialize, PartialEq)]
22 struct S2(#[serde_as(as = "PickFirst<(DisplayFromStr, _)>")] u32);
23
24 is_equal(S2(123), expect![[r#""123""#]]);
25 check_deserialization(S2(123), r#"123"#);
26
27 #[serde_as]
28 #[derive(Debug, Serialize, Deserialize, PartialEq)]
29 struct S3(
30 #[serde_as(as = "PickFirst<(_, StringWithSeparator::<SpaceSeparator, String>,)>")]
31 Vec<String>,
32 );
33 is_equal(
34 S3(vec!["A".to_string(), "B".to_string(), "C".to_string()]),
35 expect![[r#"
36 [
37 "A",
38 "B",
39 "C"
40 ]"#]],
41 );
42 check_deserialization(
43 S3(vec!["A".to_string(), "B".to_string(), "C".to_string()]),
44 r#""A B C""#,
45 );
46
47 #[serde_as]
48 #[derive(Debug, Serialize, Deserialize, PartialEq)]
49 struct S4(
50 #[serde_as(as = "PickFirst<(StringWithSeparator::<CommaSeparator, String>, _,)>")]
51 Vec<String>,
52 );
53 is_equal(
54 S4(vec!["A".to_string(), "B".to_string(), "C".to_string()]),
55 expect![[r#""A,B,C""#]],
56 );
57 check_deserialization(
58 S4(vec!["A".to_string(), "B".to_string(), "C".to_string()]),
59 r#"["A", "B", "C"]"#,
60 );
61 }
62
63 #[test]
64 fn test_pick_first_three() {
65 #[serde_as]
66 #[derive(Debug, Serialize, Deserialize, PartialEq)]
67 struct S(
68 #[serde_as(
69 as = "PickFirst<(_, Vec<DisplayFromStr>, StringWithSeparator::<CommaSeparator, u32>)>"
70 )]
71 Vec<u32>,
72 );
73 is_equal(
74 S(vec![1, 2, 3]),
75 expect![[r#"
76 [
77 1,
78 2,
79 3
80 ]"#]],
81 );
82 check_deserialization(
83 S(vec![1, 2, 3]),
84 r#"
85 [
86 "1",
87 "2",
88 "3"
89 ]"#,
90 );
91 check_deserialization(S(vec![1, 2, 3]), r#""1,2,3""#);
92 check_error_deserialization::<S>(
93 r#""Abc""#,
94 expect![[r#"
95 PickFirst could not deserialize any variant:
96 First: invalid type: string "Abc", expected a sequence
97 Second: invalid type: string "Abc", expected a sequence
98 Third: invalid digit found in string"#]],
99 );
100
101 #[serde_as]
102 #[derive(Debug, Serialize, Deserialize, PartialEq)]
103 struct S2(
104 #[serde_as(
105 as = "PickFirst<(StringWithSeparator::<CommaSeparator, u32>, _, Vec<DisplayFromStr>)>"
106 )]
107 Vec<u32>,
108 );
109 is_equal(S2(vec![1, 2, 3]), expect![[r#""1,2,3""#]]);
110 check_deserialization(
111 S2(vec![1, 2, 3]),
112 r#"
113 [
114 "1",
115 "2",
116 "3"
117 ]"#,
118 );
119 check_deserialization(
120 S2(vec![1, 2, 3]),
121 r#"
122 [
123 1,
124 2,
125 3
126 ]"#,
127 );
128 }
129
130 #[test]
131 fn test_pick_first_four() {
132 #[serde_as]
133 #[derive(Debug, Serialize, Deserialize, PartialEq)]
134 struct S(#[serde_as(as = "PickFirst<(_, _, _, _)>")] u32);
135
136 is_equal(S(123), expect![[r#"123"#]]);
137 check_error_deserialization::<S>(
138 r#""Abc""#,
139 expect![[r#"
140 PickFirst could not deserialize any variant:
141 First: invalid type: string "Abc", expected u32
142 Second: invalid type: string "Abc", expected u32
143 Third: invalid type: string "Abc", expected u32
144 Fourth: invalid type: string "Abc", expected u32"#]],
145 );
146 }