From f31530e02ceede9f44aff4f8295181a13c6be793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 4 Nov 2022 17:07:06 +0100 Subject: [PATCH] client-api: Use an enum for the scope of the push rule endpoints --- crates/ruma-client-api/CHANGELOG.md | 1 + crates/ruma-client-api/src/push.rs | 13 +++++++++++++ crates/ruma-client-api/src/push/delete_pushrule.rs | 8 ++++---- crates/ruma-client-api/src/push/get_pushrule.rs | 8 ++++---- .../src/push/get_pushrule_actions.rs | 8 ++++---- .../src/push/get_pushrule_enabled.rs | 8 ++++---- crates/ruma-client-api/src/push/set_pushrule.rs | 8 ++++---- .../src/push/set_pushrule_actions.rs | 13 +++++++++---- .../src/push/set_pushrule_enabled.rs | 12 ++++++------ 9 files changed, 49 insertions(+), 30 deletions(-) diff --git a/crates/ruma-client-api/CHANGELOG.md b/crates/ruma-client-api/CHANGELOG.md index d4f7f356..156bf121 100644 --- a/crates/ruma-client-api/CHANGELOG.md +++ b/crates/ruma-client-api/CHANGELOG.md @@ -11,6 +11,7 @@ Breaking changes: * Move `push::get_pushers::v3::Pusher` to `push` and make it use the new `PusherIds` type * Remove `push::set_pusher::v3::Pusher` and use the common type instead * Make `push::PusherKind` contain the pusher's `data` +* Use an enum for the `scope` of the `push` endpoints Improvements: diff --git a/crates/ruma-client-api/src/push.rs b/crates/ruma-client-api/src/push.rs index a07fb5b1..0ce23975 100644 --- a/crates/ruma-client-api/src/push.rs +++ b/crates/ruma-client-api/src/push.rs @@ -284,3 +284,16 @@ pub struct CustomPusherData { kind: String, data: JsonObject, } + +/// The scope of a push rule. +#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "lowercase")] +#[non_exhaustive] +pub enum RuleScope { + /// The global rules. + Global, + + #[doc(hidden)] + _Custom(PrivOwnedStr), +} diff --git a/crates/ruma-client-api/src/push/delete_pushrule.rs b/crates/ruma-client-api/src/push/delete_pushrule.rs index e3f1783e..581ee0c9 100644 --- a/crates/ruma-client-api/src/push/delete_pushrule.rs +++ b/crates/ruma-client-api/src/push/delete_pushrule.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::api::ruma_api; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to delete from. 'global' to specify global rules. + /// The scope to delete from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -43,7 +43,7 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, kind and rule ID. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn new(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self { scope, kind, rule_id } } } diff --git a/crates/ruma-client-api/src/push/get_pushrule.rs b/crates/ruma-client-api/src/push/get_pushrule.rs index 884c7ed5..b601c1e9 100644 --- a/crates/ruma-client-api/src/push/get_pushrule.rs +++ b/crates/ruma-client-api/src/push/get_pushrule.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::api::ruma_api; - use crate::push::{PushRule, RuleKind}; + use crate::push::{PushRule, RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to fetch rules from. 'global' to specify global rules. + /// The scope to fetch rules from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule. #[ruma_api(path)] @@ -46,7 +46,7 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, rule kind and rule ID. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn new(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self { scope, kind, rule_id } } } diff --git a/crates/ruma-client-api/src/push/get_pushrule_actions.rs b/crates/ruma-client-api/src/push/get_pushrule_actions.rs index 2caa84af..80cbf343 100644 --- a/crates/ruma-client-api/src/push/get_pushrule_actions.rs +++ b/crates/ruma-client-api/src/push/get_pushrule_actions.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::{api::ruma_api, push::Action}; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to fetch a rule from. 'global' to specify global rules. + /// The scope to fetch a rule from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -45,7 +45,7 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, kind and rule ID. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn new(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self { scope, kind, rule_id } } } diff --git a/crates/ruma-client-api/src/push/get_pushrule_enabled.rs b/crates/ruma-client-api/src/push/get_pushrule_enabled.rs index bb74e44b..99d2a2e1 100644 --- a/crates/ruma-client-api/src/push/get_pushrule_enabled.rs +++ b/crates/ruma-client-api/src/push/get_pushrule_enabled.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::api::ruma_api; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to fetch a rule from. 'global' to specify global rules. + /// The scope to fetch a rule from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -45,7 +45,7 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, rule kind and rule ID. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn new(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self { scope, kind, rule_id } } } diff --git a/crates/ruma-client-api/src/push/set_pushrule.rs b/crates/ruma-client-api/src/push/set_pushrule.rs index f983a6ee..d798babc 100644 --- a/crates/ruma-client-api/src/push/set_pushrule.rs +++ b/crates/ruma-client-api/src/push/set_pushrule.rs @@ -10,7 +10,7 @@ pub mod v3 { push::{Action, PushCondition}, }; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -25,9 +25,9 @@ pub mod v3 { } request: { - /// The scope to set the rule in. 'global' to specify global rules. + /// The scope to set the rule in. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -74,7 +74,7 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, rule kind, rule ID and actions. pub fn new( - scope: &'a str, + scope: RuleScope, kind: RuleKind, rule_id: &'a str, actions: &'a [Action], diff --git a/crates/ruma-client-api/src/push/set_pushrule_actions.rs b/crates/ruma-client-api/src/push/set_pushrule_actions.rs index bb4a59ae..f25bbfad 100644 --- a/crates/ruma-client-api/src/push/set_pushrule_actions.rs +++ b/crates/ruma-client-api/src/push/set_pushrule_actions.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::{api::ruma_api, push::Action}; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to fetch a rule from. 'global' to specify global rules. + /// The scope to fetch a rule from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -46,7 +46,12 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, rule kind, rule ID and actions. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str, actions: Vec) -> Self { + pub fn new( + scope: RuleScope, + kind: RuleKind, + rule_id: &'a str, + actions: Vec, + ) -> Self { Self { scope, kind, rule_id, actions } } } diff --git a/crates/ruma-client-api/src/push/set_pushrule_enabled.rs b/crates/ruma-client-api/src/push/set_pushrule_enabled.rs index a94f73a6..fab8f115 100644 --- a/crates/ruma-client-api/src/push/set_pushrule_enabled.rs +++ b/crates/ruma-client-api/src/push/set_pushrule_enabled.rs @@ -7,7 +7,7 @@ pub mod v3 { use ruma_common::api::ruma_api; - use crate::push::RuleKind; + use crate::push::{RuleKind, RuleScope}; ruma_api! { metadata: { @@ -22,9 +22,9 @@ pub mod v3 { } request: { - /// The scope to fetch a rule from. 'global' to specify global rules. + /// The scope to fetch a rule from. #[ruma_api(path)] - pub scope: &'a str, + pub scope: RuleScope, /// The kind of rule #[ruma_api(path)] @@ -46,17 +46,17 @@ pub mod v3 { impl<'a> Request<'a> { /// Creates a new `Request` with the given scope, rule kind, rule ID and enabled flag. - pub fn new(scope: &'a str, kind: RuleKind, rule_id: &'a str, enabled: bool) -> Self { + pub fn new(scope: RuleScope, kind: RuleKind, rule_id: &'a str, enabled: bool) -> Self { Self { scope, kind, rule_id, enabled } } /// Creates a new `Request` to enable the given rule. - pub fn enable(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn enable(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self::new(scope, kind, rule_id, true) } /// Creates a new `Request` to disable the given rule. - pub fn disable(scope: &'a str, kind: RuleKind, rule_id: &'a str) -> Self { + pub fn disable(scope: RuleScope, kind: RuleKind, rule_id: &'a str) -> Self { Self::new(scope, kind, rule_id, false) } }