From 045f610e3021c656b24cbca25daec55cf147d0bd Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 17 Apr 2021 17:12:32 +0200 Subject: [PATCH] common: Add conversions between `AnyPushRule` and `AnyPushRuleRef` --- ruma-common/CHANGELOG.md | 6 ++++++ ruma-common/src/push/iter.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/ruma-common/CHANGELOG.md b/ruma-common/CHANGELOG.md index 969c16c1..a13fbf9e 100644 --- a/ruma-common/CHANGELOG.md +++ b/ruma-common/CHANGELOG.md @@ -4,6 +4,12 @@ Breaking changes: * Rename `push::RulesetIter` to `push::RulesetIntoIter` +Improvements: + +* Add `push::Ruleset::iter()` for borrowing iteration of rulesets +* Add conversions between `AnyPushRule` and `AnyPushRuleRef` + (`AnyPushRule::as_ref` and `AnyPushRuleRef::to_owned`) + # 0.4.0 Breaking changes: diff --git a/ruma-common/src/push/iter.rs b/ruma-common/src/push/iter.rs index aa817e9b..8386dd6e 100644 --- a/ruma-common/src/push/iter.rs +++ b/ruma-common/src/push/iter.rs @@ -22,6 +22,17 @@ pub enum AnyPushRule { } impl AnyPushRule { + /// Convert `AnyPushRule` to `AnyPushRuleRef`. + pub fn as_ref(&self) -> AnyPushRuleRef<'_> { + match self { + Self::Override(o) => AnyPushRuleRef::Override(&o), + Self::Content(c) => AnyPushRuleRef::Content(&c), + Self::Room(r) => AnyPushRuleRef::Room(&r), + Self::Sender(s) => AnyPushRuleRef::Sender(&s), + Self::Underride(u) => AnyPushRuleRef::Underride(&u), + } + } + /// Get the `rule_id` of the push rule. pub fn rule_id(&self) -> &str { match self { @@ -104,6 +115,17 @@ pub enum AnyPushRuleRef<'a> { } impl<'a> AnyPushRuleRef<'a> { + /// Convert `AnyPushRuleRef` to `AnyPushRule` by cloning the inner value. + pub fn to_owned(self) -> AnyPushRule { + match self { + Self::Override(o) => AnyPushRule::Override(o.clone()), + Self::Content(c) => AnyPushRule::Content(c.clone()), + Self::Room(r) => AnyPushRule::Room(r.clone()), + Self::Sender(s) => AnyPushRule::Sender(s.clone()), + Self::Underride(u) => AnyPushRule::Underride(u.clone()), + } + } + /// Get the `rule_id` of the push rule. pub fn rule_id(self) -> &'a str { match self {