|
2 | 2 |
|
3 | 3 | #![allow(missing_docs, clippy::missing_docs_in_private_items)] |
4 | 4 |
|
5 | | -pub mod content_type; |
6 | | -pub mod doc_ref; |
| 5 | +pub mod doc_types; |
| 6 | +pub mod headers; |
| 7 | +pub mod is_required; |
| 8 | +pub mod metadata; |
7 | 9 |
|
8 | | -use std::{collections::HashMap, ops::Deref}; |
| 10 | +use std::collections::HashMap; |
| 11 | + |
| 12 | +use build_info; |
| 13 | + |
| 14 | +use crate::{headers::Headers, metadata::Metadata}; |
| 15 | + |
| 16 | +build_info::build_info!(pub(crate) fn build_info); |
9 | 17 |
|
10 | 18 | /// Catalyst Signed Document spec representation struct |
11 | 19 | #[derive(serde::Deserialize)] |
12 | 20 | 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 | 21 | /// A collection of document's specs |
18 | 22 | pub docs: HashMap<DocumentName, DocSpec>, |
19 | 23 | } |
20 | 24 |
|
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 | 25 | // A thin wrapper over the string document name values |
30 | 26 | #[derive(serde::Deserialize, PartialEq, Eq, Hash)] |
31 | 27 | pub struct DocumentName(String); |
@@ -53,75 +49,17 @@ impl DocumentName { |
53 | 49 | /// Specific document type definition |
54 | 50 | #[derive(serde::Deserialize)] |
55 | 51 | pub struct DocSpec { |
56 | | - /// Document type UUID v4 value |
57 | 52 | #[serde(rename = "type")] |
58 | 53 | pub doc_type: String, |
59 | | - /// `headers` field |
60 | 54 | pub headers: Headers, |
61 | | - /// Document type metadata definitions |
62 | 55 | pub metadata: Metadata, |
63 | 56 | } |
64 | 57 |
|
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 | 58 | impl CatalystSignedDocSpec { |
123 | 59 | /// Loading a Catalyst Signed Documents spec from the `signed_doc.json` |
124 | 60 | pub fn load_signed_doc_spec() -> anyhow::Result<CatalystSignedDocSpec> { |
| 61 | + let crate_version = build_info().crate_info.version.to_string(); |
| 62 | + |
125 | 63 | let signed_doc_str = include_str!("../../../specs/signed_doc.json"); |
126 | 64 | let signed_doc_spec = serde_json::from_str(signed_doc_str) |
127 | 65 | .map_err(|e| anyhow::anyhow!("Invalid Catalyst Signed Documents JSON Spec: {e}"))?; |
|
0 commit comments