11use serde:: Deserializer ;
2- use serde:: de:: { Error , Unexpected , Visitor } ;
2+ use serde:: de:: Visitor ;
33use std:: collections:: HashMap ;
44use std:: fmt:: Formatter ;
55use std:: hash:: BuildHasher ;
@@ -36,23 +36,24 @@ impl<'de, T: From<String>> Visitor<'de> for VectorVisitor<T> {
3636}
3737
3838impl < ' de , S : BuildHasher + Default > Visitor < ' de > for MapVisitor < S > {
39- type Value = HashMap < String , String , S > ;
39+ type Value = HashMap < String , Option < String > , S > ;
4040
4141 fn expecting ( & self , formatter : & mut Formatter ) -> std:: fmt:: Result {
4242 formatter. write_str (
43- "either a sequence, or a comma or newline separated string of key=value entries" ,
43+ "either a sequence, or a comma or newline separated string of key[ =value] entries" ,
4444 )
4545 }
4646
4747 fn visit_str < E : serde:: de:: Error > ( self , value : & str ) -> Result < Self :: Value , E > {
48- value
48+ Ok ( value
4949 . split ( [ '\n' , ',' ] )
5050 . map ( |s| {
51- s. split_once ( '=' )
52- . ok_or_else ( || E :: custom ( format ! ( "key=value expected, found {s}" ) ) )
53- . map ( |( key, value) | ( key. to_owned ( ) , value. to_owned ( ) ) )
51+ match s. split_once ( '=' ) {
52+ Some ( ( key, value) ) => ( key. to_owned ( ) , Some ( value. to_owned ( ) ) ) ,
53+ None => ( s. to_owned ( ) , None ) ,
54+ }
5455 } )
55- . collect ( )
56+ . collect ( ) )
5657 }
5758
5859 fn visit_seq < A > ( self , mut seq : A ) -> Result < Self :: Value , A :: Error >
@@ -61,10 +62,14 @@ impl<'de, S: BuildHasher + Default> Visitor<'de> for MapVisitor<S> {
6162 {
6263 let mut ret = HashMap :: with_hasher ( Default :: default ( ) ) ;
6364 while let Some ( el) = seq. next_element :: < String > ( ) ? {
64- let ( key, value) = el
65- . split_once ( '=' )
66- . ok_or_else ( || A :: Error :: invalid_value ( Unexpected :: Str ( & el) , & self ) ) ?;
67- ret. insert ( key. to_owned ( ) , value. to_owned ( ) ) ;
65+ match el. split_once ( '=' ) {
66+ None => {
67+ ret. insert ( el. to_owned ( ) , None ) ;
68+ }
69+ Some ( ( key, value) ) => {
70+ ret. insert ( key. to_owned ( ) , Some ( value. to_owned ( ) ) ) ;
71+ }
72+ }
6873 }
6974 Ok ( ret)
7075 }
@@ -83,7 +88,7 @@ pub(crate) fn deserialize_newline_or_comma_separated_vec<
8388 deserializer. deserialize_seq ( VectorVisitor ( PhantomData ) )
8489}
8590
86- /// deserialize into a map of `String`s to `String`s either of:
91+ /// deserialize into a map of `String`s to `Option< String> `s either of:
8792/// * a sequence of elements serializable into `String`s, or
8893/// * a single element serializable into `String`, then split on `,` and `\n`
8994pub ( crate ) fn deserialize_newline_or_comma_separated_map <
@@ -92,6 +97,6 @@ pub(crate) fn deserialize_newline_or_comma_separated_map<
9297 S : BuildHasher + Default ,
9398> (
9499 deserializer : D ,
95- ) -> Result < HashMap < String , String , S > , D :: Error > {
100+ ) -> Result < HashMap < String , Option < String > , S > , D :: Error > {
96101 deserializer. deserialize_map ( MapVisitor ( PhantomData ) )
97102}
0 commit comments