Skip to content

Commit

Permalink
Remove duplication in TryFrom impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 3, 2023
1 parent 6cd164e commit 881438d
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ impl KeyPair {
})
}

pub(crate) fn from_raw<'b>(
pkcs8: impl Into<Cow<'b, [u8]>>,
) -> Result<(KeyPairKind, &'static SignatureAlgorithm), RcgenError> {
pub(crate) fn from_raw<'b>(pkcs8: impl Into<Cow<'b, [u8]>>) -> Result<KeyPair, RcgenError> {
let pkcs8 = pkcs8.into();

let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&pkcs8) {
Expand All @@ -167,7 +165,12 @@ impl KeyPair {
} else {
return Err(RcgenError::CouldNotParseKeyPair);
};
Ok((kind, alg))

Ok(KeyPair {
kind,
alg,
serialized_der: pkcs8.to_vec(),
})
}
}

Expand All @@ -190,25 +193,15 @@ impl TryFrom<&[u8]> for KeyPair {
type Error = RcgenError;

fn try_from(pkcs8: &[u8]) -> Result<KeyPair, RcgenError> {
let (kind, alg) = KeyPair::from_raw(pkcs8)?;
Ok(KeyPair {
kind,
alg,
serialized_der: pkcs8.to_vec(),
})
KeyPair::from_raw(pkcs8)
}
}

impl TryFrom<Vec<u8>> for KeyPair {
type Error = RcgenError;

fn try_from(pkcs8: Vec<u8>) -> Result<KeyPair, RcgenError> {
let (kind, alg) = KeyPair::from_raw(pkcs8.as_slice())?;
Ok(KeyPair {
kind,
alg,
serialized_der: pkcs8,
})
KeyPair::from_raw(pkcs8)
}
}

Expand Down

0 comments on commit 881438d

Please sign in to comment.