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
This commit is contained in:
Jonas Platte 2020-12-24 02:14:26 +01:00
parent 7bf03efa6a
commit 520bab2900
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
4 changed files with 51 additions and 121 deletions

View File

@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features] [features]
default = ["serde"] default = ["serde"]
serde = ["serde1", "ruma-identifiers-validation/serde"] serde = ["ruma-serde", "serde1", "ruma-identifiers-validation/serde"]
[dependencies] [dependencies]
either = { version = "1.5.3", optional = true } either = { version = "1.5.3", optional = true }
@ -29,10 +29,10 @@ paste = "1.0.3"
rand = { version = "0.7.3", optional = true } rand = { version = "0.7.3", optional = true }
ruma-identifiers-macros = { version = "=0.17.4", path = "../ruma-identifiers-macros" } 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-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. # Renamed so we can have a serde feature.
serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] } serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] }
strum = { version = "0.19.2", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
matches = "0.1.8" matches = "0.1.8"

View File

@ -1,24 +1,17 @@
//! Key algorithms used in Matrix spec. //! Key algorithms used in Matrix spec.
use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use ruma_serde::{DeserializeFromCowStr, SerializeAsRefStr};
use ruma_serde_macros::{AsRefStr, DisplayAsRefStr, FromString};
use strum::{AsRefStr, Display, EnumString};
/// The basic key algorithms in the specification. /// The basic key algorithms in the specification.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)] ///
#[cfg_attr( /// This type can hold an arbitrary string. To check for algorithms that are not available as a
feature = "serde", /// documented variant here, use its string representation, obtained through `.as_str()`.
derive(Deserialize, Serialize), #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)]
serde(rename_all = "snake_case", crate = "serde")
)]
#[non_exhaustive] #[non_exhaustive]
#[strum(serialize_all = "snake_case")] #[ruma_enum(rename_all = "snake_case")]
#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))]
pub enum DeviceKeyAlgorithm { pub enum DeviceKeyAlgorithm {
/// The Ed25519 signature algorithm. /// The Ed25519 signature algorithm.
Ed25519, Ed25519,
@ -28,111 +21,45 @@ pub enum DeviceKeyAlgorithm {
/// The Curve25519 ECDH algorithm, but the key also contains signatures /// The Curve25519 ECDH algorithm, but the key also contains signatures
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 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<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<String> for SigningKeyAlgorithm {
type Error = strum::ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
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)] #[doc(hidden)]
_Custom(String), _Custom(String),
} }
impl EventEncryptionAlgorithm { /// The signing key algorithms defined in the Matrix spec.
/// Creates a string slice from this `EventEncryptionAlgorithm`. ///
pub fn as_str(&self) -> &str { /// This type can hold an arbitrary string. To check for algorithms that are not available as a
match *self { /// documented variant here, use its string representation, obtained through `.as_str()`.
EventEncryptionAlgorithm::OlmV1Curve25519AesSha2 => "m.olm.v1.curve25519-aes-sha2", #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)]
EventEncryptionAlgorithm::MegolmV1AesSha2 => "m.megolm.v1.aes-sha2", #[non_exhaustive]
EventEncryptionAlgorithm::_Custom(ref algorithm) => algorithm, #[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 { /// An encryption algorithm to be used to encrypt messages sent to a room.
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { ///
f.write_str(self.as_str()) /// 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<T> From<T> for EventEncryptionAlgorithm /// Megolm version 1 using AES-256 and SHA-256.
where #[ruma_enum(rename = "m.megolm.v1.aes-sha2")]
T: Into<String> + AsRef<str>, MegolmV1AesSha2,
{
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()),
}
}
}
impl From<EventEncryptionAlgorithm> for String { #[doc(hidden)]
fn from(algorithm: EventEncryptionAlgorithm) -> String { _Custom(String),
algorithm.to_string()
}
} }
#[cfg(test)] #[cfg(test)]
@ -141,14 +68,17 @@ mod tests {
#[test] #[test]
fn parse_device_key_algorithm() { fn parse_device_key_algorithm() {
assert_eq!("ed25519".parse(), Ok(DeviceKeyAlgorithm::Ed25519)); assert_eq!(DeviceKeyAlgorithm::from("ed25519"), DeviceKeyAlgorithm::Ed25519);
assert_eq!("curve25519".parse(), Ok(DeviceKeyAlgorithm::Curve25519)); assert_eq!(DeviceKeyAlgorithm::from("curve25519"), DeviceKeyAlgorithm::Curve25519);
assert_eq!("signed_curve25519".parse(), Ok(DeviceKeyAlgorithm::SignedCurve25519)); assert_eq!(
DeviceKeyAlgorithm::from("signed_curve25519"),
DeviceKeyAlgorithm::SignedCurve25519
);
} }
#[test] #[test]
fn parse_signing_key_algorithm() { fn parse_signing_key_algorithm() {
assert_eq!("ed25519".parse(), Ok(SigningKeyAlgorithm::Ed25519)); assert_eq!(SigningKeyAlgorithm::from("ed25519"), SigningKeyAlgorithm::Ed25519);
} }
#[test] #[test]

View File

@ -1,6 +1,6 @@
//! Identifiers for device keys for end-to-end encryption. //! 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}; use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId, Error};
@ -31,7 +31,7 @@ impl DeviceKeyId {
/// Returns key algorithm of the device key ID. /// Returns key algorithm of the device key ID.
pub fn algorithm(&self) -> DeviceKeyAlgorithm { 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. /// Returns device ID of the device key ID.

View File

@ -2,7 +2,7 @@
use std::{cmp::Ordering, convert::TryFrom, str::FromStr}; use std::{cmp::Ordering, convert::TryFrom, str::FromStr};
use ruma_serde::DisplayAsRefStr; use ruma_serde_macros::DisplayAsRefStr;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};