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