Skip to content

Commit

Permalink
add try from vec bytes conversion for X5Chain
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Nov 20, 2024
1 parent be54eb2 commit 93ceba1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/definitions/helpers/non_empty_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ impl<T: Clone> NonEmptyVec<T> {
}
}

impl<T: Clone> NonEmptyVec<T> {
pub fn try_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Result<Self, Error> {
let mut v = Vec::new();
let mut iter = iter.into_iter();
if let Some(t) = iter.next() {
v.push(t);
} else {
return Err(Error::Empty);
}
for t in iter {
v.push(t);
}
Ok(NonEmptyVec(v))
}
}

impl<T: Clone> TryFrom<Vec<T>> for NonEmptyVec<T> {
type Error = Error;

Expand Down
19 changes: 19 additions & 0 deletions src/definitions/x509/x5chain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::definitions::helpers::non_empty_vec;
use crate::definitions::helpers::NonEmptyVec;
use crate::definitions::x509::error::Error as X509Error;
use crate::definitions::x509::trust_anchor::check_validity_period;
Expand Down Expand Up @@ -77,6 +78,22 @@ impl From<NonEmptyVec<X509>> for X5Chain {
}
}

impl TryFrom<Vec<X509>> for X5Chain {
type Error = non_empty_vec::Error;

fn try_from(v: Vec<X509>) -> Result<Self, Self::Error> {
NonEmptyVec::try_from_iter(v.into_iter()).map(Self)
}
}

impl TryFrom<Vec<Vec<u8>>> for X5Chain {
type Error = non_empty_vec::Error;

fn try_from(v: Vec<Vec<u8>>) -> Result<Self, Self::Error> {
NonEmptyVec::try_from_iter(v.into_iter().map(|bytes| X509 { bytes })).map(Self)
}
}

impl X5Chain {
pub fn builder() -> Builder {
Builder::default()
Expand Down Expand Up @@ -124,6 +141,8 @@ impl X5Chain {
}
}

/// Returns the first certificate in the x.509 certificate chain,
/// which is expected be the reader's certificate.
pub fn get_signer_key(&self) -> Result<VerifyingKey, X509Error> {
let leaf = self.0.first().ok_or(X509Error::CborDecodingError)?;
leaf.public_key().map(|key| key.into())
Expand Down

0 comments on commit 93ceba1

Please sign in to comment.