From 0e6d3f17c619473e402070ee5a1c31936a7927d7 Mon Sep 17 00:00:00 2001 From: Ana Gelez Date: Fri, 27 Nov 2020 19:35:17 +0100 Subject: [PATCH] Seal the RulesetMember trait + use maplit instead of custom macro + fix two little issues in the documentation --- ruma-common/Cargo.toml | 1 + ruma-common/src/push.rs | 16 +++++++++++++--- ruma-common/src/push/predefined.rs | 26 +++++++------------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ruma-common/Cargo.toml b/ruma-common/Cargo.toml index ed50afcc..2bbf5811 100644 --- a/ruma-common/Cargo.toml +++ b/ruma-common/Cargo.toml @@ -12,6 +12,7 @@ edition = "2018" [dependencies] js_int = { version = "0.1.9", features = ["serde"] } +maplit = "1.0.2" ruma-common-macros = { version = "=0.2.0", path = "../ruma-common-macros" } ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index f0c74c37..5013aec0 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -14,8 +14,8 @@ //! - room rules //! - sender rules //! -//! Each of these kind of rule has a corresponding type, that is -//! just a wrapper arround another type: +//! Each of these kind of rule has a corresponding type that is +//! just a wrapper around another type: //! //! - `SimplePushRule` for room and sender rules //! - `ConditionalPushRule` for override and underride rules: push rules that may depend on a @@ -112,11 +112,21 @@ impl IntoIterator for Ruleset { } /// A trait for types that can be added in a Ruleset -pub trait RulesetMember { +pub trait RulesetMember: private::Sealed { /// Adds a value in the correct field of a Ruleset. fn add_to(self, ruleset: &mut Ruleset) -> bool; } +mod private { + // See + pub trait Sealed {} + impl Sealed for super::OverridePushRule {} + impl Sealed for super::UnderridePushRule {} + impl Sealed for super::ContentPushRule {} + impl Sealed for super::RoomPushRule {} + impl Sealed for super::SenderPushRule {} +} + /// Creates a new wrapper type around a PushRule-like type /// to make it possible to tell what kind of rule it is /// even if the inner type is the same. diff --git a/ruma-common/src/push/predefined.rs b/ruma-common/src/push/predefined.rs index 28a896cc..56df2302 100644 --- a/ruma-common/src/push/predefined.rs +++ b/ruma-common/src/push/predefined.rs @@ -1,27 +1,15 @@ ///! Constructors for [predefined push rules]. ///! ///! [predefined push rules]: https://matrix.org/docs/spec/client_server/r0.6.1#predefined-rules + +use maplit::btreeset; +use ruma_identifiers::UserId; + use super::{ Action::*, ConditionalPushRule, ContentPushRule, OverridePushRule, PatternedPushRule, PushCondition::*, RoomMemberCountIs, Ruleset, Tweak, UnderridePushRule, }; -use ruma_identifiers::UserId; - -macro_rules! set { - ($( $elt:expr ),*) => { - { - use std::collections::BTreeSet; - - let mut s = BTreeSet::new(); - $( s.insert( $elt ); )* - s - } - }; - ($( $elt:expr ),* ,) => { - set![ $( $elt ),* ] - }; -} impl Ruleset { /// The list of all [predefined push rules]. @@ -34,8 +22,8 @@ impl Ruleset { /// user's ID (for instance those to send notifications when they are mentioned). pub fn server_default(user_id: &UserId) -> Self { Self { - content: set![ContentPushRule::contains_user_name(user_id)], - override_: set![ + content: btreeset![ContentPushRule::contains_user_name(user_id)], + override_: btreeset![ OverridePushRule::master(), OverridePushRule::suppress_notices(), OverridePushRule::invite_for_me(user_id), @@ -44,7 +32,7 @@ impl Ruleset { OverridePushRule::tombstone(), OverridePushRule::roomnotif(), ], - underride: set![ + underride: btreeset![ UnderridePushRule::call(), UnderridePushRule::encrypted_room_one_to_one(), UnderridePushRule::room_one_to_one(),