From 6266eb3d9a248b81644107ad5cba7888db97f47e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 3 Oct 2023 11:56:11 +1100 Subject: [PATCH] Remove duplication in `TryFrom` impl --- src/key_pair.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/key_pair.rs b/src/key_pair.rs index 72f8c321..4c8d6b04 100644 --- a/src/key_pair.rs +++ b/src/key_pair.rs @@ -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 { let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8) { (KeyPairKind::Ed(edkp), &PKCS_ED25519) } else if let Ok(eckp) = @@ -164,7 +162,12 @@ impl KeyPair { } else { return Err(RcgenError::CouldNotParseKeyPair); }; - Ok((kind, alg)) + + Ok(KeyPair { + kind, + alg, + serialized_der: pkcs8.to_vec(), + }) } } @@ -187,12 +190,7 @@ impl TryFrom<&[u8]> for KeyPair { type Error = RcgenError; fn try_from(pkcs8: &[u8]) -> Result { - let (kind, alg) = KeyPair::from_raw(pkcs8)?; - Ok(KeyPair { - kind, - alg, - serialized_der: pkcs8.to_vec(), - }) + KeyPair::from_raw(pkcs8) } } @@ -200,12 +198,7 @@ impl TryFrom> for KeyPair { type Error = RcgenError; fn try_from(pkcs8: Vec) -> Result { - let (kind, alg) = KeyPair::from_raw(pkcs8.as_slice())?; - Ok(KeyPair { - kind, - alg, - serialized_der: pkcs8, - }) + KeyPair::from_raw(&pkcs8) } }