From b79ef0303ae0b3becd5e7b156aa258f12a086281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Mon, 22 Mar 2021 11:53:28 +0100 Subject: [PATCH] Move common::push::AnyPushRule to client_api::r0::push::PushRule --- ruma-client-api/src/r0/push.rs | 147 ++++++++++++++++++- ruma-client-api/src/r0/push/get_pushrule.rs | 7 +- ruma-common/CHANGELOG.md | 2 + ruma-common/src/push.rs | 55 ++++--- ruma-common/src/push/any_push_rule.rs | 154 -------------------- 5 files changed, 176 insertions(+), 189 deletions(-) delete mode 100644 ruma-common/src/push/any_push_rule.rs diff --git a/ruma-client-api/src/r0/push.rs b/ruma-client-api/src/r0/push.rs index 23b60865..6e0148ee 100644 --- a/ruma-client-api/src/r0/push.rs +++ b/ruma-client-api/src/r0/push.rs @@ -1,6 +1,14 @@ //! Endpoints for push notifications. +use std::{ + convert::TryFrom, + error::Error, + fmt::{self, Display, Formatter}, +}; -use ruma_common::push::PusherData; +use ruma_common::push::{ + Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule, PatternedPushRuleInit, + PushCondition, PusherData, SimplePushRule, SimplePushRuleInit, +}; use ruma_serde::StringEnum; use serde::{Deserialize, Serialize}; @@ -17,6 +25,143 @@ pub mod set_pushrule; pub mod set_pushrule_actions; pub mod set_pushrule_enabled; +/// Like `SimplePushRule`, but may represent any kind of push rule +/// thanks to `pattern` and `conditions` being optional. +/// +/// To create an instance of this type, use one of its `From` implementations. +#[derive(Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct PushRule { + /// The actions to perform when this rule is matched. + pub actions: Vec, + + /// Whether this is a default rule, or has been set explicitly. + pub default: bool, + + /// Whether the push rule is enabled or not. + pub enabled: bool, + + /// The ID of this rule. + pub rule_id: String, + + /// The conditions that must hold true for an event in order for a rule to be applied to an + /// event. A rule with no conditions always matches. + /// + /// Only applicable to underride and override rules. + #[serde(skip_serializing_if = "Option::is_none")] + pub conditions: Option>, + + /// The glob-style pattern to match against. Only applicable to content rules. + #[serde(skip_serializing_if = "Option::is_none")] + pub pattern: Option, +} + +impl From for PushRule { + fn from(push_rule: SimplePushRule) -> Self { + let SimplePushRule { actions, default, enabled, rule_id, .. } = push_rule; + Self { actions, default, enabled, rule_id, conditions: None, pattern: None } + } +} + +impl From for PushRule { + fn from(push_rule: PatternedPushRule) -> Self { + let PatternedPushRule { actions, default, enabled, rule_id, pattern, .. } = push_rule; + Self { actions, default, enabled, rule_id, conditions: None, pattern: Some(pattern) } + } +} + +impl From for PushRule { + fn from(push_rule: ConditionalPushRule) -> Self { + let ConditionalPushRule { actions, default, enabled, rule_id, conditions, .. } = push_rule; + Self { actions, default, enabled, rule_id, conditions: Some(conditions), pattern: None } + } +} + +impl From for PushRule { + fn from(init: SimplePushRuleInit) -> Self { + let SimplePushRuleInit { actions, default, enabled, rule_id } = init; + Self { actions, default, enabled, rule_id, pattern: None, conditions: None } + } +} + +impl From for PushRule { + fn from(init: ConditionalPushRuleInit) -> Self { + let ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions } = init; + Self { actions, default, enabled, rule_id, pattern: None, conditions: Some(conditions) } + } +} + +impl From for PushRule { + fn from(init: PatternedPushRuleInit) -> Self { + let PatternedPushRuleInit { actions, default, enabled, rule_id, pattern } = init; + Self { actions, default, enabled, rule_id, pattern: Some(pattern), conditions: None } + } +} + +impl From for SimplePushRule { + fn from(push_rule: PushRule) -> Self { + let PushRule { actions, default, enabled, rule_id, .. } = push_rule; + SimplePushRuleInit { actions, default, enabled, rule_id }.into() + } +} + +/// An error that happens when `PushRule` cannot +/// be converted into `PatternedPushRule` +#[derive(Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct MissingPatternError; + +impl Display for MissingPatternError { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "Push rule does not have a pattern.") + } +} + +impl Error for MissingPatternError {} + +impl TryFrom for PatternedPushRule { + type Error = MissingPatternError; + + fn try_from(push_rule: PushRule) -> Result { + if let PushRule { actions, default, enabled, rule_id, pattern: Some(pattern), .. } = + push_rule + { + Ok(PatternedPushRuleInit { actions, default, enabled, rule_id, pattern }.into()) + } else { + Err(MissingPatternError) + } + } +} + +/// An error that happens when `PushRule` cannot +/// be converted into `ConditionalPushRule` +#[derive(Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct MissingConditionsError; + +impl Display for MissingConditionsError { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "Push rule has no conditions.") + } +} + +impl Error for MissingConditionsError {} + +impl TryFrom for ConditionalPushRule { + type Error = MissingConditionsError; + + fn try_from(push_rule: PushRule) -> Result { + if let PushRule { + actions, default, enabled, rule_id, conditions: Some(conditions), .. + } = push_rule + { + Ok(ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions }.into()) + } else { + Err(MissingConditionsError) + } + } +} + /// The kinds of push rules that are available. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)] #[ruma_enum(rename_all = "snake_case")] diff --git a/ruma-client-api/src/r0/push/get_pushrule.rs b/ruma-client-api/src/r0/push/get_pushrule.rs index ade98429..379254b3 100644 --- a/ruma-client-api/src/r0/push/get_pushrule.rs +++ b/ruma-client-api/src/r0/push/get_pushrule.rs @@ -1,9 +1,8 @@ //! [GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-pushrules-scope-kind-ruleid) use ruma_api::ruma_api; -use ruma_common::push::AnyPushRule; -use super::RuleKind; +use super::{PushRule, RuleKind}; ruma_api! { metadata: { @@ -32,7 +31,7 @@ ruma_api! { response: { /// The specific push rule. #[ruma_api(body)] - pub rule: AnyPushRule, + pub rule: PushRule, } error: crate::Error @@ -47,7 +46,7 @@ impl<'a> Request<'a> { impl Response { /// Creates a new `Response` with the given rule. - pub fn new(rule: AnyPushRule) -> Self { + pub fn new(rule: PushRule) -> Self { Self { rule } } } diff --git a/ruma-common/CHANGELOG.md b/ruma-common/CHANGELOG.md index fca94afe..763f7cd0 100644 --- a/ruma-common/CHANGELOG.md +++ b/ruma-common/CHANGELOG.md @@ -4,6 +4,8 @@ Breaking changes: * Update set of conversion trait implementations for enums * Replace `BTreeSet` by `IndexSet` in `push::Ruleset`. +* Remove `push::AnyPushRule`. Use `r0::push::PushRule` + from `ruma-client-api`. * … (there's a lot more, but this changelog was not kept up to date; PRs to improve it are welcome) diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index 4b266b20..0bbb0fa4 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -30,9 +30,6 @@ //! //! It is still possible to write code that is generic over a representation by manipulating //! `SimplePushRule`, `ConditonalPushRule` or `PatternedPushRule` directly, instead of the wrappers. -//! -//! There is also the `AnyPushRule` type that is the most generic form of push rule, with all -//! the possible fields. use std::hash::{Hash, Hasher}; @@ -44,13 +41,11 @@ use ruma_serde::StringEnum; use serde::{Deserialize, Serialize}; mod action; -mod any_push_rule; mod condition; mod predefined; pub use self::{ action::{Action, Tweak}, - any_push_rule::{AnyPushRule, MissingConditionsError, MissingPatternError}, condition::{ComparisonOperator, PushCondition, RoomMemberCountIs}, }; @@ -108,34 +103,34 @@ pub struct RulesetIter { underride: IndexSetIter, } -impl Iterator for RulesetIter { - type Item = AnyPushRule; +// impl Iterator for RulesetIter { +// type Item = AnyPushRule; - fn next(&mut self) -> Option { - self.override_ - .next() - .map(|x| x.0.into()) - .or_else(|| self.content.next().map(|x| x.0.into())) - .or_else(|| self.room.next().map(|x| x.0.into())) - .or_else(|| self.sender.next().map(|x| x.0.into())) - .or_else(|| self.underride.next().map(|x| x.0.into())) - } -} +// fn next(&mut self) -> Option { +// self.override_ +// .next() +// .map(|x| x.0.into()) +// .or_else(|| self.content.next().map(|x| x.0.into())) +// .or_else(|| self.room.next().map(|x| x.0.into())) +// .or_else(|| self.sender.next().map(|x| x.0.into())) +// .or_else(|| self.underride.next().map(|x| x.0.into())) +// } +// } -impl IntoIterator for Ruleset { - type Item = AnyPushRule; - type IntoIter = RulesetIter; +// impl IntoIterator for Ruleset { +// type Item = AnyPushRule; +// type IntoIter = RulesetIter; - fn into_iter(self) -> Self::IntoIter { - RulesetIter { - content: self.content.into_iter(), - override_: self.override_.into_iter(), - room: self.room.into_iter(), - sender: self.sender.into_iter(), - underride: self.underride.into_iter(), - } - } -} +// fn into_iter(self) -> Self::IntoIter { +// RulesetIter { +// content: self.content.into_iter(), +// override_: self.override_.into_iter(), +// room: self.room.into_iter(), +// sender: self.sender.into_iter(), +// underride: self.underride.into_iter(), +// } +// } +// } /// A trait for types that can be added in a Ruleset pub trait RulesetMember: private::Sealed { diff --git a/ruma-common/src/push/any_push_rule.rs b/ruma-common/src/push/any_push_rule.rs deleted file mode 100644 index 33d7836b..00000000 --- a/ruma-common/src/push/any_push_rule.rs +++ /dev/null @@ -1,154 +0,0 @@ -use std::{ - convert::TryFrom, - error::Error, - fmt::{self, Display, Formatter}, -}; - -use serde::{Deserialize, Serialize}; - -use super::{ - Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule, PatternedPushRuleInit, - PushCondition, SimplePushRule, SimplePushRuleInit, -}; - -/// Like `SimplePushRule`, but may represent any kind of push rule -/// thanks to `pattern` and `conditions` being optional. -/// -/// To create an instance of this type, use one of its `From` implementations. -#[derive(Clone, Debug, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct AnyPushRule { - /// The actions to perform when this rule is matched. - pub actions: Vec, - - /// Whether this is a default rule, or has been set explicitly. - pub default: bool, - - /// Whether the push rule is enabled or not. - pub enabled: bool, - - /// The ID of this rule. - pub rule_id: String, - - /// The conditions that must hold true for an event in order for a rule to be applied to an - /// event. A rule with no conditions always matches. - /// - /// Only applicable to underride and override rules. - #[serde(skip_serializing_if = "Option::is_none")] - pub conditions: Option>, - - /// The glob-style pattern to match against. Only applicable to content rules. - #[serde(skip_serializing_if = "Option::is_none")] - pub pattern: Option, -} - -impl From for AnyPushRule { - fn from(push_rule: SimplePushRule) -> Self { - let SimplePushRule { actions, default, enabled, rule_id } = push_rule; - Self { actions, default, enabled, rule_id, conditions: None, pattern: None } - } -} - -impl From for AnyPushRule { - fn from(push_rule: PatternedPushRule) -> Self { - let PatternedPushRule { actions, default, enabled, rule_id, pattern } = push_rule; - Self { actions, default, enabled, rule_id, conditions: None, pattern: Some(pattern) } - } -} - -impl From for AnyPushRule { - fn from(push_rule: ConditionalPushRule) -> Self { - let ConditionalPushRule { actions, default, enabled, rule_id, conditions } = push_rule; - Self { actions, default, enabled, rule_id, conditions: Some(conditions), pattern: None } - } -} - -impl From for AnyPushRule { - fn from(init: SimplePushRuleInit) -> Self { - let SimplePushRuleInit { actions, default, enabled, rule_id } = init; - Self { actions, default, enabled, rule_id, pattern: None, conditions: None } - } -} - -impl From for AnyPushRule { - fn from(init: ConditionalPushRuleInit) -> Self { - let ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions } = init; - Self { actions, default, enabled, rule_id, pattern: None, conditions: Some(conditions) } - } -} - -impl From for AnyPushRule { - fn from(init: PatternedPushRuleInit) -> Self { - let PatternedPushRuleInit { actions, default, enabled, rule_id, pattern } = init; - Self { actions, default, enabled, rule_id, pattern: Some(pattern), conditions: None } - } -} - -impl From for SimplePushRule { - fn from(push_rule: AnyPushRule) -> Self { - let AnyPushRule { actions, default, enabled, rule_id, .. } = push_rule; - Self { actions, default, enabled, rule_id } - } -} - -/// An error that happens when `AnyPushRule` cannot -/// be converted into `PatternedPushRule` -#[derive(Debug)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct MissingPatternError; - -impl Display for MissingPatternError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "Push rule does not have a pattern.") - } -} - -impl Error for MissingPatternError {} - -impl TryFrom for PatternedPushRule { - type Error = MissingPatternError; - - fn try_from(push_rule: AnyPushRule) -> Result { - if let AnyPushRule { actions, default, enabled, rule_id, pattern: Some(pattern), .. } = - push_rule - { - Ok(PatternedPushRule { actions, default, enabled, rule_id, pattern }) - } else { - Err(MissingPatternError) - } - } -} - -/// An error that happens when `AnyPushRule` cannot -/// be converted into `ConditionalPushRule` -#[derive(Debug)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct MissingConditionsError; - -impl Display for MissingConditionsError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "Push rule has no conditions.") - } -} - -impl Error for MissingConditionsError {} - -impl TryFrom for ConditionalPushRule { - type Error = MissingConditionsError; - - fn try_from(push_rule: AnyPushRule) -> Result { - if let AnyPushRule { - actions, - default, - enabled, - rule_id, - conditions: Some(conditions), - .. - } = push_rule - { - Ok(ConditionalPushRule { actions, default, enabled, rule_id, conditions }) - } else { - Err(MissingConditionsError) - } - } -}