From 14575892bd301477cf4f96e63c7933312aeb88fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 18 Jan 2021 13:19:13 +0100 Subject: [PATCH] crypto: Implement serialize/deserialize for devices. --- matrix_sdk_crypto/src/identities/device.rs | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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 {