Skip to content

Commit 3cf40f1

Browse files
committed
wip
1 parent 955e788 commit 3cf40f1

File tree

9 files changed

+96
-79
lines changed

9 files changed

+96
-79
lines changed

rust/catalyst-signed-doc-spec/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ homepage.workspace = true
77
repository.workspace = true
88
license.workspace = true
99

10+
[lints]
11+
workspace = true
12+
1013
[dependencies]
1114
serde_json = "1.0.142"
1215
anyhow = "1.0.99"
1316
serde = { version = "1.0.219", features = ["derive"] }
1417

1518
quote = "1.0"
1619
proc-macro2 = "1.0"
20+
build-info = "0.0.41"
1721

18-
[lints]
19-
workspace = true
22+
[build-dependencies]
23+
build-info-build = "0.0.41"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Build
2+
fn main() {
3+
build_info_build::build_script();
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//!
2+
3+
use std::ops::Deref;
4+
5+
use crate::DocumentName;
6+
7+
/// A helper type for deserialization "type" metadata field
8+
pub struct DocTypes(Vec<DocumentName>);
9+
10+
impl Deref for DocTypes {
11+
type Target = Vec<DocumentName>;
12+
13+
fn deref(&self) -> &Self::Target {
14+
&self.0
15+
}
16+
}
17+
18+
impl<'de> serde::Deserialize<'de> for DocTypes {
19+
#[allow(clippy::missing_docs_in_private_items)]
20+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
21+
where D: serde::Deserializer<'de> {
22+
#[derive(serde::Deserialize)]
23+
#[serde(untagged)]
24+
enum SingleOrVec {
25+
Single(DocumentName),
26+
Multiple(Vec<DocumentName>),
27+
}
28+
let value = Option::<SingleOrVec>::deserialize(deserializer)?;
29+
let result = match value {
30+
Some(SingleOrVec::Single(item)) => vec![item],
31+
Some(SingleOrVec::Multiple(items)) => items,
32+
None => vec![],
33+
};
34+
Ok(Self(result))
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! `signed_doc.json` headers content type field JSON definition
22
3+
use crate::is_required::IsRequired;
4+
35
/// `signed_doc.json` "content type" field JSON object
46
#[derive(serde::Deserialize)]
57
#[allow(clippy::missing_docs_in_private_items)]
68
pub struct ContentType {
7-
pub required: super::IsRequired,
9+
pub required: IsRequired,
810
pub value: Option<String>,
911
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! 'headers' field definition
2+
3+
pub mod content_type;
4+
5+
/// Document's metadata fields definition
6+
#[derive(serde::Deserialize)]
7+
#[allow(clippy::missing_docs_in_private_items)]
8+
pub struct Headers {
9+
#[serde(rename = "content type")]
10+
pub content_type: content_type::ContentType,
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! 'required' field allowed values definition
2+
3+
/// "required" field definition
4+
#[derive(serde::Deserialize)]
5+
#[serde(rename_all = "lowercase")]
6+
#[allow(clippy::missing_docs_in_private_items)]
7+
pub enum IsRequired {
8+
Yes,
9+
Excluded,
10+
Optional,
11+
}

rust/catalyst-signed-doc-spec/src/lib.rs

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@
22
33
#![allow(missing_docs, clippy::missing_docs_in_private_items)]
44

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;
79

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);
917

1018
/// Catalyst Signed Document spec representation struct
1119
#[derive(serde::Deserialize)]
1220
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>,
1721
/// A collection of document's specs
1822
pub docs: HashMap<DocumentName, DocSpec>,
1923
}
2024

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-
2925
// A thin wrapper over the string document name values
3026
#[derive(serde::Deserialize, PartialEq, Eq, Hash)]
3127
pub struct DocumentName(String);
@@ -53,75 +49,17 @@ impl DocumentName {
5349
/// Specific document type definition
5450
#[derive(serde::Deserialize)]
5551
pub struct DocSpec {
56-
/// Document type UUID v4 value
5752
#[serde(rename = "type")]
5853
pub doc_type: String,
59-
/// `headers` field
6054
pub headers: Headers,
61-
/// Document type metadata definitions
6255
pub metadata: Metadata,
6356
}
6457

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-
12258
impl CatalystSignedDocSpec {
12359
/// Loading a Catalyst Signed Documents spec from the `signed_doc.json`
12460
pub fn load_signed_doc_spec() -> anyhow::Result<CatalystSignedDocSpec> {
61+
let crate_version = build_info().crate_info.version.to_string();
62+
12563
let signed_doc_str = include_str!("../../../specs/signed_doc.json");
12664
let signed_doc_spec = serde_json::from_str(signed_doc_str)
12765
.map_err(|e| anyhow::anyhow!("Invalid Catalyst Signed Documents JSON Spec: {e}"))?;

rust/catalyst-signed-doc-spec/src/doc_ref.rs renamed to rust/catalyst-signed-doc-spec/src/metadata/doc_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `signed_doc.json` "ref" field JSON definition
22
3-
use crate::{DocTypes, IsRequired};
3+
use crate::{doc_types::DocTypes, is_required::IsRequired};
44

55
/// `signed_doc.json` "ref" field JSON object
66
#[derive(serde::Deserialize)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! `metadata` field definition
2+
3+
pub mod doc_ref;
4+
5+
/// Document's metadata fields definition
6+
#[derive(serde::Deserialize)]
7+
#[allow(clippy::missing_docs_in_private_items)]
8+
pub struct Metadata {
9+
#[serde(rename = "ref")]
10+
pub doc_ref: doc_ref::Ref,
11+
}

0 commit comments

Comments
 (0)