Skip to content

Commit 74fcede

Browse files
committed
change Content internal representation to Option<Vec<u8>>
1 parent 01b62b8 commit 74fcede

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

rust/signed_doc/src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct SignaturesBuilder {
3434
/// metadata
3535
metadata: WithCborBytes<Metadata>,
3636
/// content
37-
content: WithCborBytes<Content>,
37+
content: Content,
3838
/// signatures
3939
signatures: Signatures,
4040
}
@@ -71,7 +71,7 @@ impl ContentBuilder {
7171
fn into_signatures_builder(self) -> anyhow::Result<SignaturesBuilder> {
7272
Ok(SignaturesBuilder {
7373
metadata: WithCborBytes::new(self.metadata, &mut ())?,
74-
content: WithCborBytes::new(self.content, &mut ())?,
74+
content: self.content,
7575
signatures: Signatures::default(),
7676
})
7777
}
@@ -176,7 +176,7 @@ fn build_signature(
176176
sign_fn: impl FnOnce(Vec<u8>) -> Vec<u8>,
177177
kid: CatalystId,
178178
metadata: &WithCborBytes<Metadata>,
179-
content: &WithCborBytes<Content>,
179+
content: &Content,
180180
) -> anyhow::Result<Signature> {
181181
let data_to_sign = tbs_data(&kid, metadata, content)?;
182182
let sign_bytes = sign_fn(data_to_sign);

rust/signed_doc/src/content.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22
33
/// Document Content bytes (COSE payload).
44
#[derive(Debug, Clone, PartialEq, Default)]
5-
pub struct Content(Vec<u8>);
5+
pub struct Content(Option<Vec<u8>>);
66

77
impl Content {
88
/// Return content bytes.
99
#[must_use]
1010
pub fn bytes(&self) -> &[u8] {
11-
self.0.as_slice()
12-
}
13-
14-
/// Return content byte size.
15-
#[must_use]
16-
pub fn size(&self) -> usize {
17-
self.0.len()
11+
self.0.as_ref().map(|v| v.as_slice()).unwrap_or(&[])
1812
}
1913
}
2014

2115
impl From<Vec<u8>> for Content {
2216
fn from(value: Vec<u8>) -> Self {
23-
Self(value)
17+
Self(Some(value))
2418
}
2519
}
2620

@@ -30,13 +24,16 @@ impl minicbor::Encode<()> for Content {
3024
e: &mut minicbor::Encoder<W>,
3125
_ctx: &mut (),
3226
) -> Result<(), minicbor::encode::Error<W::Error>> {
33-
if self.0.is_empty() {
34-
e.null()?;
35-
} else {
36-
e.bytes(self.0.as_slice())?;
37-
}
27+
match &self.0 {
28+
Some(bytes) => e.bytes(&bytes)?,
29+
None => e.null()?,
30+
};
3831
Ok(())
3932
}
33+
34+
fn is_nil(&self) -> bool {
35+
self.0.is_none()
36+
}
4037
}
4138

4239
impl minicbor::Decode<'_, ()> for Content {
@@ -46,11 +43,11 @@ impl minicbor::Decode<'_, ()> for Content {
4643
) -> Result<Self, minicbor::decode::Error> {
4744
let p = d.position();
4845
d.null()
49-
.map(|()| Self(Vec::new()))
46+
.map(|()| Self(None))
5047
// important to use `or_else` so it will lazy evaluated at the time when it is needed
5148
.or_else(|_| {
5249
d.set_position(p);
53-
d.bytes().map(Vec::from).map(Self)
50+
d.bytes().map(Vec::from).map(Some).map(Self)
5451
})
5552
}
5653
}

rust/signed_doc/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct InnerCatalystSignedDocument {
4040
/// Document Metadata
4141
metadata: WithCborBytes<Metadata>,
4242
/// Document Content
43-
content: WithCborBytes<Content>,
43+
content: Content,
4444
/// Signatures
4545
signatures: Signatures,
4646
/// A comprehensive problem report, which could include a decoding errors along with
@@ -64,7 +64,6 @@ impl Display for CatalystSignedDocument {
6464
f: &mut Formatter<'_>,
6565
) -> Result<(), std::fmt::Error> {
6666
self.inner.metadata.fmt(f)?;
67-
writeln!(f, "Payload Size: {} bytes", self.inner.content.size())?;
6867
writeln!(f, "Signature Information")?;
6968
if self.inner.signatures.is_empty() {
7069
writeln!(f, " This document is unsigned.")?;
@@ -114,7 +113,7 @@ impl CatalystSignedDocument {
114113

115114
/// Return document content object.
116115
#[must_use]
117-
pub(crate) fn content(&self) -> &WithCborBytes<Content> {
116+
pub(crate) fn content(&self) -> &Content {
118117
&self.inner.content
119118
}
120119

@@ -286,7 +285,7 @@ impl Decode<'_, CompatibilityPolicy> for CatalystSignedDocument {
286285
);
287286
}
288287

289-
let content = WithCborBytes::<Content>::decode(
288+
let content = Content::decode(
290289
&mut minicbor::Decoder::new(content_bytes.as_slice()),
291290
&mut (),
292291
)?;

rust/signed_doc/src/signature/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Signatures {
7272
pub(crate) fn tbs_data(
7373
kid: &CatalystId,
7474
metadata: &WithCborBytes<Metadata>,
75-
content: &WithCborBytes<Content>,
75+
content: &Content,
7676
) -> anyhow::Result<Vec<u8>> {
7777
let mut e = minicbor::Encoder::new(Vec::new());
7878

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use std::fmt::Debug;
44

5+
use minicbor::Encode;
6+
57
use crate::{
68
validator::{json_schema, rules::utils::content_json_schema_check},
79
CatalystSignedDocument,
@@ -33,7 +35,6 @@ pub(crate) enum ContentRule {
3335
/// Document's content must be present and not CBOR `nil`
3436
NotNil,
3537
/// Document's content must be a CBOR `nil`
36-
#[allow(dead_code)]
3738
Nil,
3839
}
3940

@@ -53,14 +54,14 @@ impl ContentRule {
5354
}
5455
}
5556
if let Self::NotNil = self {
56-
if doc.content().size() == 0 {
57+
if doc.content().is_nil() {
5758
doc.report()
5859
.functional_validation("Document must have a NOT CBOR `nil` content", CONTEXT);
5960
return Ok(false);
6061
}
6162
}
6263
if let Self::Nil = self {
63-
if doc.content().size() != 0 {
64+
if !doc.content().is_nil() {
6465
doc.report()
6566
.functional_validation("Document must have a CBOR `nil` content", CONTEXT);
6667
return Ok(false);

0 commit comments

Comments
 (0)