client-api: Remove Option in favor of serde(default) for capabilities
This commit is contained in:
		
							parent
							
								
									fdd326edf7
								
							
						
					
					
						commit
						9359a5b18e
					
				| @ -20,6 +20,7 @@ edition = "2018" | |||||||
| assign = "1.1.0" | assign = "1.1.0" | ||||||
| http = "0.2.1" | http = "0.2.1" | ||||||
| js_int = { version = "0.1.9", features = ["serde"] } | js_int = { version = "0.1.9", features = ["serde"] } | ||||||
|  | maplit = "1.0.2" | ||||||
| percent-encoding = "2.1.0" | percent-encoding = "2.1.0" | ||||||
| ruma-api = { version = "=0.17.0-alpha.1", path = "../ruma-api" } | ruma-api = { version = "=0.17.0-alpha.1", path = "../ruma-api" } | ||||||
| ruma-common = { version = "0.2.0", path = "../ruma-common" } | ruma-common = { version = "0.2.0", path = "../ruma-common" } | ||||||
|  | |||||||
| @ -1,5 +1,6 @@ | |||||||
| //! [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 maplit::btreemap; | ||||||
| use ruma_api::ruma_api; | use ruma_api::ruma_api; | ||||||
| use ruma_identifiers::RoomVersionId; | use ruma_identifiers::RoomVersionId; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| @ -52,12 +53,20 @@ impl From<Capabilities> for Response { | |||||||
| #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | ||||||
| pub struct Capabilities { | pub struct Capabilities { | ||||||
|     /// Capability to indicate if the user can change their password.
 |     /// Capability to indicate if the user can change their password.
 | ||||||
|     #[serde(rename = "m.change_password", skip_serializing_if = "Option::is_none")] |     #[serde(
 | ||||||
|     pub change_password: Option<ChangePasswordCapability>, |         rename = "m.change_password", | ||||||
|  |         default, | ||||||
|  |         skip_serializing_if = "ChangePasswordCapability::is_default" | ||||||
|  |     )] | ||||||
|  |     pub change_password: ChangePasswordCapability, | ||||||
| 
 | 
 | ||||||
|     /// The room versions the server supports.
 |     /// The room versions the server supports.
 | ||||||
|     #[serde(rename = "m.room_versions", skip_serializing_if = "Option::is_none")] |     #[serde(
 | ||||||
|     pub room_versions: Option<RoomVersionsCapability>, |         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,
 |     /// 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.
 |     /// labeled using the Java package naming convention and stored as arbitrary JSON values.
 | ||||||
| @ -85,6 +94,17 @@ impl ChangePasswordCapability { | |||||||
|     pub fn new(enabled: bool) -> Self { |     pub fn new(enabled: bool) -> Self { | ||||||
|         Self { enabled } |         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
 | /// Information about the m.room_versions capability
 | ||||||
| @ -107,10 +127,30 @@ impl RoomVersionsCapability { | |||||||
|     ) -> Self { |     ) -> Self { | ||||||
|         Self { default, available } |         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
 | /// The stability of a room version
 | ||||||
| #[derive(Clone, Copy, Debug, Serialize, Deserialize)] | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||||||
| #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | ||||||
| pub enum RoomVersionStability { | pub enum RoomVersionStability { | ||||||
|     /// Support for the given version is stable.
 |     /// Support for the given version is stable.
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user