ruma_events/poll/unstable_start/
unstable_poll_kind_serde.rs
1use std::borrow::Cow;
4
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6
7use crate::{poll::start::PollKind, PrivOwnedStr};
8
9pub(super) fn serialize<S>(kind: &PollKind, serializer: S) -> Result<S::Ok, S::Error>
11where
12 S: Serializer,
13{
14 let s = match kind {
15 PollKind::Undisclosed => "org.matrix.msc3381.poll.undisclosed",
16 PollKind::Disclosed => "org.matrix.msc3381.poll.disclosed",
17 PollKind::_Custom(s) => &s.0,
18 };
19
20 s.serialize(serializer)
21}
22
23pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<PollKind, D::Error>
25where
26 D: Deserializer<'de>,
27{
28 let s = Cow::<'_, str>::deserialize(deserializer)?;
29
30 let kind = match &*s {
31 "org.matrix.msc3381.poll.undisclosed" => PollKind::Undisclosed,
32 "org.matrix.msc3381.poll.disclosed" => PollKind::Disclosed,
33 _ => PollKind::_Custom(PrivOwnedStr(s.into())),
34 };
35
36 Ok(kind)
37}