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
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Types modeling end-to-end encryption related Matrix events
//!
//! These types aim to provide a more strict variant of the equivalent Ruma
//! types. Once deserialized they aim to zeroize all the secret material once
//! the type is dropped.

pub mod dummy;
pub mod forwarded_room_key;
pub mod olm_v1;
pub mod room;
pub mod room_key;
pub mod room_key_request;
pub mod room_key_withheld;
pub mod secret_send;
mod to_device;
mod utd_cause;

use ruma::serde::Raw;
pub use to_device::{ToDeviceCustomEvent, ToDeviceEvent, ToDeviceEvents};
pub use utd_cause::UtdCause;

/// A trait for event contents to define their event type.
pub trait EventType {
    /// The event type of the event content.
    const EVENT_TYPE: &'static str;

    /// Get the event type of the event content.
    ///
    /// **Note**: This should never be implemented manually, this takes the
    /// event type from the constant.
    fn event_type(&self) -> &'static str {
        Self::EVENT_TYPE
    }
}

impl<T: EventType> EventType for Raw<T> {
    const EVENT_TYPE: &'static str = T::EVENT_TYPE;
}

fn from_str<'a, T, E>(string: &'a str) -> Result<T, E>
where
    T: serde::Deserialize<'a>,
    E: serde::de::Error,
{
    serde_json::from_str(string).map_err(serde::de::Error::custom)
}