|
3 | 3 | use catalyst_types::{problem_report::ProblemReport, uuid::UuidV4}; |
4 | 4 | use coset::{cbor::Value, Label, ProtectedHeader}; |
5 | 5 |
|
6 | | -use super::{cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid, DocumentRef}; |
| 6 | +use super::{cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid, DocumentRef, Section}; |
7 | 7 |
|
8 | 8 | /// `ref` field COSE key value |
9 | 9 | const REF_KEY: &str = "ref"; |
@@ -40,7 +40,7 @@ pub struct ExtraFields { |
40 | 40 | reply: Option<DocumentRef>, |
41 | 41 | /// Reference to the document section. |
42 | 42 | #[serde(skip_serializing_if = "Option::is_none")] |
43 | | - section: Option<String>, |
| 43 | + section: Option<Section>, |
44 | 44 | /// Reference to the document collaborators. Collaborator type is TBD. |
45 | 45 | #[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")] |
46 | 46 | collabs: Vec<String>, |
@@ -79,7 +79,7 @@ impl ExtraFields { |
79 | 79 |
|
80 | 80 | /// Return `section` field. |
81 | 81 | #[must_use] |
82 | | - pub fn section(&self) -> Option<&String> { |
| 82 | + pub fn section(&self) -> Option<&Section> { |
83 | 83 | self.section.as_ref() |
84 | 84 | } |
85 | 85 |
|
@@ -128,7 +128,7 @@ impl ExtraFields { |
128 | 128 | } |
129 | 129 |
|
130 | 130 | if let Some(section) = &self.section { |
131 | | - builder = builder.text_value(SECTION_KEY.to_string(), Value::Text(section.clone())); |
| 131 | + builder = builder.text_value(SECTION_KEY.to_string(), Value::from(section.clone())); |
132 | 132 | } |
133 | 133 |
|
134 | 134 | if !self.collabs.is_empty() { |
@@ -171,178 +171,151 @@ impl ExtraFields { |
171 | 171 | if let Some(cbor_doc_ref) = |
172 | 172 | cose_protected_header_find(protected, |key| key == &Label::Text(REF_KEY.to_string())) |
173 | 173 | { |
174 | | - match DocumentRef::try_from(cbor_doc_ref) { |
175 | | - Ok(doc_ref) => { |
176 | | - extra.doc_ref = Some(doc_ref); |
177 | | - }, |
178 | | - Err(e) => { |
179 | | - error_report.conversion_error( |
180 | | - "CBOR COSE protected header doc ref", |
181 | | - &format!("{cbor_doc_ref:?}"), |
182 | | - &format!("Expected DocumentRef: {e}"), |
183 | | - &format!("{CONTEXT}, DocumentRef"), |
184 | | - ); |
185 | | - }, |
186 | | - } |
| 174 | + if let Ok(doc_ref) = DocumentRef::try_from(cbor_doc_ref) { |
| 175 | + extra.doc_ref = Some(doc_ref); |
| 176 | + } else { |
| 177 | + error_report.conversion_error( |
| 178 | + "CBOR COSE protected header doc ref", |
| 179 | + &format!("{cbor_doc_ref:?}"), |
| 180 | + &format!("Expected a CBOR DocumentRef"), |
| 181 | + &format!("{CONTEXT}, DocumentRef"), |
| 182 | + ); |
| 183 | + }; |
187 | 184 | } |
188 | 185 |
|
189 | 186 | if let Some(cbor_doc_template) = cose_protected_header_find(protected, |key| { |
190 | 187 | key == &Label::Text(TEMPLATE_KEY.to_string()) |
191 | 188 | }) { |
192 | | - match DocumentRef::try_from(cbor_doc_template) { |
193 | | - Ok(doc_template) => { |
194 | | - extra.template = Some(doc_template); |
195 | | - }, |
196 | | - Err(e) => { |
197 | | - error_report.conversion_error( |
198 | | - "CBOR COSE protected header document template", |
199 | | - &format!("{cbor_doc_template:?}"), |
200 | | - &format!("Expected DocumentRef: {e}"), |
201 | | - &format!("{CONTEXT}, DocumentRef"), |
202 | | - ); |
203 | | - }, |
| 189 | + if let Ok(doc_template) = DocumentRef::try_from(cbor_doc_template) { |
| 190 | + extra.template = Some(doc_template); |
| 191 | + } else { |
| 192 | + error_report.conversion_error( |
| 193 | + "CBOR COSE protected header document template", |
| 194 | + &format!("{cbor_doc_template:?}"), |
| 195 | + &format!("Expected a CBOR DocumentRef"), |
| 196 | + &format!("{CONTEXT}, DocumentRef"), |
| 197 | + ); |
204 | 198 | } |
205 | 199 | } |
206 | 200 |
|
207 | 201 | if let Some(cbor_doc_reply) = |
208 | 202 | cose_protected_header_find(protected, |key| key == &Label::Text(REPLY_KEY.to_string())) |
209 | 203 | { |
210 | | - match DocumentRef::try_from(cbor_doc_reply) { |
211 | | - Ok(doc_reply) => { |
212 | | - extra.reply = Some(doc_reply); |
213 | | - }, |
214 | | - Err(e) => { |
215 | | - error_report.conversion_error( |
216 | | - "CBOR COSE protected header document reply", |
217 | | - &format!("{cbor_doc_reply:?}"), |
218 | | - &format!("Expected DocumentRef: {e}"), |
219 | | - &format!("{CONTEXT}, DocumentRef"), |
220 | | - ); |
221 | | - }, |
| 204 | + if let Ok(doc_reply) = DocumentRef::try_from(cbor_doc_reply) { |
| 205 | + extra.reply = Some(doc_reply); |
| 206 | + } else { |
| 207 | + error_report.conversion_error( |
| 208 | + "CBOR COSE protected header document reply", |
| 209 | + &format!("{cbor_doc_reply:?}"), |
| 210 | + &format!("Expected a CBOR DocumentRef"), |
| 211 | + &format!("{CONTEXT}, DocumentRef"), |
| 212 | + ); |
222 | 213 | } |
223 | 214 | } |
224 | 215 |
|
225 | 216 | if let Some(cbor_doc_section) = cose_protected_header_find(protected, |key| { |
226 | 217 | key == &Label::Text(SECTION_KEY.to_string()) |
227 | 218 | }) { |
228 | | - match cbor_doc_section.clone().into_text() { |
229 | | - Ok(doc_section) => { |
230 | | - extra.section = Some(doc_section); |
231 | | - }, |
232 | | - Err(e) => { |
233 | | - error_report.conversion_error( |
234 | | - "COSE protected header document section", |
235 | | - &format!("{cbor_doc_section:?}"), |
236 | | - &format!("Expected String: {e:?}"), |
237 | | - &format!("{CONTEXT}, converting document section to String"), |
238 | | - ); |
239 | | - }, |
| 219 | + if let Ok(section) = Section::try_from(cbor_doc_section) { |
| 220 | + extra.section = Some(section); |
| 221 | + } else { |
| 222 | + error_report.conversion_error( |
| 223 | + "COSE protected header document section", |
| 224 | + &format!("{cbor_doc_section:?}"), |
| 225 | + &format!("Must be a valid CBOR encoded String JSON Path"), |
| 226 | + &format!("{CONTEXT}, converting document section to String JSON Path"), |
| 227 | + ); |
240 | 228 | } |
241 | 229 | } |
242 | 230 |
|
243 | 231 | if let Some(cbor_doc_collabs) = cose_protected_header_find(protected, |key| { |
244 | 232 | key == &Label::Text(COLLABS_KEY.to_string()) |
245 | 233 | }) { |
246 | | - match cbor_doc_collabs.clone().into_array() { |
247 | | - Ok(collabs) => { |
248 | | - let mut c = Vec::new(); |
249 | | - for (ids, collaborator) in collabs.iter().cloned().enumerate() { |
250 | | - match collaborator.clone().into_text() { |
251 | | - Ok(collaborator) => { |
252 | | - c.push(collaborator); |
253 | | - }, |
254 | | - Err(e) => { |
255 | | - error_report.conversion_error( |
256 | | - &format!("COSE protected header collaborator index {ids}"), |
257 | | - &format!("{collaborator:?}"), |
258 | | - &format!("Expected String: {e:?}"), |
259 | | - &format!("{CONTEXT}, converting collaborator to String"), |
260 | | - ); |
261 | | - }, |
262 | | - } |
| 234 | + if let Ok(collabs) = cbor_doc_collabs.clone().into_array() { |
| 235 | + let mut c = Vec::new(); |
| 236 | + for (ids, collaborator) in collabs.iter().cloned().enumerate() { |
| 237 | + match collaborator.clone().into_text() { |
| 238 | + Ok(collaborator) => { |
| 239 | + c.push(collaborator); |
| 240 | + }, |
| 241 | + Err(_) => { |
| 242 | + error_report.conversion_error( |
| 243 | + &format!("COSE protected header collaborator index {ids}"), |
| 244 | + &format!("{collaborator:?}"), |
| 245 | + &format!("Expected a CBOR String"), |
| 246 | + &format!("{CONTEXT}, converting collaborator to String"), |
| 247 | + ); |
| 248 | + }, |
263 | 249 | } |
264 | | - extra.collabs = c; |
265 | | - }, |
266 | | - Err(e) => { |
267 | | - error_report.conversion_error( |
268 | | - "CBOR COSE protected header collaborators", |
269 | | - &format!("{cbor_doc_collabs:?}"), |
270 | | - &format!("Expected Array: {e:?}"), |
271 | | - &format!("{CONTEXT}, converting collaborators to Array"), |
272 | | - ); |
273 | | - }, |
274 | | - } |
| 250 | + } |
| 251 | + extra.collabs = c; |
| 252 | + } else { |
| 253 | + error_report.conversion_error( |
| 254 | + "CBOR COSE protected header collaborators", |
| 255 | + &format!("{cbor_doc_collabs:?}"), |
| 256 | + &format!("Expected a CBOR Array"), |
| 257 | + &format!("{CONTEXT}, converting collaborators to Array"), |
| 258 | + ); |
| 259 | + }; |
275 | 260 | } |
276 | 261 |
|
277 | 262 | if let Some(cbor_doc_brand_id) = cose_protected_header_find(protected, |key| { |
278 | 263 | key == &Label::Text(BRAND_ID_KEY.to_string()) |
279 | 264 | }) { |
280 | | - match DocumentRef::try_from(cbor_doc_brand_id) { |
281 | | - Ok(brand_id) => { |
282 | | - extra.brand_id = Some(brand_id); |
283 | | - }, |
284 | | - Err(e) => { |
285 | | - error_report.conversion_error( |
286 | | - "CBOR COSE protected header brand ID", |
287 | | - &format!("{cbor_doc_brand_id:?}"), |
288 | | - &format!("Expected UUID: {e:?}"), |
289 | | - &format!("{CONTEXT}, decoding CBOR UUID for brand ID"), |
290 | | - ); |
291 | | - }, |
| 265 | + if let Ok(brand_id) = DocumentRef::try_from(cbor_doc_brand_id) { |
| 266 | + extra.brand_id = Some(brand_id); |
| 267 | + } else { |
| 268 | + error_report.conversion_error( |
| 269 | + "CBOR COSE protected header brand ID", |
| 270 | + &format!("{cbor_doc_brand_id:?}"), |
| 271 | + &format!("Expected a CBOR UUID"), |
| 272 | + &format!("{CONTEXT}, decoding CBOR UUID for brand ID"), |
| 273 | + ); |
292 | 274 | } |
293 | 275 | } |
294 | 276 |
|
295 | 277 | if let Some(cbor_doc_campaign_id) = cose_protected_header_find(protected, |key| { |
296 | 278 | key == &Label::Text(CAMPAIGN_ID_KEY.to_string()) |
297 | 279 | }) { |
298 | | - match DocumentRef::try_from(cbor_doc_campaign_id) { |
299 | | - Ok(campaign_id) => { |
300 | | - extra.campaign_id = Some(campaign_id); |
301 | | - }, |
302 | | - Err(e) => { |
303 | | - error_report.conversion_error( |
304 | | - "CBOR COSE protected header campaign ID", |
305 | | - &format!("{cbor_doc_campaign_id:?}"), |
306 | | - &format!("Expected UUID: {e:?}"), |
307 | | - &format!("{CONTEXT}, decoding CBOR UUID for campaign ID"), |
308 | | - ); |
309 | | - }, |
| 280 | + if let Ok(campaign_id) = DocumentRef::try_from(cbor_doc_campaign_id) { |
| 281 | + extra.campaign_id = Some(campaign_id); |
| 282 | + } else { |
| 283 | + error_report.conversion_error( |
| 284 | + "CBOR COSE protected header campaign ID", |
| 285 | + &format!("{cbor_doc_campaign_id:?}"), |
| 286 | + &format!("Expected a CBOR UUID"), |
| 287 | + &format!("{CONTEXT}, decoding CBOR UUID for campaign ID"), |
| 288 | + ); |
310 | 289 | } |
311 | 290 | } |
312 | 291 |
|
313 | 292 | if let Some(cbor_doc_election_id) = cose_protected_header_find(protected, |key| { |
314 | 293 | key == &Label::Text(ELECTION_ID_KEY.to_string()) |
315 | 294 | }) { |
316 | | - match decode_cbor_uuid(cbor_doc_election_id.clone()) { |
317 | | - Ok(election_id) => { |
318 | | - extra.election_id = Some(election_id); |
319 | | - }, |
320 | | - Err(e) => { |
321 | | - error_report.conversion_error( |
322 | | - "CBOR COSE protected header election ID", |
323 | | - &format!("{cbor_doc_election_id:?}"), |
324 | | - &format!("Expected UUID: {e:?}"), |
325 | | - &format!("{CONTEXT}, decoding CBOR UUID for election ID"), |
326 | | - ); |
327 | | - }, |
| 295 | + if let Ok(election_id) = decode_cbor_uuid(cbor_doc_election_id.clone()) { |
| 296 | + extra.election_id = Some(election_id); |
| 297 | + } else { |
| 298 | + error_report.conversion_error( |
| 299 | + "CBOR COSE protected header election ID", |
| 300 | + &format!("{cbor_doc_election_id:?}"), |
| 301 | + &format!("Expected a CBOR UUID"), |
| 302 | + &format!("{CONTEXT}, decoding CBOR UUID for election ID"), |
| 303 | + ); |
328 | 304 | } |
329 | 305 | } |
330 | 306 |
|
331 | 307 | if let Some(cbor_doc_category_id) = cose_protected_header_find(protected, |key| { |
332 | 308 | key == &Label::Text(CATEGORY_ID_KEY.to_string()) |
333 | 309 | }) { |
334 | | - match DocumentRef::try_from(cbor_doc_category_id) { |
335 | | - Ok(category_id) => { |
336 | | - extra.category_id = Some(category_id); |
337 | | - }, |
338 | | - Err(e) => { |
339 | | - error_report.conversion_error( |
340 | | - "CBOR COSE protected header category ID", |
341 | | - &format!("{cbor_doc_category_id:?}"), |
342 | | - &format!("Expected UUID: {e:?}"), |
343 | | - &format!("{CONTEXT}, decoding CBOR UUID for category ID"), |
344 | | - ); |
345 | | - }, |
| 310 | + if let Ok(category_id) = DocumentRef::try_from(cbor_doc_category_id) { |
| 311 | + extra.category_id = Some(category_id); |
| 312 | + } else { |
| 313 | + error_report.conversion_error( |
| 314 | + "CBOR COSE protected header category ID", |
| 315 | + &format!("{cbor_doc_category_id:?}"), |
| 316 | + &format!("Expected a CBOR UUID"), |
| 317 | + &format!("{CONTEXT}, decoding CBOR UUID for category ID"), |
| 318 | + ); |
346 | 319 | } |
347 | 320 | } |
348 | 321 |
|
|
0 commit comments