From 5407a95a994d32cf01f8c24cbc23e04c062bbb62 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 30 Aug 2020 22:20:38 +0200 Subject: [PATCH] client-api: Update tag endpoints to the new API standards --- ruma-client-api/src/r0/tag/create_tag.rs | 23 +++++++++++++++--- ruma-client-api/src/r0/tag/delete_tag.rs | 23 +++++++++++++++--- ruma-client-api/src/r0/tag/get_tags.rs | 30 +++++++++++++++++++----- ruma-events/src/tag.rs | 24 ++++++++++++++++++- 4 files changed, 87 insertions(+), 13 deletions(-) diff --git a/ruma-client-api/src/r0/tag/create_tag.rs b/ruma-client-api/src/r0/tag/create_tag.rs index 8d7b1357..f4da8613 100644 --- a/ruma-client-api/src/r0/tag/create_tag.rs +++ b/ruma-client-api/src/r0/tag/create_tag.rs @@ -14,25 +14,42 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The ID of the user creating the tag. #[ruma_api(path)] - pub user_id: UserId, + pub user_id: &'a UserId, /// The room to tag. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, /// The name of the tag to create. #[ruma_api(path)] - pub tag: String, + pub tag: &'a str, /// Info about the tag. #[ruma_api(body)] pub tag_info: TagInfo, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user ID, room ID, tag and tag info. + pub fn new(user_id: &'a UserId, room_id: &'a RoomId, tag: &'a str, tag_info: TagInfo) -> Self { + Self { user_id, room_id, tag, tag_info } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/tag/delete_tag.rs b/ruma-client-api/src/r0/tag/delete_tag.rs index d41766a3..3ea857a6 100644 --- a/ruma-client-api/src/r0/tag/delete_tag.rs +++ b/ruma-client-api/src/r0/tag/delete_tag.rs @@ -13,21 +13,38 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The user whose tag will be deleted. #[ruma_api(path)] - pub user_id: UserId, + pub user_id: &'a UserId, /// The tagged room. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, /// The name of the tag to delete. #[ruma_api(path)] - pub tag: String, + pub tag: &'a str, } + #[derive(Default)] + #[non_exhaustive] response: {} error: crate::Error } + +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user ID, room ID and tag + pub fn new(user_id: &'a UserId, room_id: &'a RoomId, tag: &'a str) -> Self { + Self { user_id, room_id, tag } + } +} + +impl Response { + /// Creates an empty `Response`. + pub fn new() -> Self { + Self + } +} diff --git a/ruma-client-api/src/r0/tag/get_tags.rs b/ruma-client-api/src/r0/tag/get_tags.rs index 487aafd3..733582e9 100644 --- a/ruma-client-api/src/r0/tag/get_tags.rs +++ b/ruma-client-api/src/r0/tag/get_tags.rs @@ -14,16 +14,18 @@ ruma_api! { requires_authentication: true, } + #[non_exhaustive] request: { /// The user whose tags will be retrieved. #[ruma_api(path)] - pub user_id: UserId, + pub user_id: &'a UserId, /// The room from which tags will be retrieved. #[ruma_api(path)] - pub room_id: RoomId, + pub room_id: &'a RoomId, } + #[non_exhaustive] response: { /// The user's tags for the room. pub tags: Tags, @@ -32,19 +34,35 @@ ruma_api! { error: crate::Error } +impl<'a> Request<'a> { + /// Creates a new `Request` with the given user ID and room ID. + pub fn new(user_id: &'a UserId, room_id: &'a RoomId) -> Self { + Self { user_id, room_id } + } +} + +impl Response { + /// Creates a new `Response` with the given tags. + pub fn new(tags: Tags) -> Self { + Self { tags } + } +} + #[cfg(test)] mod tests { + use std::convert::TryFrom; + + use assign::assign; + use ruma_events::tag::{TagInfo, Tags}; use serde_json::json; use super::Response; - use ruma_events::tag::{TagInfo, Tags}; - use std::convert::TryFrom; #[test] fn test_serializing_get_tags_response() { let mut tags = Tags::new(); - tags.insert("m.favourite".into(), TagInfo { order: Some(0.25) }); - tags.insert("u.user_tag".into(), TagInfo { order: Some(0.11) }); + tags.insert("m.favourite".into(), assign!(TagInfo::new(), { order: Some(0.25) })); + tags.insert("u.user_tag".into(), assign!(TagInfo::new(), { order: Some(0.11) })); let response = Response { tags }; let http_response = http::Response::>::try_from(response).unwrap(); diff --git a/ruma-events/src/tag.rs b/ruma-events/src/tag.rs index 2f828947..b5e015ef 100644 --- a/ruma-events/src/tag.rs +++ b/ruma-events/src/tag.rs @@ -15,16 +15,38 @@ pub type Tags = BTreeMap; /// The payload for `TagEvent`. #[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[non_exhaustive] #[ruma_event(type = "m.tag")] pub struct TagEventContent { /// A map of tag names to tag info. pub tags: Tags, } +impl TagEventContent { + /// Creates a new `TagEventContent` with the given `Tags`. + pub fn new(tags: Tags) -> Self { + Self { tags } + } +} + +impl From for TagEventContent { + fn from(tags: Tags) -> Self { + Self::new(tags) + } +} + /// Information about a tag. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct TagInfo { /// Value to use for lexicographically ordering rooms with this tag. #[serde(skip_serializing_if = "Option::is_none")] pub order: Option, } + +impl TagInfo { + /// Creates an empty `TagInfo`. + pub fn new() -> Self { + Default::default() + } +}