Group fields in create_typing_event

This commit is contained in:
Alejandro Domínguez 2020-08-21 20:35:25 +02:00 committed by GitHub
parent dd87484a92
commit d8340db310
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
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 or not. If `false`, the `timeout` key can be omitted.
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>,
/// Whether the user is typing within a length of time or not.
#[serde(flatten)]
pub state: Typing,
}
response: {}
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")),
}
}
}