diff --git a/ruma-identifiers-validation/src/key_algorithms.rs b/ruma-identifiers-validation/src/key_algorithms.rs index a96fe6e2..7bfbb021 100644 --- a/ruma-identifiers-validation/src/key_algorithms.rs +++ b/ruma-identifiers-validation/src/key_algorithms.rs @@ -1,5 +1,7 @@ //! Key algorithms used in Matrix spec. +use std::convert::TryFrom; + #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -21,6 +23,22 @@ pub enum DeviceKeyAlgorithm { SignedCurve25519, } +impl TryFrom<&'_ str> for DeviceKeyAlgorithm { + type Error = strum::ParseError; + + fn try_from(value: &str) -> Result { + value.parse() + } +} + +impl TryFrom for DeviceKeyAlgorithm { + type Error = strum::ParseError; + + fn try_from(value: String) -> Result { + value.parse() + } +} + /// The server key algorithms defined in the Matrix spec. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] @@ -30,3 +48,36 @@ pub enum ServerKeyAlgorithm { /// The Ed25519 signature algorithm. Ed25519, } + +impl TryFrom<&'_ str> for ServerKeyAlgorithm { + type Error = strum::ParseError; + + fn try_from(value: &str) -> Result { + value.parse() + } +} + +impl TryFrom for ServerKeyAlgorithm { + type Error = strum::ParseError; + + fn try_from(value: String) -> Result { + value.parse() + } +} + +#[cfg(test)] +mod tests { + use super::{DeviceKeyAlgorithm, ServerKeyAlgorithm}; + + #[test] + fn parse_device_key_algorithm() { + assert_eq!("ed25519".parse(), Ok(DeviceKeyAlgorithm::Ed25519)); + assert_eq!("curve25519".parse(), Ok(DeviceKeyAlgorithm::Curve25519)); + assert_eq!("signed_curve25519".parse(), Ok(DeviceKeyAlgorithm::SignedCurve25519)); + } + + #[test] + fn parse_server_key_algorithm() { + assert_eq!("ed25519".parse(), Ok(ServerKeyAlgorithm::Ed25519)); + } +}