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]
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" }

View File

@ -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 <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
/// to make it possible to tell what kind of rule it is
/// even if the inner type is the same.

View File

@ -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(),