events: Add tag::TagName type

This commit is contained in:
Kévin Commaille 2021-05-03 17:33:12 +02:00 committed by GitHub
parent 7cf9fab6af
commit 448e29ce24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -40,6 +40,7 @@ Breaking changes:
},
sticker::StickerEventContent
```
* Add `tag::TagName` type and use it for `tag::Tags`
Improvements:

View File

@ -3,6 +3,7 @@
use std::collections::BTreeMap;
use ruma_events_macros::BasicEventContent;
use ruma_serde::StringEnum;
use serde::{Deserialize, Serialize};
use crate::BasicEvent;
@ -11,7 +12,7 @@ use crate::BasicEvent;
pub type TagEvent = BasicEvent<TagEventContent>;
/// Map of tag names to tag info.
pub type Tags = BTreeMap<String, TagInfo>;
pub type Tags = BTreeMap<TagName, TagInfo>;
/// The payload for `TagEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
@ -35,6 +36,29 @@ impl From<Tags> for TagEventContent {
}
}
/// The name of a tag.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum TagName {
/// `m.favourite`: The user's favourite rooms. These should be shown with higher precedence
/// than other rooms.
#[ruma_enum(rename = "m.favourite")]
Favorite,
/// `m.lowpriority`: These should be shown with lower precedence than others.
#[ruma_enum(rename = "m.lowpriority")]
LowPriority,
/// `m.server_notice`: Used to identify
/// [Server Notice Rooms](https://matrix.org/docs/spec/client_server/r0.6.1#module-server-notices).
#[ruma_enum(rename = "m.server_notice")]
ServerNotice,
/// A custom tag
#[doc(hidden)]
_Custom(String),
}
/// Information about a tag.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
@ -50,3 +74,37 @@ impl TagInfo {
Default::default()
}
}
#[cfg(test)]
mod tests {
use maplit::btreemap;
use serde_json::{json, to_value as to_json_value};
use super::{TagEventContent, TagInfo, TagName};
#[test]
fn serialization() {
let tags = btreemap! {
TagName::Favorite => TagInfo::new(),
TagName::LowPriority => TagInfo::new(),
TagName::ServerNotice => TagInfo::new(),
"u.custom".to_owned().into() => TagInfo { order: Some(0.9) }
};
let content = TagEventContent { tags };
assert_eq!(
to_json_value(content).unwrap(),
json!({
"tags": {
"m.favourite": {},
"m.lowpriority": {},
"m.server_notice": {},
"u.custom": {
"order": 0.9
}
},
})
);
}
}