diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index f732cc19..a26a33be 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -35,7 +35,7 @@ use matrix_sdk_common::{ }, locks::Mutex, }; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_json::{json, Value}; use tracing::warn; @@ -57,18 +57,38 @@ use crate::{ }; /// A read-only version of a `Device`. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ReadOnlyDevice { user_id: Arc, - device_id: Arc>, + device_id: Arc, algorithms: Arc<[EventEncryptionAlgorithm]>, keys: Arc>, pub(crate) signatures: Arc>>, display_name: Arc>, deleted: Arc, + #[serde( + serialize_with = "local_trust_serializer", + deserialize_with = "local_trust_deserializer" + )] trust_state: Arc>, } +fn local_trust_serializer(x: &Atomic, s: S) -> Result +where + S: Serializer, +{ + let value = x.load(Ordering::SeqCst); + s.serialize_some(&value) +} + +fn local_trust_deserializer<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: Deserializer<'de>, +{ + let value = LocalTrust::deserialize(deserializer)?; + Ok(Arc::new(Atomic::new(value))) +} + #[derive(Debug, Clone)] /// A device represents a E2EE capable client of an user. pub struct Device {