ruma_events/poll/unstable_start/
unstable_poll_kind_serde.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! `Serialize` and `Deserialize` helpers for unstable poll kind (MSC3381).

use std::borrow::Cow;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{poll::start::PollKind, PrivOwnedStr};

/// Serializes a PollKind using the unstable prefixes.
pub(super) fn serialize<S>(kind: &PollKind, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let s = match kind {
        PollKind::Undisclosed => "org.matrix.msc3381.poll.undisclosed",
        PollKind::Disclosed => "org.matrix.msc3381.poll.disclosed",
        PollKind::_Custom(s) => &s.0,
    };

    s.serialize(serializer)
}

/// Deserializes a PollKind using the unstable prefixes.
pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<PollKind, D::Error>
where
    D: Deserializer<'de>,
{
    let s = Cow::<'_, str>::deserialize(deserializer)?;

    let kind = match &*s {
        "org.matrix.msc3381.poll.undisclosed" => PollKind::Undisclosed,
        "org.matrix.msc3381.poll.disclosed" => PollKind::Disclosed,
        _ => PollKind::_Custom(PrivOwnedStr(s.into())),
    };

    Ok(kind)
}