Skip to content

Commit

Permalink
no need to feature-gate from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery committed Mar 27, 2024
1 parent 2912c17 commit 77ef338
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
7 changes: 3 additions & 4 deletions lorawan-encoding/Cargo.toml
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
aes = { version = "0.8", optional = true }
cmac = { version = "0.7", optional = true }
generic-array = "0"
hex = { version = "0", default-features = false, optional = true}
hex = { version = "0", default-features = false }
defmt = { version = "0.3", optional = true }
serde = { version = "1", default-features = false, features = ["derive"], optional = true}

Expand All @@ -28,10 +28,9 @@ harness = false

[features]
default = ["full"]
full = ["with-to-string", "with-from-str", "with-downlink", "default-crypto"]
full = ["with-to-string", "with-downlink", "default-crypto"]
default-crypto = ["aes", "cmac"]
with-to-string = ["hex"]
with-from-str = ["hex"]
with-to-string = []
with-downlink = []
serde = ["dep:serde"]
defmt = ["dep:defmt"]
2 changes: 0 additions & 2 deletions lorawan-encoding/src/extra/mod.rs
Expand Up @@ -9,5 +9,3 @@
pub extern crate std;

pub mod hasher;
#[cfg(any(feature = "with-to-string", feature = "with-from-str"))]
pub mod string;
1 change: 1 addition & 0 deletions lorawan-encoding/src/lib.rs
Expand Up @@ -17,6 +17,7 @@ pub mod maccommandcreator;
pub mod maccommands;
pub mod packet_length;
pub mod parser;
pub mod string;

#[cfg(feature = "full")]
pub mod extra;
Expand Down
Expand Up @@ -5,60 +5,72 @@
// copied, modified, or distributed except according to those terms.
//
// author: Ivaylo Petrov <ivajloip@gmail.com>

#[cfg(feature = "with-to-string")]
pub extern crate std;
use crate::keys::*;
use crate::parser::*;

macro_rules! fixed_len_struct_impl_string_msb {
macro_rules! fixed_len_struct_impl_to_string_msb {
(
$type:ident,$size:expr;
) => {
impl From<&str> for $type {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
Self::from(res)
}
}

#[cfg(feature = "with-to-string")]
impl std::string::ToString for $type {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
res.extend(std::iter::repeat('0').take($size * 2));
res.extend(std::iter::repeat('-').take($size * 2));
let slice = unsafe { &mut res.as_bytes_mut() };
hex::encode_to_slice(self.as_ref(), slice).unwrap();
res
}
}

impl From<&str> for $type {
};
(
$type:ident[$size:expr];
) => {
impl From<&str> for $type<[u8; $size]> {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
Self::from(res)
}
}
};
(
$type:ident[$size:expr];
) => {

#[cfg(feature = "with-to-string")]
impl<T: AsRef<[u8]>> std::string::ToString for $type<T> {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
res.extend(std::iter::repeat('0').take($size * 2));
res.extend(std::iter::repeat('-').take($size * 2));
let slice = unsafe { &mut res.as_bytes_mut() };
hex::encode_to_slice(self.as_ref(), slice).unwrap();
res
}
}
};
}

impl From<&str> for $type<[u8; $size]> {
macro_rules! fixed_len_struct_impl_string_lsb {
(
$type:ident,$size:expr;
) => {
impl From<&str> for $type {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
res.reverse();
Self::from(res)
}
}
};
}

macro_rules! fixed_len_struct_impl_string_lsb {
(
$type:ident,$size:expr;
) => {
#[cfg(feature = "with-to-string")]
impl std::string::ToString for $type {
fn to_string(&self) -> std::string::String {
let mut res = std::string::String::with_capacity($size * 2);
Expand All @@ -70,47 +82,38 @@ macro_rules! fixed_len_struct_impl_string_lsb {
res
}
}

impl From<&str> for $type {
fn from(s: &str) -> Self {
let mut res = [0; $size];
hex::decode_to_slice(s.as_bytes(), &mut res).unwrap();
res.reverse();
Self::from(res)
}
}
};
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
EUI64[8];
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
DevNonce[2];
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
AppNonce[3];
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
DevAddr[4];
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
NwkAddr[3];
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
AppKey, 16;
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
NewSKey, 16;
}

fixed_len_struct_impl_string_msb! {
fixed_len_struct_impl_to_string_msb! {
AppSKey, 16;
}

Expand All @@ -130,10 +133,10 @@ mod test {
#[test]
fn test_appskey_to_string() {
let appskey = AppSKey::from([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
0xEE, 0xFF,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfd, 0xb9, 0x75, 0x31, 0x24, 0x68,
0xac, 0xed,
]);
assert_eq!(appskey.to_string(), "00112233445566778899aabbccddeeff");
assert_eq!(appskey.to_string(), "0123456789abcdeffdb975312468aced");
}

#[test]
Expand All @@ -150,13 +153,13 @@ mod test {

#[test]
fn test_deveui_to_string() {
let deveui = DevEui::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]);
assert_eq!(deveui.to_string(), "7766554433221100");
let deveui = DevEui::from([0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12]);
assert_eq!(deveui.to_string(), "123456789abcdef0");
}

#[test]
fn test_deveui_from_str() {
let deveui = DevEui::from("7766554433221100");
assert_eq!(deveui, DevEui::from([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]));
let deveui = DevEui::from("123456789abcdef0");
assert_eq!(deveui, DevEui::from([0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12]));
}
}

0 comments on commit 77ef338

Please sign in to comment.