Skip to content

Commit 6d933fb

Browse files
committed
added new Section struct
1 parent 09e1b2c commit 6d933fb

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

rust/signed_doc/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use catalyst_types::problem_report::ProblemReport;
1919
pub use catalyst_types::uuid::{Uuid, UuidV4, UuidV7};
2020
pub use content::Content;
2121
use coset::{CborSerializable, Header};
22-
use metadata::{ContentEncoding, ContentType};
23-
pub use metadata::{DocumentRef, ExtraFields, Metadata};
22+
pub use metadata::{ContentEncoding, ContentType, DocumentRef, ExtraFields, Metadata, Section};
2423
use minicbor::{decode, encode, Decode, Decoder, Encode};
2524
use providers::VerifyingKeyProvider;
2625
pub use signature::{IdUri, Signatures};

rust/signed_doc/src/metadata/document_ref.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ impl Display for DocumentRef {
2626
}
2727
}
2828

29-
impl DocumentRef {
30-
/// Determine if internal `UUID`s are valid.
31-
#[must_use]
32-
pub fn is_valid(&self) -> bool {
33-
match self.ver {
34-
Some(ver) => self.id.is_valid() && ver.is_valid() && ver >= self.id,
35-
None => self.id.is_valid(),
36-
}
37-
}
38-
}
39-
4029
impl TryFrom<DocumentRef> for Value {
4130
type Error = anyhow::Error;
4231

rust/signed_doc/src/metadata/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod content_encoding;
66
mod content_type;
77
mod document_ref;
88
mod extra_fields;
9+
mod section;
910

1011
use algorithm::Algorithm;
1112
use catalyst_types::{
@@ -17,6 +18,7 @@ pub use content_type::ContentType;
1718
use coset::{iana::CoapContentFormat, CborSerializable};
1819
pub use document_ref::DocumentRef;
1920
pub use extra_fields::ExtraFields;
21+
pub use section::Section;
2022

2123
/// `content_encoding` field COSE key value
2224
const CONTENT_ENCODING_KEY: &str = "Content-Encoding";
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! Catalyst Signed Document `section` field type defition.
2+
3+
use std::{fmt::Display, str::FromStr};
4+
5+
use coset::cbor::Value;
6+
use serde::{Deserialize, Serialize};
7+
8+
/// 'section' field type defition, which is a JSON path string
9+
#[derive(Clone, Debug, PartialEq)]
10+
pub struct Section(jsonpath_rust::JsonPath<serde_json::Value>);
11+
12+
impl Display for Section {
13+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14+
self.0.fmt(f)
15+
}
16+
}
17+
18+
impl Serialize for Section {
19+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
20+
where S: serde::Serializer {
21+
self.to_string().serialize(serializer)
22+
}
23+
}
24+
25+
impl<'de> Deserialize<'de> for Section {
26+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
27+
where D: serde::Deserializer<'de> {
28+
let str = String::deserialize(deserializer)?;
29+
Ok(Self::from_str(&str).map_err(|e| serde::de::Error::custom(e))?)
30+
}
31+
}
32+
33+
impl FromStr for Section {
34+
type Err = anyhow::Error;
35+
36+
fn from_str(s: &str) -> Result<Self, Self::Err> {
37+
Ok(Self(
38+
jsonpath_rust::JsonPath::<serde_json::Value>::from_str(&s)?,
39+
))
40+
}
41+
}
42+
43+
impl From<Section> for Value {
44+
fn from(value: Section) -> Self {
45+
Value::Text(value.to_string())
46+
}
47+
}
48+
49+
impl TryFrom<&Value> for Section {
50+
type Error = anyhow::Error;
51+
52+
fn try_from(val: &Value) -> anyhow::Result<Self> {
53+
let str = val
54+
.as_text()
55+
.ok_or(anyhow::anyhow!("Not a cbor string type"))?;
56+
Ok(Self::from_str(str)?)
57+
}
58+
}

rust/signed_doc/src/validator/rules/content_type.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ where Provider: 'static + CatalystSignedDocumentProvider
1919
&'a self, doc: &'a CatalystSignedDocument, _provider: &'a Provider,
2020
) -> BoxFuture<'a, anyhow::Result<bool>> {
2121
async {
22-
if doc.doc_content_type()? != self.exp {
22+
let content_type = doc.doc_content_type()?;
23+
if content_type != self.exp {
2324
doc.report().invalid_value(
2425
"content-type",
25-
doc.doc_content_type()?.to_string().as_str(),
26+
content_type.to_string().as_str(),
2627
self.exp.to_string().as_str(),
2728
"Invalid Document content-type value",
2829
);

0 commit comments

Comments
 (0)