Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: more cleanup of datanumber #99

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/variable_versions/data_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::convert::Into;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;

macro_rules! impl_try_from_data_number {
($($t:ty => $v:ident),*) => {
macro_rules! impl_try_from {
($($t:ty => $v:ident),*; $($s:ty => $sv:ident),*) => {
$(
impl TryFrom<&DataNumber> for $t {
type Error = DataNumberError;
Expand All @@ -26,13 +26,7 @@ macro_rules! impl_try_from_data_number {
}
}
}
)*
};
}

macro_rules! impl_try_from_field_value {
($($t:ty => $v:ident),*) => {
$(
impl TryFrom<&FieldValue> for $t {
type Error = FieldValueError;

Expand All @@ -47,6 +41,19 @@ macro_rules! impl_try_from_field_value {
}
}
)*

$(
impl TryFrom<&FieldValue> for $s {
type Error = FieldValueError;

fn try_from(value: &FieldValue) -> Result<Self, Self::Error> {
match value {
FieldValue::$sv(s) => Ok(s.clone()),
_ => Err(FieldValueError::InvalidDataType),
}
}
}
)*
};
}

Expand All @@ -69,22 +76,13 @@ pub enum DataNumberError {
InvalidDataType,
}

impl_try_from_data_number!(
impl_try_from!(
u8 => U8,
u16 => U16,
u32 => U32,
i32 => I32,
u64 => U64,
u128 => U128
);

impl_try_from_field_value!(
u8 => DataNumber,
u16 => DataNumber,
u32 => DataNumber,
i32 => DataNumber,
u64 => DataNumber,
u128 => DataNumber
u128 => U128;
);

impl TryFrom<&FieldValue> for String {
Expand Down Expand Up @@ -298,7 +296,7 @@ impl FieldValue {
pub fn to_be_bytes(&self) -> Vec<u8> {
match self {
FieldValue::String(s) => s.as_bytes().to_vec(),
FieldValue::DataNumber(d) => d.to_be_bytes().to_vec(),
FieldValue::DataNumber(d) => d.to_be_bytes(),
FieldValue::Float64(f) => f.to_be_bytes().to_vec(),
FieldValue::Duration(d) => (d.as_secs() as u32).to_be_bytes().to_vec(),
FieldValue::Ip4Addr(ip) => ip.octets().to_vec(),
Expand Down
Loading