ruma_events/relation/
rel_serde.rs

1use ruma_common::serde::Raw;
2use serde::{de::DeserializeOwned, Deserialize, Deserializer};
3
4use super::{BundledMessageLikeRelations, BundledThread, ReferenceChunk};
5
6#[derive(Deserialize)]
7struct BundledMessageLikeRelationsJsonRepr<E> {
8    #[serde(rename = "m.replace")]
9    replace: Option<Raw<Box<E>>>,
10    #[serde(rename = "m.thread")]
11    thread: Option<Box<BundledThread>>,
12    #[serde(rename = "m.reference")]
13    reference: Option<Box<ReferenceChunk>>,
14}
15
16impl<'de, E> Deserialize<'de> for BundledMessageLikeRelations<E>
17where
18    E: DeserializeOwned,
19{
20    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
21    where
22        D: Deserializer<'de>,
23    {
24        let BundledMessageLikeRelationsJsonRepr { replace, thread, reference } =
25            BundledMessageLikeRelationsJsonRepr::deserialize(deserializer)?;
26
27        let (replace, has_invalid_replacement) =
28            match replace.as_ref().map(Raw::deserialize).transpose() {
29                Ok(replace) => (replace, false),
30                Err(_) => (None, true),
31            };
32
33        Ok(BundledMessageLikeRelations { replace, has_invalid_replacement, thread, reference })
34    }
35}