@@ -16,17 +16,19 @@ pub(crate) mod utils;
1616use catalyst_types:: { problem_report:: ProblemReport , uuid:: UuidV7 } ;
1717pub use content_encoding:: ContentEncoding ;
1818pub use content_type:: ContentType ;
19- use coset:: CborSerializable ;
2019pub use doc_type:: DocType ;
2120pub use document_refs:: { DocLocator , DocumentRef , DocumentRefs } ;
22- use minicbor:: { Decode , Decoder } ;
21+ use minicbor:: Decoder ;
2322pub use section:: Section ;
2423use strum:: IntoDiscriminant as _;
2524use utils:: { cose_protected_header_find, decode_document_field_from_protected_header, CborUuidV7 } ;
2625
2726use crate :: {
2827 decode_context:: DecodeContext ,
29- metadata:: supported_field:: { SupportedField , SupportedLabel } ,
28+ metadata:: {
29+ supported_field:: { SupportedField , SupportedLabel } ,
30+ utils:: decode_cose_protected_header_value,
31+ } ,
3032} ;
3133
3234/// `content_encoding` field COSE key value
@@ -122,29 +124,26 @@ impl Metadata {
122124
123125 /// Return `ref` field.
124126 #[ must_use]
125- pub fn doc_ref ( & self ) -> Option < DocumentRef > {
127+ pub fn doc_ref ( & self ) -> Option < & DocumentRefs > {
126128 self . 0
127129 . get ( & SupportedLabel :: Ref )
128130 . and_then ( SupportedField :: try_as_ref_ref)
129- . copied ( )
130131 }
131132
132133 /// Return `template` field.
133134 #[ must_use]
134- pub fn template ( & self ) -> Option < DocumentRef > {
135+ pub fn template ( & self ) -> Option < & DocumentRefs > {
135136 self . 0
136137 . get ( & SupportedLabel :: Template )
137138 . and_then ( SupportedField :: try_as_template_ref)
138- . copied ( )
139139 }
140140
141141 /// Return `reply` field.
142142 #[ must_use]
143- pub fn reply ( & self ) -> Option < DocumentRef > {
143+ pub fn reply ( & self ) -> Option < & DocumentRefs > {
144144 self . 0
145145 . get ( & SupportedLabel :: Reply )
146146 . and_then ( SupportedField :: try_as_reply_ref)
147- . copied ( )
148147 }
149148
150149 /// Return `section` field.
@@ -166,11 +165,10 @@ impl Metadata {
166165
167166 /// Return `parameters` field.
168167 #[ must_use]
169- pub fn parameters ( & self ) -> Option < DocumentRef > {
168+ pub fn parameters ( & self ) -> Option < & DocumentRefs > {
170169 self . 0
171170 . get ( & SupportedLabel :: Parameters )
172171 . and_then ( SupportedField :: try_as_parameters_ref)
173- . copied ( )
174172 }
175173
176174 /// Build `Metadata` object from the metadata fields, doing all necessary validation.
@@ -266,20 +264,6 @@ impl Metadata {
266264 }
267265 }
268266
269- if let Some ( value) = cose_protected_header_find (
270- protected,
271- |key| matches ! ( key, coset:: Label :: Text ( label) if label. eq_ignore_ascii_case( TYPE_KEY ) ) ,
272- )
273- . and_then ( |value| {
274- DocType :: decode (
275- & mut Decoder :: new ( & value. clone ( ) . to_vec ( ) . unwrap_or_default ( ) ) ,
276- context,
277- )
278- . ok ( )
279- } ) {
280- metadata_fields. push ( SupportedField :: Type ( value) ) ;
281- }
282-
283267 if let Some ( value) = decode_document_field_from_protected_header :: < CborUuidV7 > (
284268 protected,
285269 ID_KEY ,
@@ -302,30 +286,30 @@ impl Metadata {
302286 metadata_fields. push ( SupportedField :: Ver ( value) ) ;
303287 }
304288
305- if let Some ( value) = decode_document_field_from_protected_header (
306- protected,
307- REF_KEY ,
308- COSE_DECODING_CONTEXT ,
309- context. report ,
289+ // DocType and DocRef now using minicbor decoding.
290+ if let Some ( value) = decode_cose_protected_header_value :: < DecodeContext , DocType > (
291+ protected, context, TYPE_KEY ,
292+ ) {
293+ metadata_fields. push ( SupportedField :: Type ( value) ) ;
294+ } ;
295+ if let Some ( value) = decode_cose_protected_header_value :: < DecodeContext , DocumentRefs > (
296+ protected, context, REF_KEY ,
310297 ) {
311298 metadata_fields. push ( SupportedField :: Ref ( value) ) ;
312- }
313- if let Some ( value) = decode_document_field_from_protected_header (
299+ } ;
300+ if let Some ( value) = decode_cose_protected_header_value :: < DecodeContext , DocumentRefs > (
314301 protected,
302+ context,
315303 TEMPLATE_KEY ,
316- COSE_DECODING_CONTEXT ,
317- context. report ,
318304 ) {
319305 metadata_fields. push ( SupportedField :: Template ( value) ) ;
320306 }
321- if let Some ( value) = decode_document_field_from_protected_header (
322- protected,
323- REPLY_KEY ,
324- COSE_DECODING_CONTEXT ,
325- context. report ,
307+ if let Some ( value) = decode_cose_protected_header_value :: < DecodeContext , DocumentRefs > (
308+ protected, context, REPLY_KEY ,
326309 ) {
327310 metadata_fields. push ( SupportedField :: Reply ( value) ) ;
328311 }
312+
329313 if let Some ( value) = decode_document_field_from_protected_header (
330314 protected,
331315 SECTION_KEY ,
@@ -343,20 +327,15 @@ impl Metadata {
343327 CATEGORY_ID_KEY ,
344328 ]
345329 . iter ( )
346- . filter_map ( |field_name| -> Option < DocumentRef > {
347- decode_document_field_from_protected_header (
348- protected,
349- field_name,
350- COSE_DECODING_CONTEXT ,
351- context. report ,
352- )
330+ . filter_map ( |field_name| -> Option < DocumentRefs > {
331+ decode_cose_protected_header_value ( protected, context, field_name)
353332 } )
354333 . fold ( ( None , false ) , |( res, _) , v| ( Some ( v) , res. is_some ( ) ) ) ;
355334 if has_multiple_fields {
356335 context. report . duplicate_field (
357- "brand_id, campaign_id, category_id " ,
358- "Only value at the same time is allowed parameters, brand_id, campaign_id, category_id" ,
359- "Validation of parameters field aliases"
336+ "Parameters field " ,
337+ "Only one parameter can be used at a time: either brand_id, campaign_id, category_id" ,
338+ COSE_DECODING_CONTEXT
360339 ) ;
361340 }
362341 if let Some ( value) = parameters {
0 commit comments