Group fields in create_typing_event
This commit is contained in:
parent
dd87484a92
commit
d8340db310
@ -1,5 +1,6 @@
|
|||||||
//! [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)
|
//! [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 std::time::Duration;
|
||||||
|
|
||||||
use ruma_api::ruma_api;
|
use ruma_api::ruma_api;
|
||||||
@ -24,21 +25,57 @@ ruma_api! {
|
|||||||
#[ruma_api(path)]
|
#[ruma_api(path)]
|
||||||
pub room_id: RoomId,
|
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)]
|
||||||
/// Whether the user is typing or not. If `false`, the `timeout` key can be omitted.
|
pub state: Typing,
|
||||||
pub typing: bool,
|
|
||||||
|
|
||||||
/// The length of time in milliseconds to mark this user as typing.
|
|
||||||
#[serde(
|
|
||||||
with = "ruma_serde::duration::opt_ms",
|
|
||||||
default,
|
|
||||||
skip_serializing_if = "Option::is_none",
|
|
||||||
)]
|
|
||||||
pub timeout: Option<Duration>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response: {}
|
response: {}
|
||||||
|
|
||||||
error: crate::Error
|
error: crate::Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct TypingInner {
|
||||||
|
typing: bool,
|
||||||
|
#[serde(
|
||||||
|
with = "ruma_serde::duration::opt_ms",
|
||||||
|
default,
|
||||||
|
skip_serializing_if = "Option::is_none"
|
||||||
|
)]
|
||||||
|
timeout: Option<Duration>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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) },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user