Skip to content

Commit

Permalink
change Try to TryFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jan 9, 2025
1 parent 782c7b9 commit 5c74471
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
34 changes: 8 additions & 26 deletions rust/signed_doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
sync::Arc,
};

use anyhow::anyhow;
use coset::CborSerializable;

mod metadata;
Expand All @@ -24,8 +25,6 @@ struct InnerCatalystSignedDocument {
payload: JsonContent,
/// Raw COSE Sign data
cose_sign: coset::CoseSign,
/// Content Errors found when parsing the Document
content_errors: Vec<String>,
}

/// Keep all the contents private.
Expand All @@ -50,49 +49,38 @@ impl Display for CatalystSignedDocument {
hex::encode(signature.signature.as_slice())
)?;
}
writeln!(f, "]\n")?;
writeln!(f, "Content Errors [")?;
for error in &self.inner.content_errors {
writeln!(f, " {error:#}")?;
}
writeln!(f, "]")
writeln!(f, "]\n")
}
}

impl TryFrom<&[u8]> for CatalystSignedDocument {
type Error = anyhow::Error;
type Error = Vec<anyhow::Error>;

fn try_from(cose_bytes: &[u8]) -> Result<Self, Self::Error> {
// Try reading as a tagged COSE SIGN, otherwise try reading as untagged.
let cose_sign = coset::CoseSign::from_slice(cose_bytes)
.map_err(|e| anyhow::anyhow!("Invalid COSE Sign document: {e}"))?;

let mut content_errors = Vec::new();
.map_err(|e| vec![anyhow::anyhow!("Invalid COSE Sign document: {e}")])?;

let metadata = Metadata::from(&cose_sign.protected);

if metadata.is_valid() {
content_errors.extend_from_slice(metadata.content_errors());
}
let metadata = Metadata::try_from(&cose_sign.protected)?;

let mut content_errors = Vec::new();
let mut payload = JsonContent::default();

if let Some(bytes) = &cose_sign.payload {
match JsonContent::try_from((bytes, metadata.content_encoding())) {
Ok(c) => payload = c,
Err(e) => {
content_errors.push(format!("Invalid Payload: {e}"));
content_errors.push(anyhow!("Invalid Payload: {e}"));
},
}
} else {
content_errors.push("COSE payload is empty".to_string());
content_errors.push(anyhow!("COSE payload is empty"));
};

let inner = InnerCatalystSignedDocument {
metadata,
payload,
cose_sign,
content_errors,
};
Ok(CatalystSignedDocument {
inner: Arc::new(inner),
Expand All @@ -103,12 +91,6 @@ impl TryFrom<&[u8]> for CatalystSignedDocument {
impl CatalystSignedDocument {
// A bunch of getters to access the contents, or reason through the document, such as.

/// Are there any validation errors (as opposed to structural errors).
#[must_use]
pub fn is_valid(&self) -> bool {
!self.inner.content_errors.is_empty()
}

/// Return Document Type `UUIDv4`.
#[must_use]
pub fn doc_type(&self) -> uuid::Uuid {
Expand Down
13 changes: 10 additions & 3 deletions rust/signed_doc/src/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ impl Default for Metadata {
}
}

impl From<&coset::ProtectedHeader> for Metadata {
fn from(protected: &coset::ProtectedHeader) -> Self {
impl TryFrom<&coset::ProtectedHeader> for Metadata {
type Error = Vec<anyhow::Error>;

#[allow(clippy::too_many_lines)]
fn try_from(protected: &coset::ProtectedHeader) -> Result<Self, Self::Error> {
let mut metadata = Metadata::default();
let mut errors = Vec::new();

Expand Down Expand Up @@ -248,7 +251,11 @@ impl From<&coset::ProtectedHeader> for Metadata {
Err(e) => errors.extend(e),
};

metadata
if errors.is_empty() {
Ok(metadata)
} else {
Err(errors)
}
}
}

Expand Down

0 comments on commit 5c74471

Please sign in to comment.