matrix_sdk_crypto/types/events/mod.rs
1// Copyright 2022 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types modeling end-to-end encryption related Matrix events
16//!
17//! These types aim to provide a more strict variant of the equivalent Ruma
18//! types. Once deserialized they aim to zeroize all the secret material once
19//! the type is dropped.
20
21pub mod dummy;
22pub mod forwarded_room_key;
23pub mod olm_v1;
24pub mod room;
25pub mod room_key;
26pub mod room_key_request;
27pub mod room_key_withheld;
28pub mod secret_send;
29mod to_device;
30mod utd_cause;
31
32use ruma::serde::Raw;
33pub use to_device::{ToDeviceCustomEvent, ToDeviceEvent, ToDeviceEvents};
34pub use utd_cause::{CryptoContextInfo, UtdCause};
35
36/// A trait for event contents to define their event type.
37pub trait EventType {
38 /// The event type of the event content.
39 const EVENT_TYPE: &'static str;
40
41 /// Get the event type of the event content.
42 ///
43 /// **Note**: This should never be implemented manually, this takes the
44 /// event type from the constant.
45 fn event_type(&self) -> &'static str {
46 Self::EVENT_TYPE
47 }
48}
49
50impl<T: EventType> EventType for Raw<T> {
51 const EVENT_TYPE: &'static str = T::EVENT_TYPE;
52}
53
54fn from_str<'a, T, E>(string: &'a str) -> Result<T, E>
55where
56 T: serde::Deserialize<'a>,
57 E: serde::de::Error,
58{
59 serde_json::from_str(string).map_err(serde::de::Error::custom)
60}