Add to_string and from_string methods to network types

This commit is contained in:
Sosthene 2024-07-19 22:46:07 +02:00 committed by Nicolas Cantu
parent 066410ec63
commit 74e901cd46

View File

@ -105,6 +105,10 @@ impl CipherMessage {
error: None,
}
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, Serialize, Deserialize, Tsify)]
@ -121,6 +125,15 @@ impl AnkNetworkMsg {
content: raw.into(),
}
}
pub fn from_string(json: &str) -> Result<Self> {
let res: Self = serde_json::from_str(json)?;
Ok(res)
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Tsify, Clone)]
@ -178,6 +191,15 @@ impl CachedMessage {
new
}
pub fn from_string(json: &str) -> Result<Self> {
let res = serde_json::from_str(json)?;
Ok(res)
}
pub fn to_string(&self) -> String {
serde_json::to_string(self).unwrap()
}
pub fn try_decrypt_cipher(&self, cipher: Vec<u8>) -> Result<Vec<u8>> {
if self.ciphertext.is_some() || self.shared_secret.is_none() {
return Err(Error::msg(