Group fields in create_typing_event
//! [PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}](https://matrix.org/docs/spec/client_server/r0.6.0#put-matrix-client-r0-rooms-roomid-typing-userid)
+use serde::{de::Error, Deserialize, Deserializer, Serialize};
use std::time::Duration;
use ruma_api::ruma_api;
@@ -24,21 +25,57 @@ ruma_api! {
#[ruma_api(path)]
pub room_id: RoomId,
- // TODO: Group the following two body fields into an enum
+ /// Whether the user is typing within a length of time or not.
+ #[serde(flatten)]
+ pub state: Typing,
+ }
+
+ response: {}
+
+ error: crate::Error
+}
- /// Whether the user is typing or not. If `false`, the `timeout` key can be omitted.
- pub typing: bool,
+/// A mark for whether the user is typing within a length of time or not.
+#[derive(Clone, Copy, Debug, Serialize)]
+#[serde(into = "TypingInner")]
+pub enum Typing {
+ /// Not typing.
+ No,
+ /// Typing during the specified length of time.
+ Yes(Duration),
+}
- /// The length of time in milliseconds to mark this user as typing.
+#[derive(Deserialize, Serialize)]
+struct TypingInner {
+ typing: bool,
#[serde(
with = "ruma_serde::duration::opt_ms",
default,
- skip_serializing_if = "Option::is_none",
+ skip_serializing_if = "Option::is_none"
)]
- pub timeout: Option<Duration>,
+ timeout: Option<Duration>,
}
- response: {}
+impl From<Typing> for TypingInner {
+ fn from(typing: Typing) -> Self {
+ match typing {
+ Typing::No => Self { typing: false, timeout: None },
+ Typing::Yes(time) => Self { typing: true, timeout: Some(time) },
+ }
+ }
+}
- error: crate::Error
+impl<'de> Deserialize<'de> for Typing {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let inner = TypingInner::deserialize(deserializer)?;
+
+ match (inner.typing, inner.timeout) {
+ (false, _) => Ok(Self::No),
+ (true, Some(time)) => Ok(Self::Yes(time)),
+ _ => Err(D::Error::missing_field("timeout")),
+ }
+ }
}
