crypto: Implement serialize/deserialize for devices.

master
Damir Jelić 2021-01-18 13:19:13 +01:00
parent 43a74524c5
commit 14575892bd
1 changed files with 23 additions and 3 deletions

View File

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