diff --git a/rust/src/config/mod.rs b/rust/src/config/mod.rs index aed0ea7762..f53014537d 100644 --- a/rust/src/config/mod.rs +++ b/rust/src/config/mod.rs @@ -15,6 +15,7 @@ use pyo3::{exceptions::PyRuntimeError, prelude::*}; use std::collections::BTreeSet; +use std::str::FromStr; #[derive(FromPyObject, Clone)] pub struct SynapseConfig { @@ -22,28 +23,36 @@ pub struct SynapseConfig { pub experimental: ExperimentalConfig, } -#[derive(Clone, Ord)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum RoomCreationPreset { - PrviateChat, + PrivateChat, PublicChat, TrustedPrivateChat, } +impl FromStr for RoomCreationPreset { + type Err = PyErr; + + fn from_str(s: &str) -> Result { + Ok(match s { + "private_chat" => RoomCreationPreset::PrivateChat, + "public_chat" => RoomCreationPreset::PublicChat, + "trusted_private_chat" => RoomCreationPreset::TrustedPrivateChat, + other => { + return Err(PyRuntimeError::new_err(format!( + "Unknown variant {other:?} does not translate to `RoomCreationPreset`. \ + This is a Synapse programming error." + ))) + } + }) + } +} + impl<'a, 'py> FromPyObject<'a, 'py> for RoomCreationPreset { type Error = PyErr; - /// Extract from a Python `LoggingTransaction` passed as an argument. - fn extract(value_py: Borrowed<'a, 'py, PyAny>) -> PyResult { - Ok(match value_py.extract()? { - "private_chat" => RoomCreationPreset::PrviateChat, - "public_chat" => RoomCreationPreset::PublicChat, - "trusted_private_chat" => RoomCreationPreset::TrustedPrivateChat, - other => { - return Err(PyRuntimeError::new_err( - format!("Unknown variant {other:#} does not translate to `RoomCreationPreset`. This is a Synapse programming error."), - )) - } - }) + fn extract(value: Borrowed<'a, 'py, PyAny>) -> PyResult { + value.extract::<&str>()?.parse() } } diff --git a/rust/src/handlers/versions.rs b/rust/src/handlers/versions.rs index aed3ab5f71..bed37083fb 100644 --- a/rust/src/handlers/versions.rs +++ b/rust/src/handlers/versions.rs @@ -261,7 +261,7 @@ pub fn synapse_config_to_global_unstable_feature_map(config: &SynapseConfig) -> e2ee_forced_private: config .room .encryption_enabled_by_default_for_room_presets - .contains(&RoomCreationPreset::PrviateChat), + .contains(&RoomCreationPreset::PrivateChat), e2ee_forced_trusted_private: config .room .encryption_enabled_by_default_for_room_presets