Seal the RulesetMember trait

+ use maplit instead of custom macro
+ fix two little issues in the documentation
This commit is contained in:
Ana Gelez 2020-11-27 19:35:17 +01:00
parent 65ef5f21e5
commit 0e6d3f17c6
3 changed files with 21 additions and 22 deletions

View File

@ -12,6 +12,7 @@ edition = "2018"
[dependencies] [dependencies]
js_int = { version = "0.1.9", features = ["serde"] } js_int = { version = "0.1.9", features = ["serde"] }
maplit = "1.0.2"
ruma-common-macros = { version = "=0.2.0", path = "../ruma-common-macros" } ruma-common-macros = { version = "=0.2.0", path = "../ruma-common-macros" }
ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" }
ruma-serde = { version = "0.2.3", path = "../ruma-serde" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" }

View File

@ -14,8 +14,8 @@
//! - room rules //! - room rules
//! - sender rules //! - sender rules
//! //!
//! Each of these kind of rule has a corresponding type, that is //! Each of these kind of rule has a corresponding type that is
//! just a wrapper arround another type: //! just a wrapper around another type:
//! //!
//! - `SimplePushRule` for room and sender rules //! - `SimplePushRule` for room and sender rules
//! - `ConditionalPushRule` for override and underride rules: push rules that may depend on a //! - `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 /// 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. /// Adds a value in the correct field of a Ruleset.
fn add_to(self, ruleset: &mut Ruleset) -> bool; fn add_to(self, ruleset: &mut Ruleset) -> bool;
} }
mod private {
// See <https://rust-lang.github.io/api-guidelines/future-proofing.html>
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 /// Creates a new wrapper type around a PushRule-like type
/// to make it possible to tell what kind of rule it is /// to make it possible to tell what kind of rule it is
/// even if the inner type is the same. /// even if the inner type is the same.

View File

@ -1,27 +1,15 @@
///! Constructors for [predefined push rules]. ///! Constructors for [predefined push rules].
///! ///!
///! [predefined push rules]: https://matrix.org/docs/spec/client_server/r0.6.1#predefined-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::{ use super::{
Action::*, ConditionalPushRule, ContentPushRule, OverridePushRule, PatternedPushRule, Action::*, ConditionalPushRule, ContentPushRule, OverridePushRule, PatternedPushRule,
PushCondition::*, RoomMemberCountIs, Ruleset, Tweak, UnderridePushRule, 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 { impl Ruleset {
/// The list of all [predefined push rules]. /// 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). /// user's ID (for instance those to send notifications when they are mentioned).
pub fn server_default(user_id: &UserId) -> Self { pub fn server_default(user_id: &UserId) -> Self {
Self { Self {
content: set![ContentPushRule::contains_user_name(user_id)], content: btreeset![ContentPushRule::contains_user_name(user_id)],
override_: set![ override_: btreeset![
OverridePushRule::master(), OverridePushRule::master(),
OverridePushRule::suppress_notices(), OverridePushRule::suppress_notices(),
OverridePushRule::invite_for_me(user_id), OverridePushRule::invite_for_me(user_id),
@ -44,7 +32,7 @@ impl Ruleset {
OverridePushRule::tombstone(), OverridePushRule::tombstone(),
OverridePushRule::roomnotif(), OverridePushRule::roomnotif(),
], ],
underride: set![ underride: btreeset![
UnderridePushRule::call(), UnderridePushRule::call(),
UnderridePushRule::encrypted_room_one_to_one(), UnderridePushRule::encrypted_room_one_to_one(),
UnderridePushRule::room_one_to_one(), UnderridePushRule::room_one_to_one(),