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 015b450 commit 6266eb3
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 @@ -143,9 +143,7 @@ impl KeyPair {
})
}

pub(crate) fn from_raw(
pkcs8: &[u8],
) -> Result<(KeyPairKind, &'static SignatureAlgorithm), RcgenError> {
pub(crate) fn from_raw(pkcs8: &[u8]) -> Result<KeyPair, RcgenError> {
let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8) {
(KeyPairKind::Ed(edkp), &PKCS_ED25519)
} else if let Ok(eckp) =
Expand All @@ -164,7 +162,12 @@ impl KeyPair {
} else {
return Err(RcgenError::CouldNotParseKeyPair);
};
Ok((kind, alg))

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

Expand All @@ -187,25 +190,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 6266eb3

Please sign in to comment.