Skip to content

Commit 97af0ff

Browse files
committed
fix(rust/signed_doc): update metadata to require content type
* ContentType::Json is default
1 parent aa69d7b commit 97af0ff

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ fn cose_protected_header() -> coset::Header {
204204
}
205205

206206
fn build_empty_cose_doc(doc_bytes: Vec<u8>, meta: &Metadata) -> coset::CoseSign {
207-
let mut builder = coset::HeaderBuilder::new();
208-
209-
if let Some(content_type) = meta.content_type() {
210-
builder = builder.content_format(CoapContentFormat::from(content_type));
207+
let mut builder = coset::HeaderBuilder::new().content_format(CoapContentFormat::from(meta.content_type()));
211208
}
212209

213210
if let Some(content_encoding) = meta.content_encoding() {

rust/signed_doc/meta.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"required": [
116116
"type",
117117
"id",
118-
"ver"
118+
"ver",
119+
"content-type"
119120
]
120121
}

rust/signed_doc/src/metadata/content_type.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ pub enum ContentType {
1717
Json,
1818
}
1919

20+
impl Default for ContentType {
21+
fn default() -> Self {
22+
Self::Json
23+
}
24+
}
25+
2026
impl Display for ContentType {
2127
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
2228
match self {

rust/signed_doc/src/metadata/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub struct Metadata {
3232
id: DocumentId,
3333
/// Document Version `UUIDv7`.
3434
ver: DocumentVersion,
35+
/// Document Payload Content Type.
36+
#[serde(default, rename = "content-type")]
37+
content_type: ContentType,
38+
/// Document Payload Content Encoding.
39+
#[serde(default, rename = "content-encoding")]
40+
content_encoding: Option<ContentEncoding>,
3541
/// Reference to the latest document.
3642
#[serde(rename = "ref")]
3743
doc_ref: Option<DocumentRef>,
@@ -41,12 +47,6 @@ pub struct Metadata {
4147
reply: Option<DocumentRef>,
4248
/// Reference to the document section.
4349
section: Option<String>,
44-
/// Document Payload Content Type.
45-
#[serde(default, rename = "content-type")]
46-
content_type: Option<ContentType>,
47-
/// Document Payload Content Encoding.
48-
#[serde(default, rename = "content-encoding")]
49-
content_encoding: Option<ContentEncoding>,
5050
/// Metadata Content Errors
5151
#[serde(skip)]
5252
content_errors: Vec<String>,
@@ -109,7 +109,7 @@ impl Metadata {
109109

110110
/// Returns the Document Content Type, if any.
111111
#[must_use]
112-
pub fn content_type(&self) -> Option<ContentType> {
112+
pub fn content_type(&self) -> ContentType {
113113
self.content_type
114114
}
115115

@@ -130,7 +130,7 @@ impl Display for Metadata {
130130
writeln!(f, " template: {:?},", self.template)?;
131131
writeln!(f, " reply: {:?},", self.reply)?;
132132
writeln!(f, " section: {:?}", self.section)?;
133-
writeln!(f, " content_type: {:?}", self.content_type)?;
133+
writeln!(f, " content_type: {}", self.content_type)?;
134134
writeln!(f, " content_encoding: {:?}", self.content_encoding)?;
135135
writeln!(f, "}}")
136136
}
@@ -146,7 +146,7 @@ impl Default for Metadata {
146146
template: None,
147147
reply: None,
148148
section: None,
149-
content_type: None,
149+
content_type: ContentType::default(),
150150
content_encoding: None,
151151
content_errors: Vec::new(),
152152
}
@@ -173,7 +173,7 @@ impl From<&coset::ProtectedHeader> for Metadata {
173173
match protected.header.content_type.as_ref() {
174174
Some(iana_content_type) => {
175175
match ContentType::try_from(iana_content_type) {
176-
Ok(content_type) => metadata.content_type = Some(content_type),
176+
Ok(content_type) => metadata.content_type = content_type,
177177
Err(e) => {
178178
errors.push(format!("Invalid Document Content-Type: {e}"));
179179
},

0 commit comments

Comments
 (0)