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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::{collections::BTreeMap, convert::Infallible, fmt::Display};

use serde::de::DeserializeOwned;

/// See [`TryFromRaw`][try]. This trait is merely a convenience that is be implemented instead of
/// [`TryFromRaw`][try] to get a [`TryFromRaw`][try] implementation with slightly less code if the
/// conversion can't fail, that is, the raw type and `Self` are identical in definition.
///
/// [try]: trait.TryFromRaw.html
pub trait FromRaw: Sized {
    /// The raw type.
    type Raw: DeserializeOwned;

    /// Converts the raw type to `Self`.
    fn from_raw(_: Self::Raw) -> Self;
}

/// Types corresponding to some item in the matrix spec. Types that implement this trait have a
/// corresponding 'raw' type, a potentially invalid representation that can be converted to `Self`.
pub trait TryFromRaw: Sized {
    /// The raw type.
    type Raw: DeserializeOwned;
    /// The error type returned if conversion fails.
    type Err: Display;

    /// Tries to convert the raw type to `Self`.
    fn try_from_raw(_: Self::Raw) -> Result<Self, Self::Err>;
}

impl FromRaw for ruma_serde::empty::Empty {
    type Raw = Self;

    fn from_raw(raw: Self) -> Self {
        raw
    }
}

impl FromRaw for serde_json::Value {
    type Raw = Self;

    fn from_raw(raw: Self) -> Self {
        raw
    }
}

impl<K, V> FromRaw for BTreeMap<K, V>
where
    Self: DeserializeOwned,
{
    type Raw = Self;

    fn from_raw(raw: Self) -> Self {
        raw
    }
}

impl<T: FromRaw> TryFromRaw for T {
    type Raw = <T as FromRaw>::Raw;
    type Err = Infallible;

    fn try_from_raw(raw: Self::Raw) -> Result<Self, Self::Err> {
        Ok(Self::from_raw(raw))
    }
}