summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Christian Grünhage <jan.christian@gruenhage.xyz>2023-05-28 08:51:38 +0200
committerJan Christian Grünhage <jan.christian@gruenhage.xyz>2023-05-28 08:51:38 +0200
commit6b0202a0c26a13f78a51dbca6b22ae676b40b063 (patch)
tree47614f126b9bc9451e5cd39cdcab340b93495e7b
parent22d23a889e2cafd83b5bc6eb3279c5c24ce62964 (diff)
openpgp: Implement Display and FromStr on CipherSuitejcgruenhage/serde-display-fromstr-ciphersuite
-rw-r--r--openpgp/src/cert/builder.rs46
1 files changed, 44 insertions, 2 deletions
diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs
index 71ac1e90..36f25ce8 100644
--- a/openpgp/src/cert/builder.rs
+++ b/openpgp/src/cert/builder.rs
@@ -1,5 +1,9 @@
-use std::time;
-use std::marker::PhantomData;
+use std::{
+ fmt::{Display, Formatter},
+ marker::PhantomData,
+ str::FromStr,
+ time,
+};
use crate::packet;
use crate::packet::{
@@ -86,6 +90,44 @@ impl Default for CipherSuite {
}
}
+impl Display for CipherSuite {
+ fn fmt(&self, formatter: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ match self {
+ CipherSuite::Cv25519 => formatter.write_str("cv25519")?,
+ CipherSuite::P256 => formatter.write_str("p256")?,
+ CipherSuite::P384 => formatter.write_str("p384")?,
+ CipherSuite::P521 => formatter.write_str("p521")?,
+ CipherSuite::RSA2k => formatter.write_str("rsa2k")?,
+ CipherSuite::RSA3k => formatter.write_str("rsa3k")?,
+ CipherSuite::RSA4k => formatter.write_str("rsa4k")?,
+ }
+ Ok(())
+ }
+}
+
+impl FromStr for CipherSuite {
+ type Err = anyhow::Error;
+
+ fn from_str(s: &str) -> Result<Self> {
+ Ok(match s.to_lowercase().as_str() {
+ "cv25519" => CipherSuite::Cv25519,
+ "p256" => CipherSuite::P256,
+ "p384" => CipherSuite::P384,
+ "p521" => CipherSuite::P521,
+ "rsa2k" => CipherSuite::RSA2k,
+ "rsa3k" => CipherSuite::RSA3k,
+ "rsa4k" => CipherSuite::RSA4k,
+ cipher_suite => {
+ return Err(crate::Error::InvalidArgument(format!(
+ "cipher suite {} is not known",
+ cipher_suite
+ ))
+ .into())
+ }
+ })
+ }
+}
+
impl CipherSuite {
/// Returns whether the currently selected cryptographic backend
/// supports the encryption and signing algorithms that the cipher