Skip to main content

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_bundle;
27pub mod room_key_request;
28pub mod room_key_withheld;
29#[cfg(feature = "experimental-push-secrets")]
30pub mod secret_push;
31pub mod secret_send;
32mod to_device;
33mod utd_cause;
34
35use ruma::serde::Raw;
36pub use to_device::{ToDeviceCustomEvent, ToDeviceEvent, ToDeviceEvents};
37pub use utd_cause::{CryptoContextInfo, UtdCause};
38
39/// A trait for event contents to define their event type.
40pub trait EventType {
41    /// The event type of the event content.
42    const EVENT_TYPE: &'static str;
43
44    /// Get the event type of the event content.
45    ///
46    /// **Note**: This usually doesn't need to be implemented. The default
47    /// implementation will take the event type from the
48    /// [`EventType::EVENT_TYPE`] constant.
49    fn event_type(&self) -> &str {
50        Self::EVENT_TYPE
51    }
52}
53
54impl<T: EventType> EventType for Raw<T> {
55    const EVENT_TYPE: &'static str = T::EVENT_TYPE;
56}
57
58fn from_str<'a, T, E>(string: &'a str) -> Result<T, E>
59where
60    T: serde::Deserialize<'a>,
61    E: serde::de::Error,
62{
63    serde_json::from_str(string).map_err(serde::de::Error::custom)
64}