Skip to content

Commit 93ceba1

Browse files
committed
add try from vec bytes conversion for X5Chain
Signed-off-by: Ryan Tate <[email protected]>
1 parent be54eb2 commit 93ceba1

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/definitions/helpers/non_empty_vec.rs

+16
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ impl<T: Clone> NonEmptyVec<T> {
5858
}
5959
}
6060

61+
impl<T: Clone> NonEmptyVec<T> {
62+
pub fn try_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Result<Self, Error> {
63+
let mut v = Vec::new();
64+
let mut iter = iter.into_iter();
65+
if let Some(t) = iter.next() {
66+
v.push(t);
67+
} else {
68+
return Err(Error::Empty);
69+
}
70+
for t in iter {
71+
v.push(t);
72+
}
73+
Ok(NonEmptyVec(v))
74+
}
75+
}
76+
6177
impl<T: Clone> TryFrom<Vec<T>> for NonEmptyVec<T> {
6278
type Error = Error;
6379

src/definitions/x509/x5chain.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::definitions::helpers::non_empty_vec;
12
use crate::definitions::helpers::NonEmptyVec;
23
use crate::definitions::x509::error::Error as X509Error;
34
use crate::definitions::x509::trust_anchor::check_validity_period;
@@ -77,6 +78,22 @@ impl From<NonEmptyVec<X509>> for X5Chain {
7778
}
7879
}
7980

81+
impl TryFrom<Vec<X509>> for X5Chain {
82+
type Error = non_empty_vec::Error;
83+
84+
fn try_from(v: Vec<X509>) -> Result<Self, Self::Error> {
85+
NonEmptyVec::try_from_iter(v.into_iter()).map(Self)
86+
}
87+
}
88+
89+
impl TryFrom<Vec<Vec<u8>>> for X5Chain {
90+
type Error = non_empty_vec::Error;
91+
92+
fn try_from(v: Vec<Vec<u8>>) -> Result<Self, Self::Error> {
93+
NonEmptyVec::try_from_iter(v.into_iter().map(|bytes| X509 { bytes })).map(Self)
94+
}
95+
}
96+
8097
impl X5Chain {
8198
pub fn builder() -> Builder {
8299
Builder::default()
@@ -124,6 +141,8 @@ impl X5Chain {
124141
}
125142
}
126143

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

0 commit comments

Comments
 (0)