|
| 1 | +//! Catalyst Signed Document spec type |
| 2 | +
|
| 3 | +#![allow(missing_docs, clippy::missing_docs_in_private_items)] |
| 4 | + |
| 5 | +pub mod content_type; |
| 6 | +pub mod doc_ref; |
| 7 | + |
| 8 | +use std::{collections::HashMap, ops::Deref}; |
| 9 | + |
| 10 | +/// Catalyst Signed Document spec representation struct |
| 11 | +#[derive(serde::Deserialize)] |
| 12 | +pub struct CatalystSignedDocSpec { |
| 13 | + /// A collection of document's supported content types |
| 14 | + #[serde(rename = "contentTypes")] |
| 15 | + #[allow(dead_code)] |
| 16 | + pub content_types: HashMap<String, ContentTypeSpec>, |
| 17 | + /// A collection of document's specs |
| 18 | + pub docs: HashMap<DocumentName, DocSpec>, |
| 19 | +} |
| 20 | + |
| 21 | +/// Catalyst Signed Document supported content type declaration struct |
| 22 | +#[derive(serde::Deserialize)] |
| 23 | +pub struct ContentTypeSpec { |
| 24 | + /// CoAP Content-Formats |
| 25 | + #[allow(dead_code)] |
| 26 | + coap_type: Option<u32>, |
| 27 | +} |
| 28 | + |
| 29 | +// A thin wrapper over the string document name values |
| 30 | +#[derive(serde::Deserialize, PartialEq, Eq, Hash)] |
| 31 | +pub struct DocumentName(String); |
| 32 | + |
| 33 | +impl DocumentName { |
| 34 | + /// returns document name |
| 35 | + pub fn name(&self) -> &str { |
| 36 | + &self.0 |
| 37 | + } |
| 38 | + |
| 39 | + /// returns a document name as a `Ident` in the following form |
| 40 | + /// `"PROPOSAL_FORM_TEMPLATE"` |
| 41 | + pub fn ident(&self) -> proc_macro2::Ident { |
| 42 | + quote::format_ident!( |
| 43 | + "{}", |
| 44 | + self.0 |
| 45 | + .split_whitespace() |
| 46 | + .map(str::to_uppercase) |
| 47 | + .collect::<Vec<_>>() |
| 48 | + .join("_") |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Specific document type definition |
| 54 | +#[derive(serde::Deserialize)] |
| 55 | +pub struct DocSpec { |
| 56 | + /// Document type UUID v4 value |
| 57 | + #[serde(rename = "type")] |
| 58 | + pub doc_type: String, |
| 59 | + /// `headers` field |
| 60 | + pub headers: Headers, |
| 61 | + /// Document type metadata definitions |
| 62 | + pub metadata: Metadata, |
| 63 | +} |
| 64 | + |
| 65 | +/// Document's metadata fields definition |
| 66 | +#[derive(serde::Deserialize)] |
| 67 | +#[allow(clippy::missing_docs_in_private_items)] |
| 68 | +pub struct Metadata { |
| 69 | + #[serde(rename = "ref")] |
| 70 | + pub doc_ref: doc_ref::Ref, |
| 71 | +} |
| 72 | + |
| 73 | +/// Document's metadata fields definition |
| 74 | +#[derive(serde::Deserialize)] |
| 75 | +#[allow(clippy::missing_docs_in_private_items)] |
| 76 | +pub struct Headers { |
| 77 | + #[serde(rename = "content type")] |
| 78 | + pub content_type: content_type::ContentType, |
| 79 | +} |
| 80 | + |
| 81 | +/// "required" field definition |
| 82 | +#[derive(serde::Deserialize)] |
| 83 | +#[serde(rename_all = "lowercase")] |
| 84 | +#[allow(clippy::missing_docs_in_private_items)] |
| 85 | +pub enum IsRequired { |
| 86 | + Yes, |
| 87 | + Excluded, |
| 88 | + Optional, |
| 89 | +} |
| 90 | + |
| 91 | +/// A helper type for deserialization "type" metadata field |
| 92 | +pub struct DocTypes(Vec<DocumentName>); |
| 93 | + |
| 94 | +impl Deref for DocTypes { |
| 95 | + type Target = Vec<DocumentName>; |
| 96 | + |
| 97 | + fn deref(&self) -> &Self::Target { |
| 98 | + &self.0 |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<'de> serde::Deserialize<'de> for DocTypes { |
| 103 | + #[allow(clippy::missing_docs_in_private_items)] |
| 104 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 105 | + where D: serde::Deserializer<'de> { |
| 106 | + #[derive(serde::Deserialize)] |
| 107 | + #[serde(untagged)] |
| 108 | + enum SingleOrVec { |
| 109 | + Single(DocumentName), |
| 110 | + Multiple(Vec<DocumentName>), |
| 111 | + } |
| 112 | + let value = Option::<SingleOrVec>::deserialize(deserializer)?; |
| 113 | + let result = match value { |
| 114 | + Some(SingleOrVec::Single(item)) => vec![item], |
| 115 | + Some(SingleOrVec::Multiple(items)) => items, |
| 116 | + None => vec![], |
| 117 | + }; |
| 118 | + Ok(Self(result)) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl CatalystSignedDocSpec { |
| 123 | + /// Loading a Catalyst Signed Documents spec from the `signed_doc.json` |
| 124 | + pub fn load_signed_doc_spec() -> anyhow::Result<CatalystSignedDocSpec> { |
| 125 | + let signed_doc_str = include_str!("../../../specs/signed_doc.json"); |
| 126 | + let signed_doc_spec = serde_json::from_str(signed_doc_str) |
| 127 | + .map_err(|e| anyhow::anyhow!("Invalid Catalyst Signed Documents JSON Spec: {e}"))?; |
| 128 | + Ok(signed_doc_spec) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +mod tests { |
| 134 | + use super::*; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn load_signed_doc_spec_test() { |
| 138 | + assert!(CatalystSignedDocSpec::load_signed_doc_spec().is_ok()); |
| 139 | + } |
| 140 | +} |
0 commit comments