client-api: Allow iterating Capabilities
… and move all capabilities-related types (capabilities::{get_capabilities => self}).
This commit is contained in:
parent
ed559c63f8
commit
27e91c972d
@ -1,3 +1,213 @@
|
|||||||
//! Endpoints for querying the server's supported feature set
|
//! Endpoints for querying the server's supported feature set
|
||||||
|
|
||||||
|
use std::{borrow::Cow, collections::BTreeMap};
|
||||||
|
|
||||||
|
use maplit::btreemap;
|
||||||
|
use ruma_identifiers::RoomVersionId;
|
||||||
|
use ruma_serde::StringEnum;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{from_value as from_json_value, to_value as to_json_value, Value as JsonValue};
|
||||||
|
|
||||||
|
use iter::{CapabilitiesIter, CapabilityRef};
|
||||||
|
|
||||||
pub mod get_capabilities;
|
pub mod get_capabilities;
|
||||||
|
pub mod iter;
|
||||||
|
|
||||||
|
/// Contains information about all the capabilities that the server supports.
|
||||||
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||||
|
pub struct Capabilities {
|
||||||
|
/// Capability to indicate if the user can change their password.
|
||||||
|
#[serde(
|
||||||
|
rename = "m.change_password",
|
||||||
|
default,
|
||||||
|
skip_serializing_if = "ChangePasswordCapability::is_default"
|
||||||
|
)]
|
||||||
|
pub change_password: ChangePasswordCapability,
|
||||||
|
|
||||||
|
/// The room versions the server supports.
|
||||||
|
#[serde(
|
||||||
|
rename = "m.room_versions",
|
||||||
|
default,
|
||||||
|
skip_serializing_if = "RoomVersionsCapability::is_default"
|
||||||
|
)]
|
||||||
|
pub room_versions: RoomVersionsCapability,
|
||||||
|
|
||||||
|
/// Any other custom capabilities that the server supports outside of the specification,
|
||||||
|
/// labeled using the Java package naming convention and stored as arbitrary JSON values.
|
||||||
|
#[serde(flatten)]
|
||||||
|
custom_capabilities: BTreeMap<String, JsonValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Capabilities {
|
||||||
|
/// Creates empty `Capabilities`.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Default::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the value of the given capability.
|
||||||
|
///
|
||||||
|
/// Prefer to use the public fields of `Capabilities` where possible; this method is meant to be
|
||||||
|
/// used for unsupported capabilities only.
|
||||||
|
pub fn get(&self, capability: &str) -> Option<Cow<'_, JsonValue>> {
|
||||||
|
fn serialize<T: Serialize>(cap: &T) -> JsonValue {
|
||||||
|
to_json_value(cap).expect("capability serialization to succeed")
|
||||||
|
}
|
||||||
|
|
||||||
|
match capability {
|
||||||
|
"m.change_password" => Some(Cow::Owned(serialize(&self.change_password))),
|
||||||
|
"m.room_versions" => Some(Cow::Owned(serialize(&self.room_versions))),
|
||||||
|
_ => self.custom_capabilities.get(capability).map(Cow::Borrowed),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a capability to the given value.
|
||||||
|
///
|
||||||
|
/// Prefer to use the public fields of `Capabilities` where possible; this method is meant to be
|
||||||
|
/// used for unsupported capabilities only and does not allow setting arbitrary data for
|
||||||
|
/// supported ones.
|
||||||
|
pub fn set(&mut self, capability: &str, value: JsonValue) -> serde_json::Result<()> {
|
||||||
|
match capability {
|
||||||
|
"m.change_password" => self.change_password = from_json_value(value)?,
|
||||||
|
"m.room_versions" => self.room_versions = from_json_value(value)?,
|
||||||
|
_ => {
|
||||||
|
self.custom_capabilities.insert(capability.to_owned(), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an iterator over the capabilities.
|
||||||
|
pub fn iter(&self) -> CapabilitiesIter {
|
||||||
|
CapabilitiesIter::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a Capabilities {
|
||||||
|
type Item = CapabilityRef<'a>;
|
||||||
|
type IntoIter = CapabilitiesIter<'a>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Information about the m.change_password capability
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||||
|
pub struct ChangePasswordCapability {
|
||||||
|
/// `true` if the user can change their password, `false` otherwise.
|
||||||
|
pub enabled: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChangePasswordCapability {
|
||||||
|
/// Creates a new `ChangePasswordCapability` with the given enabled flag.
|
||||||
|
pub fn new(enabled: bool) -> Self {
|
||||||
|
Self { enabled }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether all fields have their default value.
|
||||||
|
pub fn is_default(&self) -> bool {
|
||||||
|
self.enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ChangePasswordCapability {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { enabled: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Information about the m.room_versions capability
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
||||||
|
pub struct RoomVersionsCapability {
|
||||||
|
/// The default room version the server is using for new rooms.
|
||||||
|
pub default: RoomVersionId,
|
||||||
|
|
||||||
|
/// A detailed description of the room versions the server supports.
|
||||||
|
pub available: BTreeMap<RoomVersionId, RoomVersionStability>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoomVersionsCapability {
|
||||||
|
/// Creates a new `RoomVersionsCapability` with the given default room version ID and room
|
||||||
|
/// version descriptions.
|
||||||
|
pub fn new(
|
||||||
|
default: RoomVersionId,
|
||||||
|
available: BTreeMap<RoomVersionId, RoomVersionStability>,
|
||||||
|
) -> Self {
|
||||||
|
Self { default, available }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether all fields have their default value.
|
||||||
|
pub fn is_default(&self) -> bool {
|
||||||
|
self.default == RoomVersionId::Version1
|
||||||
|
&& self.available.len() == 1
|
||||||
|
&& self
|
||||||
|
.available
|
||||||
|
.get(&RoomVersionId::Version1)
|
||||||
|
.map(|stability| *stability == RoomVersionStability::Stable)
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RoomVersionsCapability {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
default: RoomVersionId::Version1,
|
||||||
|
available: btreemap! { RoomVersionId::Version1 => RoomVersionStability::Stable },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The stability of a room version
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
|
||||||
|
#[ruma_enum(rename_all = "lowercase")]
|
||||||
|
pub enum RoomVersionStability {
|
||||||
|
/// Support for the given version is stable.
|
||||||
|
Stable,
|
||||||
|
|
||||||
|
/// Support for the given version is unstable.
|
||||||
|
Unstable,
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
_Custom(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use super::Capabilities;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn capabilities_iter() -> serde_json::Result<()> {
|
||||||
|
let mut caps = Capabilities::new();
|
||||||
|
let custom_cap = json!({
|
||||||
|
"key": "value",
|
||||||
|
});
|
||||||
|
caps.set("m.some_random_capability", custom_cap)?;
|
||||||
|
let mut caps_iter = caps.iter();
|
||||||
|
|
||||||
|
let iter_res = caps_iter.next().unwrap();
|
||||||
|
assert_eq!(iter_res.name(), "m.change_password");
|
||||||
|
assert_eq!(iter_res.value(), Cow::Borrowed(&json!({ "enabled": true })));
|
||||||
|
|
||||||
|
let iter_res = caps_iter.next().unwrap();
|
||||||
|
assert_eq!(iter_res.name(), "m.room_versions");
|
||||||
|
assert_eq!(
|
||||||
|
iter_res.value(),
|
||||||
|
Cow::Borrowed(&json!({ "available": { "1": "stable" },"default" :"1" }))
|
||||||
|
);
|
||||||
|
|
||||||
|
let iter_res = caps_iter.next().unwrap();
|
||||||
|
assert_eq!(iter_res.name(), "m.some_random_capability");
|
||||||
|
assert_eq!(iter_res.value(), Cow::Borrowed(&json!({ "key": "value" })));
|
||||||
|
|
||||||
|
assert!(caps_iter.next().is_none());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
//! [GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-capabilities)
|
//! [GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-capabilities)
|
||||||
|
|
||||||
use std::{borrow::Cow, collections::BTreeMap};
|
|
||||||
|
|
||||||
use maplit::btreemap;
|
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
use ruma_identifiers::RoomVersionId;
|
|
||||||
use ruma_serde::StringEnum;
|
use super::Capabilities;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use serde_json::{from_value as from_json_value, to_value as to_json_value, Value as JsonValue};
|
|
||||||
|
|
||||||
ruma_api! {
|
ruma_api! {
|
||||||
metadata: {
|
metadata: {
|
||||||
@ -49,151 +44,3 @@ impl From<Capabilities> for Response {
|
|||||||
Self::new(capabilities)
|
Self::new(capabilities)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains information about all the capabilities that the server supports.
|
|
||||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
|
||||||
pub struct Capabilities {
|
|
||||||
/// Capability to indicate if the user can change their password.
|
|
||||||
#[serde(
|
|
||||||
rename = "m.change_password",
|
|
||||||
default,
|
|
||||||
skip_serializing_if = "ChangePasswordCapability::is_default"
|
|
||||||
)]
|
|
||||||
pub change_password: ChangePasswordCapability,
|
|
||||||
|
|
||||||
/// The room versions the server supports.
|
|
||||||
#[serde(
|
|
||||||
rename = "m.room_versions",
|
|
||||||
default,
|
|
||||||
skip_serializing_if = "RoomVersionsCapability::is_default"
|
|
||||||
)]
|
|
||||||
pub room_versions: RoomVersionsCapability,
|
|
||||||
|
|
||||||
/// Any other custom capabilities that the server supports outside of the specification,
|
|
||||||
/// labeled using the Java package naming convention and stored as arbitrary JSON values.
|
|
||||||
#[serde(flatten)]
|
|
||||||
custom_capabilities: BTreeMap<String, JsonValue>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Capabilities {
|
|
||||||
/// Creates empty `Capabilities`.
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Default::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the value of the given capability.
|
|
||||||
///
|
|
||||||
/// Prefer to use the public fields of `Capabilities` where possible; this method is meant to be
|
|
||||||
/// used for unsupported capabilities only.
|
|
||||||
pub fn get(&self, capability: &str) -> Option<Cow<'_, JsonValue>> {
|
|
||||||
fn serialize<T: Serialize>(cap: &T) -> JsonValue {
|
|
||||||
to_json_value(cap).expect("capability serialization to succeed")
|
|
||||||
}
|
|
||||||
|
|
||||||
match capability {
|
|
||||||
"m.change_password" => Some(Cow::Owned(serialize(&self.change_password))),
|
|
||||||
"m.room_versions" => Some(Cow::Owned(serialize(&self.room_versions))),
|
|
||||||
_ => self.custom_capabilities.get(capability).map(Cow::Borrowed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets a capability to the given value.
|
|
||||||
///
|
|
||||||
/// Prefer to use the public fields of `Capabilities` where possible; this method is meant to be
|
|
||||||
/// used for unsupported capabilities only and does not allow setting arbitrary data for
|
|
||||||
/// supported ones.
|
|
||||||
pub fn set(&mut self, capability: &str, value: JsonValue) -> serde_json::Result<()> {
|
|
||||||
match capability {
|
|
||||||
"m.change_password" => self.change_password = from_json_value(value)?,
|
|
||||||
"m.room_versions" => self.room_versions = from_json_value(value)?,
|
|
||||||
_ => {
|
|
||||||
self.custom_capabilities.insert(capability.to_owned(), value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Information about the m.change_password capability
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
|
||||||
pub struct ChangePasswordCapability {
|
|
||||||
/// `true` if the user can change their password, `false` otherwise.
|
|
||||||
pub enabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChangePasswordCapability {
|
|
||||||
/// Creates a new `ChangePasswordCapability` with the given enabled flag.
|
|
||||||
pub fn new(enabled: bool) -> Self {
|
|
||||||
Self { enabled }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether all fields have their default value.
|
|
||||||
pub fn is_default(&self) -> bool {
|
|
||||||
self.enabled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ChangePasswordCapability {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self { enabled: true }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Information about the m.room_versions capability
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
|
|
||||||
pub struct RoomVersionsCapability {
|
|
||||||
/// The default room version the server is using for new rooms.
|
|
||||||
pub default: RoomVersionId,
|
|
||||||
|
|
||||||
/// A detailed description of the room versions the server supports.
|
|
||||||
pub available: BTreeMap<RoomVersionId, RoomVersionStability>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RoomVersionsCapability {
|
|
||||||
/// Creates a new `RoomVersionsCapability` with the given default room version ID and room
|
|
||||||
/// version descriptions.
|
|
||||||
pub fn new(
|
|
||||||
default: RoomVersionId,
|
|
||||||
available: BTreeMap<RoomVersionId, RoomVersionStability>,
|
|
||||||
) -> Self {
|
|
||||||
Self { default, available }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether all fields have their default value.
|
|
||||||
pub fn is_default(&self) -> bool {
|
|
||||||
self.default == RoomVersionId::Version1
|
|
||||||
&& self.available.len() == 1
|
|
||||||
&& self
|
|
||||||
.available
|
|
||||||
.get(&RoomVersionId::Version1)
|
|
||||||
.map(|stability| *stability == RoomVersionStability::Stable)
|
|
||||||
.unwrap_or(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for RoomVersionsCapability {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
default: RoomVersionId::Version1,
|
|
||||||
available: btreemap! { RoomVersionId::Version1 => RoomVersionStability::Stable },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The stability of a room version
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
|
|
||||||
#[ruma_enum(rename_all = "lowercase")]
|
|
||||||
pub enum RoomVersionStability {
|
|
||||||
/// Support for the given version is stable.
|
|
||||||
Stable,
|
|
||||||
|
|
||||||
/// Support for the given version is unstable.
|
|
||||||
Unstable,
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
_Custom(String),
|
|
||||||
}
|
|
||||||
|
72
ruma-client-api/src/r0/capabilities/iter.rs
Normal file
72
ruma-client-api/src/r0/capabilities/iter.rs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
//! Iterator implementation for `Capabilities`
|
||||||
|
|
||||||
|
use std::{borrow::Cow, collections::btree_map};
|
||||||
|
|
||||||
|
use serde_json::Value as JsonValue;
|
||||||
|
|
||||||
|
use super::Capabilities;
|
||||||
|
|
||||||
|
/// Reference to a capability.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CapabilityRef<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
value: Option<&'a JsonValue>,
|
||||||
|
caps: &'a Capabilities,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> CapabilityRef<'a> {
|
||||||
|
/// Get name of the capability.
|
||||||
|
pub fn name(&self) -> &'a str {
|
||||||
|
self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get value of the capability.
|
||||||
|
pub fn value(&self) -> Cow<'a, JsonValue> {
|
||||||
|
match self.value {
|
||||||
|
// unknown capability from btreemap iterator
|
||||||
|
Some(val) => Cow::Borrowed(val),
|
||||||
|
// O(1) lookup of known capability
|
||||||
|
None => self.caps.get(self.name).unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An iterator over capabilities.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CapabilitiesIter<'a> {
|
||||||
|
/// Reference to Capabilities
|
||||||
|
caps: &'a Capabilities,
|
||||||
|
/// Current position of the iterator
|
||||||
|
pos: usize,
|
||||||
|
/// Iterator for custom capabilities
|
||||||
|
custom_caps_iterator: btree_map::Iter<'a, String, JsonValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> CapabilitiesIter<'a> {
|
||||||
|
/// Creates a new CapabilitiesIter
|
||||||
|
pub(super) fn new(caps: &'a Capabilities) -> Self {
|
||||||
|
Self { caps, pos: 0, custom_caps_iterator: caps.custom_capabilities.iter() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for CapabilitiesIter<'a> {
|
||||||
|
type Item = CapabilityRef<'a>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
match self.pos {
|
||||||
|
0 => {
|
||||||
|
self.pos += 1;
|
||||||
|
Some(CapabilityRef { name: "m.change_password", value: None, caps: self.caps })
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
self.pos += 1;
|
||||||
|
Some(CapabilityRef { name: "m.room_versions", value: None, caps: self.caps })
|
||||||
|
}
|
||||||
|
_ => self.custom_caps_iterator.next().map(|(name, value)| CapabilityRef {
|
||||||
|
name,
|
||||||
|
value: Some(value),
|
||||||
|
caps: self.caps,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user