From 520bab290046bd2cce3063c784f7236bf8caa1c8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 24 Dec 2020 02:14:26 +0100 Subject: [PATCH] identifiers: Clean up dependencies * Get rid of strum, now all crypto algorithms use ruma-serde's string / enum conversion code * Make the dependency on ruma-serde optional and only activate it through the serde feature --- ruma-identifiers/Cargo.toml | 6 +- ruma-identifiers/src/crypto_algorithms.rs | 160 ++++++---------------- ruma-identifiers/src/device_key_id.rs | 4 +- ruma-identifiers/src/room_version_id.rs | 2 +- 4 files changed, 51 insertions(+), 121 deletions(-) diff --git a/ruma-identifiers/Cargo.toml b/ruma-identifiers/Cargo.toml index 069e84bc..4652351d 100644 --- a/ruma-identifiers/Cargo.toml +++ b/ruma-identifiers/Cargo.toml @@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["serde"] -serde = ["serde1", "ruma-identifiers-validation/serde"] +serde = ["ruma-serde", "serde1", "ruma-identifiers-validation/serde"] [dependencies] either = { version = "1.5.3", optional = true } @@ -29,10 +29,10 @@ paste = "1.0.3" rand = { version = "0.7.3", optional = true } ruma-identifiers-macros = { version = "=0.17.4", path = "../ruma-identifiers-macros" } ruma-identifiers-validation = { version = "0.1.1", path = "../ruma-identifiers-validation", default-features = false } -ruma-serde = { version = "0.2.3", path = "../ruma-serde" } +ruma-serde = { version = "0.2.3", path = "../ruma-serde", optional = true } +ruma-serde-macros = { version = "0.2.3", path = "../ruma-serde-macros" } # Renamed so we can have a serde feature. serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] } -strum = { version = "0.19.2", features = ["derive"] } [dev-dependencies] matches = "0.1.8" diff --git a/ruma-identifiers/src/crypto_algorithms.rs b/ruma-identifiers/src/crypto_algorithms.rs index f561df47..4815b0f8 100644 --- a/ruma-identifiers/src/crypto_algorithms.rs +++ b/ruma-identifiers/src/crypto_algorithms.rs @@ -1,24 +1,17 @@ //! Key algorithms used in Matrix spec. -use std::{ - convert::TryFrom, - fmt::{Display, Formatter, Result as FmtResult}, -}; - #[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; - -use strum::{AsRefStr, Display, EnumString}; +use ruma_serde::{DeserializeFromCowStr, SerializeAsRefStr}; +use ruma_serde_macros::{AsRefStr, DisplayAsRefStr, FromString}; /// The basic key algorithms in the specification. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize), - serde(rename_all = "snake_case", crate = "serde") -)] +/// +/// This type can hold an arbitrary string. To check for algorithms that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)] #[non_exhaustive] -#[strum(serialize_all = "snake_case")] +#[ruma_enum(rename_all = "snake_case")] +#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))] pub enum DeviceKeyAlgorithm { /// The Ed25519 signature algorithm. Ed25519, @@ -28,111 +21,45 @@ pub enum DeviceKeyAlgorithm { /// The Curve25519 ECDH algorithm, but the key also contains signatures 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 signing 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), - serde(rename_all = "snake_case", crate = "serde") -)] -#[non_exhaustive] -#[strum(serialize_all = "snake_case")] -pub enum SigningKeyAlgorithm { - /// The Ed25519 signature algorithm. - Ed25519, -} - -impl TryFrom<&'_ str> for SigningKeyAlgorithm { - type Error = strum::ParseError; - - fn try_from(value: &str) -> Result { - value.parse() - } -} - -impl TryFrom for SigningKeyAlgorithm { - type Error = strum::ParseError; - - fn try_from(value: String) -> Result { - value.parse() - } -} - -/// An encryption algorithm to be used to encrypt messages sent to a room. -/// -/// This type can hold an arbitrary string. To check for events that are not -/// available as a documented variant here, use its string representation, -/// obtained through `.as_str()`. -#[derive(Clone, Debug, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(Deserialize, Serialize), - serde(from = "String", into = "String", crate = "serde") -)] -#[non_exhaustive] -pub enum EventEncryptionAlgorithm { - /// Olm version 1 using Curve25519, AES-256, and SHA-256. - OlmV1Curve25519AesSha2, - - /// Megolm version 1 using AES-256 and SHA-256. - MegolmV1AesSha2, #[doc(hidden)] _Custom(String), } -impl EventEncryptionAlgorithm { - /// Creates a string slice from this `EventEncryptionAlgorithm`. - pub fn as_str(&self) -> &str { - match *self { - EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => "m.olm.v1.curve25519-aes-sha2", - EventEncryptionAlgorithm::MegolmV1AesSha2 => "m.megolm.v1.aes-sha2", - EventEncryptionAlgorithm::_Custom(ref algorithm) => algorithm, - } - } +/// The signing key algorithms defined in the Matrix spec. +/// +/// This type can hold an arbitrary string. To check for algorithms that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)] +#[non_exhaustive] +#[ruma_enum(rename_all = "snake_case")] +#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))] +pub enum SigningKeyAlgorithm { + /// The Ed25519 signature algorithm. + Ed25519, + + #[doc(hidden)] + _Custom(String), } -impl Display for EventEncryptionAlgorithm { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - f.write_str(self.as_str()) - } -} +/// An encryption algorithm to be used to encrypt messages sent to a room. +/// +/// This type can hold an arbitrary string. To check for algorithms that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)] +#[non_exhaustive] +#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))] +pub enum EventEncryptionAlgorithm { + /// Olm version 1 using Curve25519, AES-256, and SHA-256. + #[ruma_enum(rename = "m.olm.v1.curve25519-aes-sha2")] + OlmV1Curve25519AesSha2, -impl From for EventEncryptionAlgorithm -where - T: Into + AsRef, -{ - fn from(s: T) -> EventEncryptionAlgorithm { - match s.as_ref() { - "m.olm.v1.curve25519-aes-sha2" => EventEncryptionAlgorithm::OlmV1Curve25519AesSha2, - "m.megolm.v1.aes-sha2" => EventEncryptionAlgorithm::MegolmV1AesSha2, - _ => EventEncryptionAlgorithm::_Custom(s.into()), - } - } -} + /// Megolm version 1 using AES-256 and SHA-256. + #[ruma_enum(rename = "m.megolm.v1.aes-sha2")] + MegolmV1AesSha2, -impl From for String { - fn from(algorithm: EventEncryptionAlgorithm) -> String { - algorithm.to_string() - } + #[doc(hidden)] + _Custom(String), } #[cfg(test)] @@ -141,14 +68,17 @@ mod tests { #[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)); + assert_eq!(DeviceKeyAlgorithm::from("ed25519"), DeviceKeyAlgorithm::Ed25519); + assert_eq!(DeviceKeyAlgorithm::from("curve25519"), DeviceKeyAlgorithm::Curve25519); + assert_eq!( + DeviceKeyAlgorithm::from("signed_curve25519"), + DeviceKeyAlgorithm::SignedCurve25519 + ); } #[test] fn parse_signing_key_algorithm() { - assert_eq!("ed25519".parse(), Ok(SigningKeyAlgorithm::Ed25519)); + assert_eq!(SigningKeyAlgorithm::from("ed25519"), SigningKeyAlgorithm::Ed25519); } #[test] diff --git a/ruma-identifiers/src/device_key_id.rs b/ruma-identifiers/src/device_key_id.rs index 4c6b2640..5b5f6ea4 100644 --- a/ruma-identifiers/src/device_key_id.rs +++ b/ruma-identifiers/src/device_key_id.rs @@ -1,6 +1,6 @@ //! Identifiers for device keys for end-to-end encryption. -use std::{convert::TryInto, num::NonZeroU8, str::FromStr}; +use std::{convert::TryInto, num::NonZeroU8}; use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId, Error}; @@ -31,7 +31,7 @@ impl DeviceKeyId { /// Returns key algorithm of the device key ID. pub fn algorithm(&self) -> DeviceKeyAlgorithm { - DeviceKeyAlgorithm::from_str(&self.full_id[..self.colon_idx.get() as usize]).unwrap() + self.full_id[..self.colon_idx.get() as usize].into() } /// Returns device ID of the device key ID. diff --git a/ruma-identifiers/src/room_version_id.rs b/ruma-identifiers/src/room_version_id.rs index eb495c4b..221900ce 100644 --- a/ruma-identifiers/src/room_version_id.rs +++ b/ruma-identifiers/src/room_version_id.rs @@ -2,7 +2,7 @@ use std::{cmp::Ordering, convert::TryFrom, str::FromStr}; -use ruma_serde::DisplayAsRefStr; +use ruma_serde_macros::DisplayAsRefStr; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer};