1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! EUI-48 and EUI-64 no-std implementation using heapless.
//!
//! # Example
//!
//! ```rust
//! use eui::Eui48;
//! use eui::Eui64;
//!
//! let eui48 = Eui48::from(85204980412143);
//! let eui64 = Eui64::from(eui48);
//!     
//! assert_eq!(eui48.to_string(), "4d7e54972eef");
//! assert_eq!(eui64.to_string(), "4d7e540000972eef");
//! ```
#![no_std]

#[cfg(feature = "serde")]
mod de;
#[cfg(feature = "serde")]
mod ser;

use core::fmt::{Display, Error, Formatter};
use heapless::consts::*;
use heapless::{String, Vec};

const HEX_CHARS: &[u8] = b"0123456789abcdef";

#[derive(Eq, PartialEq, Copy, Clone, Debug, hash32_derive::Hash32)]
pub struct Eui48([u8; 6]);
#[derive(Eq, PartialEq, Copy, Clone, Debug, hash32_derive::Hash32)]
pub struct Eui64([u8; 8]);

macro_rules! to_hex_string {
    ($eui: expr, $size: ty) => {{
        let mut vec = Vec::<u8, $size>::new();

        for &byte in $eui.0.iter() {
            vec.push(HEX_CHARS[(byte >> 4) as usize]).unwrap();
            vec.push(HEX_CHARS[(byte & 0xf) as usize]).unwrap();
        }

        unsafe { String::from_utf8_unchecked(vec) }
    }};
}

impl Eui48 {
    #[inline]
    pub fn to_string(&self) -> String<U12> {
        to_hex_string!(self, U12)
    }
}

impl Eui64 {
    #[inline]
    pub fn to_string(&self) -> String<U16> {
        to_hex_string!(self, U16)
    }
}

impl From<u64> for Eui48 {
    fn from(value: u64) -> Self {
        let b1: u8 = ((value >> 40) & 0xff) as u8;
        let b2: u8 = ((value >> 32) & 0xff) as u8;
        let b3: u8 = ((value >> 24) & 0xff) as u8;
        let b4: u8 = ((value >> 16) & 0xff) as u8;
        let b5: u8 = ((value >> 8) & 0xff) as u8;
        let b6: u8 = (value & 0xff) as u8;

        return Eui48([b1, b2, b3, b4, b5, b6]);
    }
}

impl From<u64> for Eui64 {
    fn from(value: u64) -> Self {
        Eui64(value.to_be_bytes())
    }
}

impl From<Eui48> for Eui64 {
    fn from(eui48: Eui48) -> Self {
        let mut data = [0u8; 8];

        for i in 0..6 {
            if i < 3 {
                data[i] = eui48.0[i]
            } else {
                data[i + 2] = eui48.0[i]
            }
        }

        Eui64(data)
    }
}

impl From<Eui48> for u64 {
    fn from(eui48: Eui48) -> Self {
        let data = eui48.0;

        ((data[0] as u64) << 40)
            + ((data[1] as u64) << 32)
            + ((data[2] as u64) << 24)
            + ((data[3] as u64) << 16)
            + ((data[4] as u64) << 8)
            + ((data[5] as u64) << 0)
    }
}

impl From<Eui64> for u64 {
    fn from(eui64: Eui64) -> Self {
        u64::from_be_bytes(eui64.0)
    }
}

impl Display for Eui48 {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.to_string())
    }
}

impl Display for Eui64 {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.to_string())
    }
}

#[test]
fn test_eui48_to_string() {
    let eui48 = Eui48::from(85204980412143);

    assert_eq!(eui48.to_string(), "4d7e54972eef")
}

#[test]
fn test_eui64_to_string() {
    let eui64 = Eui64::from(5583992946972634863);

    assert_eq!(eui64.to_string(), "4d7e540000972eef")
}

#[test]
fn test_eui48_to_eui64() {
    let eui48 = Eui48::from(85204980412143);
    let eui64 = Eui64::from(eui48);

    assert_eq!(eui64.to_string(), "4d7e540000972eef")
}

#[test]
fn test_u64_from_eui48() {
    let eui48 = Eui48::from(85204980412143);
    assert_eq!(u64::from(eui48), 85204980412143);
}

#[test]
fn test_u64_from_eui64() {
    let eui64 = Eui64::from(5583992946972634863);
    assert_eq!(u64::from(eui64), 5583992946972634863);
}

#[test]
fn test_hash_eui48() {
    use heapless::FnvIndexMap;

    let eui48 = Eui48::from(85204980412143);

    let mut fnv_index_map: FnvIndexMap<Eui48, u8, U1> = FnvIndexMap::new();
    fnv_index_map.insert(eui48, 1).unwrap();

    assert_eq!(1, *fnv_index_map.get(&eui48).unwrap())
}

#[test]
fn test_hash_eui64() {
    use heapless::FnvIndexMap;

    let eui64 = Eui64::from(5583992946972634863);

    let mut fnv_index_map: FnvIndexMap<Eui64, u8, U1> = FnvIndexMap::new();
    fnv_index_map.insert(eui64, 1).unwrap();

    assert_eq!(1, *fnv_index_map.get(&eui64).unwrap())
}