@@ -70,6 +70,13 @@ impl FromStr for Priority {
70
70
}
71
71
}
72
72
73
+ #[ derive( Deserialize ) ]
74
+ #[ serde( untagged) ]
75
+ enum NumberOrString {
76
+ Number ( u8 ) ,
77
+ String ( String ) ,
78
+ }
79
+
73
80
impl Serialize for Priority {
74
81
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
75
82
where
@@ -86,7 +93,65 @@ impl<'de> Deserialize<'de> for Priority {
86
93
where
87
94
D : Deserializer < ' de > ,
88
95
{
89
- let priority: u8 = u8:: deserialize ( deserializer) ?;
90
- Priority :: from_u8 ( priority) . map_err ( de:: Error :: custom)
96
+ match NumberOrString :: deserialize ( deserializer) ? {
97
+ NumberOrString :: Number ( priority) => {
98
+ Priority :: from_u8 ( priority) . map_err ( de:: Error :: custom)
99
+ }
100
+ NumberOrString :: String ( priority) => {
101
+ Self :: from_str ( & priority) . map_err ( de:: Error :: custom)
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ #[ cfg( test) ]
108
+ mod tests {
109
+ use super :: * ;
110
+
111
+ #[ test]
112
+ fn test_deserialize ( ) {
113
+ let json = serde_json:: json!( 5 ) ;
114
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
115
+ assert_eq ! ( priority, Priority :: Max ) ;
116
+
117
+ let json = serde_json:: json!( 4 ) ;
118
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
119
+ assert_eq ! ( priority, Priority :: High ) ;
120
+
121
+ let json = serde_json:: json!( 3 ) ;
122
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
123
+ assert_eq ! ( priority, Priority :: Default ) ;
124
+
125
+ let json = serde_json:: json!( 2 ) ;
126
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
127
+ assert_eq ! ( priority, Priority :: Low ) ;
128
+
129
+ let json = serde_json:: json!( 1 ) ;
130
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
131
+ assert_eq ! ( priority, Priority :: Min ) ;
132
+
133
+ let json = serde_json:: json!( "urgent" ) ;
134
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
135
+ assert_eq ! ( priority, Priority :: Max ) ;
136
+
137
+ let json = serde_json:: json!( "max" ) ;
138
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
139
+ assert_eq ! ( priority, Priority :: Max ) ;
140
+
141
+ let json = serde_json:: json!( "high" ) ;
142
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
143
+ assert_eq ! ( priority, Priority :: High ) ;
144
+
145
+ let json = serde_json:: json!( "default" ) ;
146
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
147
+ assert_eq ! ( priority, Priority :: Default ) ;
148
+
149
+ let json = serde_json:: json!( "low" ) ;
150
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
151
+ assert_eq ! ( priority, Priority :: Low ) ;
152
+
153
+ let json = serde_json:: json!( "min" ) ;
154
+ let priority: Priority = serde_json:: from_value ( json) . unwrap ( ) ;
155
+ assert_eq ! ( priority, Priority :: Min ) ;
91
156
}
92
157
}
0 commit comments