11//! Catalyst Signed Document Extra Fields.
22
3- use catalyst_types:: { problem_report:: ProblemReport , uuid :: UuidV4 } ;
3+ use catalyst_types:: problem_report:: ProblemReport ;
44use coset:: { cbor:: Value , Label , ProtectedHeader } ;
55
66use super :: {
7- cose_protected_header_find,
8- utils:: { decode_document_field_from_protected_header, CborUuidV4 } ,
9- DocumentRef , Section ,
7+ cose_protected_header_find, utils:: decode_document_field_from_protected_header, DocumentRef ,
8+ Section ,
109} ;
1110
1211/// `ref` field COSE key value
@@ -19,13 +18,13 @@ const REPLY_KEY: &str = "reply";
1918const SECTION_KEY : & str = "section" ;
2019/// `collabs` field COSE key value
2120const COLLABS_KEY : & str = "collabs" ;
22- /// `brand_id` field COSE key value
21+ /// `parameters` field COSE key value
22+ const PARAMETERS_KEY : & str = "parameters" ;
23+ /// `brand_id` field COSE key value (alias of the `parameters` field)
2324const BRAND_ID_KEY : & str = "brand_id" ;
24- /// `campaign_id` field COSE key value
25+ /// `campaign_id` field COSE key value (alias of the `parameters` field)
2526const CAMPAIGN_ID_KEY : & str = "campaign_id" ;
26- /// `election_id` field COSE key value
27- const ELECTION_ID_KEY : & str = "election_id" ;
28- /// `category_id` field COSE key value
27+ /// `category_id` field COSE key value (alias of the `parameters` field)
2928const CATEGORY_ID_KEY : & str = "category_id" ;
3029
3130/// Extra Metadata Fields.
@@ -48,18 +47,9 @@ pub struct ExtraFields {
4847 /// Reference to the document collaborators. Collaborator type is TBD.
4948 #[ serde( default = "Vec::new" , skip_serializing_if = "Vec::is_empty" ) ]
5049 collabs : Vec < String > ,
51- /// Unique identifier for the brand that is running the voting .
50+ /// Reference to the parameters document .
5251 #[ serde( skip_serializing_if = "Option::is_none" ) ]
53- brand_id : Option < DocumentRef > ,
54- /// Unique identifier for the campaign of voting.
55- #[ serde( skip_serializing_if = "Option::is_none" ) ]
56- campaign_id : Option < DocumentRef > ,
57- /// Unique identifier for the election.
58- #[ serde( skip_serializing_if = "Option::is_none" ) ]
59- election_id : Option < UuidV4 > ,
60- /// Unique identifier for the voting category as a collection of proposals.
61- #[ serde( skip_serializing_if = "Option::is_none" ) ]
62- category_id : Option < DocumentRef > ,
52+ parameters : Option < DocumentRef > ,
6353}
6454
6555impl ExtraFields {
@@ -93,28 +83,10 @@ impl ExtraFields {
9383 & self . collabs
9484 }
9585
96- /// Return `brand_id` field.
97- #[ must_use]
98- pub fn brand_id ( & self ) -> Option < DocumentRef > {
99- self . brand_id
100- }
101-
102- /// Return `campaign_id` field.
103- #[ must_use]
104- pub fn campaign_id ( & self ) -> Option < DocumentRef > {
105- self . campaign_id
106- }
107-
108- /// Return `election_id` field.
86+ /// Return `parameters` field.
10987 #[ must_use]
110- pub fn election_id ( & self ) -> Option < UuidV4 > {
111- self . election_id
112- }
113-
114- /// Return `category_id` field.
115- #[ must_use]
116- pub fn category_id ( & self ) -> Option < DocumentRef > {
117- self . category_id
88+ pub fn parameters ( & self ) -> Option < DocumentRef > {
89+ self . parameters
11890 }
11991
12092 /// Fill the COSE header `ExtraFields` data into the header builder.
@@ -141,26 +113,11 @@ impl ExtraFields {
141113 Value :: Array ( self . collabs . iter ( ) . cloned ( ) . map ( Value :: Text ) . collect ( ) ) ,
142114 ) ;
143115 }
144- if let Some ( brand_id) = & self . brand_id {
145- builder = builder. text_value ( BRAND_ID_KEY . to_string ( ) , Value :: try_from ( * brand_id) ?) ;
146- }
147116
148- if let Some ( campaign_id) = & self . campaign_id {
149- builder =
150- builder. text_value ( CAMPAIGN_ID_KEY . to_string ( ) , Value :: try_from ( * campaign_id) ?) ;
117+ if let Some ( parameters) = & self . parameters {
118+ builder = builder. text_value ( PARAMETERS_KEY . to_string ( ) , Value :: try_from ( * parameters) ?) ;
151119 }
152120
153- if let Some ( election_id) = & self . election_id {
154- builder = builder. text_value (
155- ELECTION_ID_KEY . to_string ( ) ,
156- Value :: try_from ( CborUuidV4 ( * election_id) ) ?,
157- ) ;
158- }
159-
160- if let Some ( category_id) = & self . category_id {
161- builder =
162- builder. text_value ( CATEGORY_ID_KEY . to_string ( ) , Value :: try_from ( * category_id) ?) ;
163- }
164121 Ok ( builder)
165122 }
166123
@@ -196,41 +153,38 @@ impl ExtraFields {
196153 COSE_DECODING_CONTEXT ,
197154 error_report,
198155 ) ;
199- let brand_id = decode_document_field_from_protected_header (
200- protected,
156+
157+ // process `parameters` field and all its aliases
158+ let ( parameters, has_multiple_fields) = [
159+ PARAMETERS_KEY ,
201160 BRAND_ID_KEY ,
202- COSE_DECODING_CONTEXT ,
203- error_report,
204- ) ;
205- let campaign_id = decode_document_field_from_protected_header (
206- protected,
207161 CAMPAIGN_ID_KEY ,
208- COSE_DECODING_CONTEXT ,
209- error_report,
210- ) ;
211- let election_id = decode_document_field_from_protected_header :: < CborUuidV4 > (
212- protected,
213- ELECTION_ID_KEY ,
214- COSE_DECODING_CONTEXT ,
215- error_report,
216- )
217- . map ( |v| v. 0 ) ;
218- let category_id = decode_document_field_from_protected_header (
219- protected,
220162 CATEGORY_ID_KEY ,
221- COSE_DECODING_CONTEXT ,
222- error_report,
223- ) ;
163+ ]
164+ . iter ( )
165+ . filter_map ( |field_name| -> Option < DocumentRef > {
166+ decode_document_field_from_protected_header (
167+ protected,
168+ field_name,
169+ COSE_DECODING_CONTEXT ,
170+ error_report,
171+ )
172+ } )
173+ . fold ( ( None , false ) , |( res, _) , v| ( Some ( v) , res. is_some ( ) ) ) ;
174+ if has_multiple_fields {
175+ error_report. duplicate_field (
176+ "brand_id, campaign_id, category_id" ,
177+ "Only value at the same time is allowed parameters, brand_id, campaign_id, category_id" ,
178+ "Validation of parameters field aliases"
179+ ) ;
180+ }
224181
225182 let mut extra = ExtraFields {
226183 doc_ref,
227184 template,
228185 reply,
229186 section,
230- brand_id,
231- campaign_id,
232- election_id,
233- category_id,
187+ parameters,
234188 ..Default :: default ( )
235189 } ;
236190
0 commit comments