From 0e9ec519590efe1af05f812770a2388f7a04a3bb Mon Sep 17 00:00:00 2001 From: Felipe Diniello Date: Tue, 13 Jun 2023 21:22:15 +0200 Subject: [PATCH] create TYPES --- kairo-common/src/lib.rs | 14 ++++--- kairo-common/src/{ => types}/mac.rs | 53 ++++++++++++++++----------- kairo-common/src/types/mod.rs | 2 + kairo-common/src/{ => types}/point.rs | 0 4 files changed, 42 insertions(+), 27 deletions(-) rename kairo-common/src/{ => types}/mac.rs (58%) create mode 100644 kairo-common/src/types/mod.rs rename kairo-common/src/{ => types}/point.rs (100%) diff --git a/kairo-common/src/lib.rs b/kairo-common/src/lib.rs index 4d24d23..5da4a60 100644 --- a/kairo-common/src/lib.rs +++ b/kairo-common/src/lib.rs @@ -5,15 +5,17 @@ use serde::{Deserialize, Serialize}; pub mod helper; pub mod influxdb_models; -mod mac; pub mod unit_conversion; -pub type Antenna = antenna::Antenna; -pub type Point = point::Point; -pub type MAC = mac::MAC; +mod types; +pub type Point = types::point::Point; +pub type MAC = types::mac::MAC; + + +pub mod antenna; +pub type Antenna = antenna::Antenna; + -mod antenna; -mod point; #[derive(Debug, Serialize, Deserialize)] pub struct DeviceReport { diff --git a/kairo-common/src/mac.rs b/kairo-common/src/types/mac.rs similarity index 58% rename from kairo-common/src/mac.rs rename to kairo-common/src/types/mac.rs index 90016fe..e6bd8c5 100644 --- a/kairo-common/src/mac.rs +++ b/kairo-common/src/types/mac.rs @@ -1,10 +1,3 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::str::FromStr; - -use influxdb::Type; -use serde::de::{self, Visitor}; -use serde::{Deserialize, Serialize, Serializer}; - #[allow(clippy::upper_case_acronyms)] #[derive(Default, Clone, Copy, Hash, PartialEq, Eq)] pub struct MAC { @@ -21,7 +14,11 @@ impl MAC { } } -impl FromStr for MAC { +//////////////////////////////////////////////////// +// Standard implementations: +// + +impl std::str::FromStr for MAC { type Err = std::string::ParseError; fn from_str(s: &str) -> Result { let mut m = MAC::default(); @@ -30,25 +27,33 @@ impl FromStr for MAC { } } -impl Display for MAC { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { +impl std::fmt::Display for MAC { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", String::from_utf8_lossy(&self.s)) } } -impl Debug for MAC { - fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { +impl std::fmt::Debug for MAC { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", String::from_utf8_lossy(&self.s)) } } -impl From for Type { +//////////////////////////////////////////////////// +// Influx implementations: +// + +impl From for influxdb::Type { fn from(val: MAC) -> Self { - Type::Text(val.to_string()) + influxdb::Type::Text(val.to_string()) } } -impl<'de> Deserialize<'de> for MAC { +//////////////////////////////////////////////////// +// Serde implementations: +// + +impl<'de> serde::Deserialize<'de> for MAC { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -57,7 +62,7 @@ impl<'de> Deserialize<'de> for MAC { len: usize, } - impl<'de> Visitor<'de> for MACVisitor { + impl<'de> serde::de::Visitor<'de> for MACVisitor { type Value = MAC; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { write!(formatter, "a string containing at least {} bytes", self.len) @@ -65,12 +70,15 @@ impl<'de> Deserialize<'de> for MAC { fn visit_str(self, s: &str) -> Result where - E: de::Error, + E: serde::de::Error, { if s.len() == self.len { Ok(MAC::new(s)) } else { - Err(de::Error::invalid_value(de::Unexpected::Str(s), &self)) + Err(serde::de::Error::invalid_value( + serde::de::Unexpected::Str(s), + &self, + )) } } } @@ -80,10 +88,13 @@ impl<'de> Deserialize<'de> for MAC { } } -impl Serialize for MAC { - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> +impl serde::Serialize for MAC { + fn serialize( + &self, + serializer: S, + ) -> Result<::Ok, ::Error> where - S: Serializer, + S: serde::Serializer, { serializer.serialize_str(self.as_str()) } diff --git a/kairo-common/src/types/mod.rs b/kairo-common/src/types/mod.rs new file mode 100644 index 0000000..a14478f --- /dev/null +++ b/kairo-common/src/types/mod.rs @@ -0,0 +1,2 @@ +pub mod mac; +pub mod point; \ No newline at end of file diff --git a/kairo-common/src/point.rs b/kairo-common/src/types/point.rs similarity index 100% rename from kairo-common/src/point.rs rename to kairo-common/src/types/point.rs