create TYPES

This commit is contained in:
2023-06-13 21:22:15 +02:00
parent a5976252e8
commit 0e9ec51959
4 changed files with 42 additions and 27 deletions

View File

@@ -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 {

View File

@@ -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<Self, Self::Err> {
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<MAC> for Type {
////////////////////////////////////////////////////
// Influx implementations:
//
impl From<MAC> 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<D>(deserializer: D) -> Result<Self, D::Error>
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<E>(self, s: &str) -> Result<Self::Value, E>
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<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
impl serde::Serialize for MAC {
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where
S: Serializer,
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}

View File

@@ -0,0 +1,2 @@
pub mod mac;
pub mod point;