11use crate :: error:: { HttpSigError , HttpSigResult } ;
2- use sfv:: { Parser , SerializeValue } ;
2+ use sfv:: { FieldType , Parser } ;
33
44type IndexSet < K > = indexmap:: IndexSet < K , rustc_hash:: FxBuildHasher > ;
55
@@ -46,13 +46,13 @@ impl TryFrom<(&str, &sfv::BareItem)> for HttpMessageComponentParam {
4646 "tr" => Ok ( Self :: Tr ) ,
4747 "req" => Ok ( Self :: Req ) ,
4848 "name" => {
49- let name = val. as_str ( ) . ok_or ( HttpSigError :: InvalidComponentParam (
49+ let name = val. as_string ( ) . ok_or ( HttpSigError :: InvalidComponentParam (
5050 "Invalid http field param: name" . to_string ( ) ,
5151 ) ) ?;
5252 Ok ( Self :: Name ( name. to_string ( ) ) )
5353 }
5454 "key" => {
55- let key = val. as_str ( ) . ok_or ( HttpSigError :: InvalidComponentParam (
55+ let key = val. as_string ( ) . ok_or ( HttpSigError :: InvalidComponentParam (
5656 "Invalid http field param: key" . to_string ( ) ,
5757 ) ) ?;
5858 Ok ( Self :: Key ( key. to_string ( ) ) )
@@ -106,10 +106,10 @@ pub(super) fn handle_params_sf(field_values: &mut [String]) -> HttpSigResult<()>
106106 let parsed_list = field_values
107107 . iter ( )
108108 . map ( |v| {
109- if let Ok ( list) = Parser :: parse_list ( v . as_bytes ( ) ) {
110- list. serialize_value ( )
111- } else if let Ok ( dict) = Parser :: parse_dictionary ( v . as_bytes ( ) ) {
112- dict. serialize_value ( )
109+ if let Ok ( list) = Parser :: new ( v ) . parse :: < sfv :: List > ( ) {
110+ list. serialize ( ) . ok_or ( "Failed to serialize structured field value for sf" )
111+ } else if let Ok ( dict) = Parser :: new ( v ) . parse :: < sfv :: Dictionary > ( ) {
112+ dict. serialize ( ) . ok_or ( "Failed to serialize structured field value for sf" )
113113 } else {
114114 Err ( "invalid structured field value for sf" )
115115 }
@@ -129,7 +129,8 @@ pub(super) fn handle_params_sf(field_values: &mut [String]) -> HttpSigResult<()>
129129pub ( super ) fn handle_params_key_into ( field_values : & [ String ] , key : & str ) -> HttpSigResult < Vec < String > > {
130130 let dicts = field_values
131131 . iter ( )
132- . map ( |v| Parser :: parse_dictionary ( v. as_bytes ( ) ) )
132+ . map ( |v| Parser :: new ( v. as_str ( ) ) . parse ( ) as Result < sfv:: Dictionary , _ > )
133+ // Parser::parse_dictionary(v.as_bytes()))
133134 . collect :: < Result < Vec < _ > , _ > > ( )
134135 . map_err ( |e| HttpSigError :: InvalidComponentParam ( format ! ( "Failed to parse structured field value: {e}" ) ) ) ?;
135136
@@ -138,11 +139,12 @@ pub(super) fn handle_params_key_into(field_values: &[String], key: &str) -> Http
138139 . filter_map ( |dict| {
139140 dict. get ( key) . map ( |v| {
140141 let sfvalue: sfv:: List = vec ! [ v. clone( ) ] ;
141- sfvalue. serialize_value ( )
142+ // sfvalue.serialize_value()
143+ sfvalue. serialize ( )
142144 } )
143145 } )
144- . collect :: < Result < Vec < _ > , _ > > ( )
145- . map_err ( |e | HttpSigError :: InvalidComponentParam ( format ! ( "Failed to serialize structured field value: {e} " ) ) ) ?;
146+ . collect :: < Option < Vec < _ > > > ( )
147+ . ok_or_else ( | | HttpSigError :: InvalidComponentParam ( format ! ( "Failed to serialize structured field value" ) ) ) ?;
146148
147149 Ok ( found_entries)
148150}
@@ -157,19 +159,19 @@ mod tests {
157159 fn parser_test ( ) {
158160 // Parsing structured field value of Item type.
159161 let item_header_input = "12.445;foo=bar" ;
160- let item = Parser :: parse_item ( item_header_input. as_bytes ( ) ) . unwrap ( ) ;
161- assert_eq ! ( item. serialize_value ( ) . unwrap ( ) , item_header_input) ;
162+ let item = Parser :: new ( item_header_input) . parse :: < sfv :: Item > ( ) . unwrap ( ) ;
163+ assert_eq ! ( item. serialize ( ) , item_header_input) ;
162164
163165 // Parsing structured field value of List type.
164166 let list_header_input = " 1; a=tok, (\" foo\" \" bar\" );baz, ( )" ;
165- let list = Parser :: parse_list ( list_header_input. as_bytes ( ) ) . unwrap ( ) ;
166- assert_eq ! ( list. serialize_value ( ) . unwrap( ) , "1;a=tok, (\" foo\" \" bar\" );baz, ()" ) ;
167+ let list = Parser :: new ( list_header_input) . parse :: < sfv :: List > ( ) . unwrap ( ) ;
168+ assert_eq ! ( list. serialize ( ) . unwrap( ) , "1;a=tok, (\" foo\" \" bar\" );baz, ()" ) ;
167169
168170 // Parsing structured field value of Dictionary type.
169171 let dict_header_input = "a=?0, b, c; foo=bar, rating=1.5, fruits=(apple pear), d" ;
170- let dict = Parser :: parse_dictionary ( dict_header_input. as_bytes ( ) ) . unwrap ( ) ;
172+ let dict = Parser :: new ( dict_header_input) . parse :: < sfv :: Dictionary > ( ) . unwrap ( ) ;
171173 assert_eq ! (
172- dict. serialize_value ( ) . unwrap( ) ,
174+ dict. serialize ( ) . unwrap( ) ,
173175 "a=?0, b, c;foo=bar, rating=1.5, fruits=(apple pear), d"
174176 ) ;
175177 }
0 commit comments