Add tests for key_algorithm parsing

This commit is contained in:
Jonas Platte 2020-08-08 18:05:54 +02:00
parent 9384d24d04
commit f7ac4e2080
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -1,5 +1,7 @@
//! Key algorithms used in Matrix spec. //! Key algorithms used in Matrix spec.
use std::convert::TryFrom;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -21,6 +23,22 @@ pub enum DeviceKeyAlgorithm {
SignedCurve25519, SignedCurve25519,
} }
impl TryFrom<&'_ str> for DeviceKeyAlgorithm {
type Error = strum::ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<String> for DeviceKeyAlgorithm {
type Error = strum::ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}
/// The server key algorithms defined in the Matrix spec. /// The server key algorithms defined in the Matrix spec.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
@ -30,3 +48,36 @@ pub enum ServerKeyAlgorithm {
/// The Ed25519 signature algorithm. /// The Ed25519 signature algorithm.
Ed25519, Ed25519,
} }
impl TryFrom<&'_ str> for ServerKeyAlgorithm {
type Error = strum::ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<String> for ServerKeyAlgorithm {
type Error = strum::ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
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));
}
}