Skip to content

Commit 8eb8d9f

Browse files
committed
pkey/Id: implement TryInto<&str>
1 parent edb4e66 commit 8eb8d9f

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

openssl/src/pkey.rs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ impl Id {
119119
}
120120
}
121121

122+
impl TryFrom<Id> for &'static str {
123+
type Error = ();
124+
fn try_from(id: Id) -> Result<Self, Self::Error> {
125+
match id {
126+
Id::RSA => Ok("RSA"),
127+
#[cfg(any(ossl111, libressl310, boringssl, awslc))]
128+
Id::RSA_PSS => Ok("RSA-PSS"),
129+
#[cfg(not(boringssl))]
130+
Id::HMAC => Ok("HMAC"),
131+
#[cfg(not(any(boringssl, awslc)))]
132+
Id::CMAC => Ok("CMAC"),
133+
Id::DSA => Ok("DSA"),
134+
Id::DH => Ok("DH"),
135+
#[cfg(ossl110)]
136+
Id::DHX => Ok("DHX"),
137+
Id::EC => Ok("EC"),
138+
#[cfg(ossl111)]
139+
Id::SM2 => Ok("SM2"),
140+
#[cfg(any(ossl110, boringssl, libressl360, awslc))]
141+
Id::HKDF => Ok("HKDF"),
142+
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
143+
Id::ED25519 => Ok("Ed25519"),
144+
#[cfg(ossl111)]
145+
Id::ED448 => Ok("Ed448"),
146+
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
147+
Id::X25519 => Ok("X25519"),
148+
#[cfg(ossl111)]
149+
Id::X448 => Ok("X448"),
150+
#[cfg(ossl111)]
151+
Id::POLY1305 => Ok("POLY1305"),
152+
_ => Err(()),
153+
}
154+
}
155+
}
156+
122157
/// A trait indicating that a key has parameters.
123158
pub unsafe trait HasParams {}
124159

@@ -382,35 +417,7 @@ where
382417

383418
impl<T> fmt::Debug for PKey<T> {
384419
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
385-
let alg = match self.id() {
386-
Id::RSA => "RSA",
387-
#[cfg(any(ossl111, libressl310, boringssl, awslc))]
388-
Id::RSA_PSS => "RSA-PSS",
389-
#[cfg(not(boringssl))]
390-
Id::HMAC => "HMAC",
391-
#[cfg(not(any(boringssl, awslc)))]
392-
Id::CMAC => "CMAC",
393-
Id::DSA => "DSA",
394-
Id::DH => "DH",
395-
#[cfg(ossl110)]
396-
Id::DHX => "DHX",
397-
Id::EC => "EC",
398-
#[cfg(ossl111)]
399-
Id::SM2 => "SM2",
400-
#[cfg(any(ossl110, boringssl, libressl360, awslc))]
401-
Id::HKDF => "HKDF",
402-
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
403-
Id::ED25519 => "Ed25519",
404-
#[cfg(ossl111)]
405-
Id::ED448 => "Ed448",
406-
#[cfg(any(ossl111, boringssl, libressl370, awslc))]
407-
Id::X25519 => "X25519",
408-
#[cfg(ossl111)]
409-
Id::X448 => "X448",
410-
#[cfg(ossl111)]
411-
Id::POLY1305 => "POLY1305",
412-
_ => "unknown",
413-
};
420+
let alg = self.id().try_into().unwrap_or("unknown");
414421
fmt.debug_struct("PKey").field("algorithm", &alg).finish()
415422
// TODO: Print details for each specific type of key
416423
}

0 commit comments

Comments
 (0)