Skip to content

Commit

Permalink
Merge pull request #679 from althea-net/kenneth/serde_change
Browse files Browse the repository at this point in the history
Changed serialization for OperatorUpdateMessage
  • Loading branch information
kennethAltheaSystems authored Aug 12, 2022
2 parents 9f2f260 + 65bdf56 commit 70b2452
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions althea_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ arrayvec = {version= "0.7", features = ["serde"]}
phonenumber = "0.3"
lettre = {version = "0.10", features = ["serde"]}
ipnetwork = "0.20"
bincode = "1.3"

[dev-dependencies]
rand = "0.8"
Expand Down
57 changes: 56 additions & 1 deletion althea_types/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use babel_monitor::Route as RouteLegacy;
use clarity::Address;
use ipnetwork::IpNetwork;
use num256::Uint256;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::fmt::Display;
Expand Down Expand Up @@ -512,14 +514,41 @@ pub struct OperatorUpdateMessage {
pub local_update_instruction: Option<UpdateTypeLegacy>,
/// String that holds the download link to the latest firmware release
/// When a user hits 'update router', it updates to this version
/// #[serde(default = "default_shaper_settings")]
pub local_update_instruction_v2: Option<UpdateType>,
/// settings for the device bandwidth shaper
#[serde(default = "default_shaper_settings")]
pub shaper_settings: ShaperSettings,
/// Updated contact info from ops tools
// Updated contact info from ops tools
#[serde(
serialize_with = "data_serialize",
deserialize_with = "data_deserialize"
)]
pub contact_info: Option<ContactType>,
}

/// Serializes a ContactType as a string
pub fn data_serialize<S>(value: &Option<ContactType>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let string_value = serde_json::to_string(&value).unwrap_or_default();
serializer.serialize_str(&string_value)
}

/// Deserializes a string as a ContactType
pub fn data_deserialize<'de, D>(deserializer: D) -> Result<Option<ContactType>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer).unwrap_or_default();
let value: Option<ContactType> = match serde_json::from_str(&s) {
Ok(value) => value,
Err(e) => return Err(e).map_err(D::Error::custom),
};
Ok(value)
}

/// Settings for the bandwidth shaper
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct ShaperSettings {
Expand Down Expand Up @@ -744,3 +773,29 @@ pub struct HeartbeatMessage {
pub struct ExitSystemTime {
pub system_time: SystemTime,
}
#[cfg(test)]
mod test {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DummyStruct {
#[serde(
serialize_with = "data_serialize",
deserialize_with = "data_deserialize"
)]
contact: Option<ContactType>,
}
use lettre::Address;

use crate::{data_deserialize, data_serialize, ContactType};
#[test]
fn test_operator_update_serialize() {
let entry: DummyStruct = DummyStruct {
contact: Some(ContactType::Email {
email: Address::new("something", "1.1.1.1").unwrap(),
sequence_number: Some(0),
}),
};
let data = bincode::serialize(&entry).unwrap();
let _try_bincode: DummyStruct = bincode::deserialize(&data).unwrap();
}
}

0 comments on commit 70b2452

Please sign in to comment.