common: Change the return type of Ruleset::get_actions to a slice

This commit is contained in:
Jonas Platte 2021-04-17 17:53:38 +02:00
parent aa8e00f886
commit fca5d36402
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 59 additions and 40 deletions

View File

@ -3,6 +3,8 @@
Breaking changes: Breaking changes:
* Rename `push::RulesetIter` to `push::RulesetIntoIter` * Rename `push::RulesetIter` to `push::RulesetIntoIter`
* Change the return type of `push::Ruleset::get_actions` from an iterator to a
slice
Improvements: Improvements:

View File

@ -91,7 +91,7 @@ impl Ruleset {
/// Get the push actions that apply to this event. /// Get the push actions that apply to this event.
/// ///
/// Returns an empty iterator if no push rule applies. /// Returns an empty slice if no push rule applies.
/// ///
/// # Arguments /// # Arguments
/// ///
@ -101,7 +101,7 @@ impl Ruleset {
&'a self, &'a self,
event: &Raw<T>, event: &Raw<T>,
context: &PushConditionRoomCtx, context: &PushConditionRoomCtx,
) -> impl Iterator<Item = &'a Action> ) -> &'a [Action]
where where
T: Serialize, T: Serialize,
{ {
@ -110,7 +110,6 @@ impl Ruleset {
.find(|rule| rule.applies(&event, context)) .find(|rule| rule.applies(&event, context))
.map(|rule| rule.actions()) .map(|rule| rule.actions())
.unwrap_or(&[]) .unwrap_or(&[])
.iter()
} }
} }
@ -939,16 +938,18 @@ mod tests {
}"#, }"#,
) )
.unwrap(); .unwrap();
let mut actions_one_to_one = set.get_actions(&message, context_one_to_one);
assert_matches!(actions_one_to_one.next(), Some(Action::Notify));
assert_matches!(actions_one_to_one.next(), Some(Action::SetTweak(Tweak::Sound(_))));
assert_matches!(actions_one_to_one.next(), Some(Action::SetTweak(Tweak::Highlight(false))));
let mut actions_public_room = set.get_actions(&message, context_public_room);
assert_matches!(actions_public_room.next(), Some(Action::Notify));
assert_matches!( assert_matches!(
actions_public_room.next(), set.get_actions(&message, context_one_to_one),
Some(Action::SetTweak(Tweak::Highlight(false))) [
Action::Notify,
Action::SetTweak(Tweak::Sound(_)),
Action::SetTweak(Tweak::Highlight(false))
]
);
assert_matches!(
set.get_actions(&message, context_public_room),
[Action::SetTweak(Tweak::Highlight(false))]
); );
let user_name = serde_json::from_str::<Raw<JsonValue>>( let user_name = serde_json::from_str::<Raw<JsonValue>>(
@ -960,15 +961,23 @@ mod tests {
}"#, }"#,
) )
.unwrap(); .unwrap();
let mut actions_one_to_one = set.get_actions(&user_name, context_one_to_one);
assert_matches!(actions_one_to_one.next(), Some(Action::Notify));
assert_matches!(actions_one_to_one.next(), Some(Action::SetTweak(Tweak::Sound(_))));
assert_matches!(actions_one_to_one.next(), Some(Action::SetTweak(Tweak::Highlight(true))));
let mut actions_public_room = set.get_actions(&user_name, context_public_room); assert_matches!(
assert_matches!(actions_public_room.next(), Some(Action::Notify)); set.get_actions(&user_name, context_one_to_one),
assert_matches!(actions_public_room.next(), Some(Action::SetTweak(Tweak::Sound(_)))); [
assert_matches!(actions_public_room.next(), Some(Action::SetTweak(Tweak::Highlight(true)))); Action::Notify,
Action::SetTweak(Tweak::Sound(_)),
Action::SetTweak(Tweak::Highlight(true)),
]
);
assert_matches!(
set.get_actions(&user_name, context_public_room),
[
Action::Notify,
Action::SetTweak(Tweak::Sound(_)),
Action::SetTweak(Tweak::Highlight(true)),
]
);
let notice = serde_json::from_str::<Raw<JsonValue>>( let notice = serde_json::from_str::<Raw<JsonValue>>(
r#"{ r#"{
@ -979,8 +988,7 @@ mod tests {
}"#, }"#,
) )
.unwrap(); .unwrap();
let mut actions = set.get_actions(&notice, context_one_to_one); assert_matches!(set.get_actions(&notice, context_one_to_one), [Action::DontNotify]);
assert_matches!(actions.next(), Some(Action::DontNotify));
let at_room = serde_json::from_str::<Raw<JsonValue>>( let at_room = serde_json::from_str::<Raw<JsonValue>>(
r#"{ r#"{
@ -993,13 +1001,14 @@ mod tests {
}"#, }"#,
) )
.unwrap(); .unwrap();
let mut actions = set.get_actions(&at_room, context_public_room);
assert_matches!(actions.next(), Some(Action::Notify)); assert_matches!(
assert_matches!(actions.next(), Some(Action::SetTweak(Tweak::Highlight(true)))); set.get_actions(&at_room, context_public_room),
[Action::Notify, Action::SetTweak(Tweak::Highlight(true)),]
);
let empty = serde_json::from_str::<Raw<JsonValue>>(r#"{}"#).unwrap(); let empty = serde_json::from_str::<Raw<JsonValue>>(r#"{}"#).unwrap();
let mut actions = set.get_actions(&empty, context_one_to_one); assert_matches!(set.get_actions(&empty, context_one_to_one), []);
assert_matches!(actions.next(), None);
} }
#[test] #[test]
@ -1038,8 +1047,7 @@ mod tests {
set.add(disabled); set.add(disabled);
let test_set = set.clone(); let test_set = set.clone();
let mut actions = test_set.get_actions(&message, context_one_to_one); assert_matches!(test_set.get_actions(&message, context_one_to_one), []);
assert_matches!(actions.next(), None);
let no_conditions = AnyPushRule::Underride(ConditionalPushRule { let no_conditions = AnyPushRule::Underride(ConditionalPushRule {
actions: vec![Action::SetTweak(Tweak::Highlight(true))], actions: vec![Action::SetTweak(Tweak::Highlight(true))],
@ -1051,8 +1059,10 @@ mod tests {
set.add(no_conditions); set.add(no_conditions);
let test_set = set.clone(); let test_set = set.clone();
let mut actions = test_set.get_actions(&message, context_one_to_one); assert_matches!(
assert_matches!(actions.next(), Some(Action::SetTweak(Tweak::Highlight(true)))); test_set.get_actions(&message, context_one_to_one),
[Action::SetTweak(Tweak::Highlight(true))]
);
let sender = AnyPushRule::Sender(SimplePushRule { let sender = AnyPushRule::Sender(SimplePushRule {
actions: vec![Action::Notify], actions: vec![Action::Notify],
@ -1063,8 +1073,7 @@ mod tests {
set.add(sender); set.add(sender);
let test_set = set.clone(); let test_set = set.clone();
let mut actions = test_set.get_actions(&message, context_one_to_one); assert_matches!(test_set.get_actions(&message, context_one_to_one), [Action::Notify]);
assert_matches!(actions.next(), Some(Action::Notify));
let room = AnyPushRule::Room(SimplePushRule { let room = AnyPushRule::Room(SimplePushRule {
actions: vec![Action::DontNotify], actions: vec![Action::DontNotify],
@ -1075,8 +1084,7 @@ mod tests {
set.add(room); set.add(room);
let test_set = set.clone(); let test_set = set.clone();
let mut actions = test_set.get_actions(&message, context_one_to_one); assert_matches!(test_set.get_actions(&message, context_one_to_one), [Action::DontNotify]);
assert_matches!(actions.next(), Some(Action::DontNotify));
let content = AnyPushRule::Content(PatternedPushRule { let content = AnyPushRule::Content(PatternedPushRule {
actions: vec![Action::SetTweak(Tweak::Sound("content".into()))], actions: vec![Action::SetTweak(Tweak::Sound("content".into()))],
@ -1088,8 +1096,11 @@ mod tests {
set.add(content); set.add(content);
let test_set = set.clone(); let test_set = set.clone();
let mut actions = test_set.get_actions(&message, context_one_to_one); assert_matches!(
assert_matches!(actions.next(), Some(Action::SetTweak(Tweak::Sound(sound))) if sound == "content"); test_set.get_actions(&message, context_one_to_one),
[Action::SetTweak(Tweak::Sound(sound))]
if sound == "content"
);
let three_conditions = AnyPushRule::Override(ConditionalPushRule { let three_conditions = AnyPushRule::Override(ConditionalPushRule {
actions: vec![Action::SetTweak(Tweak::Sound("three".into()))], actions: vec![Action::SetTweak(Tweak::Sound("three".into()))],
@ -1107,8 +1118,11 @@ mod tests {
}); });
set.add(three_conditions); set.add(three_conditions);
let mut actions = set.get_actions(&message, context_one_to_one); assert_matches!(
assert_matches!(actions.next(), Some(Action::SetTweak(Tweak::Sound(sound))) if sound == "content"); set.get_actions(&message, context_one_to_one),
[Action::SetTweak(Tweak::Sound(sound))]
if sound == "content"
);
let new_message = serde_json::from_str::<Raw<JsonValue>>( let new_message = serde_json::from_str::<Raw<JsonValue>>(
r#"{ r#"{
@ -1122,7 +1136,10 @@ mod tests {
) )
.unwrap(); .unwrap();
let mut actions = set.get_actions(&new_message, context_one_to_one); assert_matches!(
assert_matches!(actions.next(), Some(Action::SetTweak(Tweak::Sound(sound))) if sound == "three"); set.get_actions(&new_message, context_one_to_one),
[Action::SetTweak(Tweak::Sound(sound))]
if sound == "three"
);
} }
} }