File tree Expand file tree Collapse file tree 3 files changed +39
-20
lines changed Expand file tree Collapse file tree 3 files changed +39
-20
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ impl Content {
2828 . decode ( & data)
2929 . map_err ( |e| anyhow:: anyhow!( "Failed to decode {encoding} content: {e}" ) ) ?;
3030 }
31+ content_type. validate ( & data) ?;
3132
3233 Ok ( Self {
3334 data,
@@ -44,18 +45,7 @@ impl Content {
4445 pub ( crate ) fn from_decoded (
4546 data : Vec < u8 > , content_type : ContentType , content_encoding : Option < ContentEncoding > ,
4647 ) -> anyhow:: Result < Self > {
47- match content_type {
48- ContentType :: Json => {
49- if let Err ( e) = serde_json:: to_value ( & data) {
50- anyhow:: bail!( "Invalid {content_type} content: {e}" )
51- }
52- } ,
53- ContentType :: Cbor => {
54- if let Err ( e) = minicbor:: decode :: < minicbor:: data:: Token > ( & data) {
55- anyhow:: bail!( "Invalid {content_type} content: {e}" )
56- }
57- } ,
58- }
48+ content_type. validate ( & data) ?;
5949 Ok ( Self {
6050 data,
6151 content_type,
@@ -97,13 +87,7 @@ impl Content {
9787
9888 /// Return content byte size
9989 #[ must_use]
100- pub fn len ( & self ) -> usize {
90+ pub fn size ( & self ) -> usize {
10191 self . data . len ( )
10292 }
103-
104- /// Return `true` if content is empty
105- #[ must_use]
106- pub fn is_empty ( & self ) -> bool {
107- self . data . is_empty ( )
108- }
10993}
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ pub struct CatalystSignedDocument {
4747impl Display for CatalystSignedDocument {
4848 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
4949 writeln ! ( f, "{}" , self . inner. metadata) ?;
50- writeln ! ( f, "Payload Size: {} bytes" , self . inner. content. len ( ) ) ?;
50+ writeln ! ( f, "Payload Size: {} bytes" , self . inner. content. size ( ) ) ?;
5151 writeln ! ( f, "Signature Information" ) ?;
5252 if self . inner . signatures . is_empty ( ) {
5353 writeln ! ( f, " This document is unsigned." ) ?;
Original file line number Diff line number Diff line change @@ -18,6 +18,25 @@ pub enum ContentType {
1818 Json ,
1919}
2020
21+ impl ContentType {
22+ /// Validates the provided `content` bytes to be a defined `ContentType`.
23+ pub fn validate ( self , content : & [ u8 ] ) -> anyhow:: Result < ( ) > {
24+ match self {
25+ Self :: Json => {
26+ if let Err ( e) = serde_json:: from_slice :: < serde_json:: Value > ( content) {
27+ anyhow:: bail!( "Invalid {self} content: {e}" )
28+ }
29+ } ,
30+ Self :: Cbor => {
31+ if let Err ( e) = minicbor:: decode :: < minicbor:: data:: Token > ( content) {
32+ anyhow:: bail!( "Invalid {self} content: {e}" )
33+ }
34+ } ,
35+ }
36+ Ok ( ( ) )
37+ }
38+ }
39+
2140impl Display for ContentType {
2241 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
2342 match self {
@@ -78,3 +97,19 @@ impl TryFrom<&coset::ContentType> for ContentType {
7897 Ok ( content_type)
7998 }
8099}
100+
101+ #[ cfg( test) ]
102+ mod tests {
103+ use super :: * ;
104+
105+ #[ test]
106+ fn content_type_validate_test ( ) {
107+ let json_bytes = serde_json:: to_vec ( & serde_json:: Value :: Null ) . unwrap ( ) ;
108+ assert ! ( ContentType :: Json . validate( & json_bytes) . is_ok( ) ) ;
109+ assert ! ( ContentType :: Cbor . validate( & json_bytes) . is_err( ) ) ;
110+
111+ let cbor_bytes = minicbor:: to_vec ( minicbor:: data:: Token :: Null ) . unwrap ( ) ;
112+ assert ! ( ContentType :: Json . validate( & cbor_bytes) . is_err( ) ) ;
113+ assert ! ( ContentType :: Cbor . validate( & cbor_bytes) . is_ok( ) ) ;
114+ }
115+ }
You can’t perform that action at this time.
0 commit comments