Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
Vendored
+108
@@ -0,0 +1,108 @@
|
||||
use core::{fmt, str::FromStr};
|
||||
|
||||
use crate::{parser, MacAddr6, MacAddr8, ParseError};
|
||||
|
||||
/// A MAC address, either in *EUI-48* or *EUI-64* format.
|
||||
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
|
||||
pub enum MacAddr {
|
||||
V6(MacAddr6),
|
||||
V8(MacAddr8),
|
||||
}
|
||||
|
||||
impl MacAddr {
|
||||
/// Returns `true` if the address is `MacAddr6` address.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::{MacAddr, MacAddr6};
|
||||
/// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
|
||||
///
|
||||
/// assert_eq!(addr.is_v6(), true);
|
||||
/// assert_eq!(addr.is_v8(), false);
|
||||
/// ```
|
||||
pub fn is_v6(&self) -> bool {
|
||||
match self {
|
||||
MacAddr::V6(_) => true,
|
||||
MacAddr::V8(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is `MacAddr8` address.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::{MacAddr, MacAddr8};
|
||||
/// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB]);
|
||||
///
|
||||
/// assert_eq!(addr.is_v6(), false);
|
||||
/// assert_eq!(addr.is_v8(), true);
|
||||
/// ```
|
||||
pub fn is_v8(&self) -> bool {
|
||||
match self {
|
||||
MacAddr::V6(_) => false,
|
||||
MacAddr::V8(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a `MacAddr` address to a byte slice.
|
||||
///
|
||||
/// Length of the returned slice is depends on the enum member used.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::{MacAddr, MacAddr6};
|
||||
/// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
|
||||
///
|
||||
/// assert_eq!(addr.as_bytes(), &[0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
|
||||
/// ```
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
MacAddr::V6(addr) => addr.as_bytes(),
|
||||
MacAddr::V8(addr) => addr.as_bytes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MacAddr6> for MacAddr {
|
||||
fn from(addr: MacAddr6) -> Self {
|
||||
MacAddr::V6(addr)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MacAddr8> for MacAddr {
|
||||
fn from(addr: MacAddr8) -> Self {
|
||||
MacAddr::V8(addr)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for MacAddr {
|
||||
type Err = ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
parser::Parser::new(s).read_addr()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 6]> for MacAddr {
|
||||
fn from(bytes: [u8; 6]) -> Self {
|
||||
MacAddr::V6(MacAddr6::from(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 8]> for MacAddr {
|
||||
fn from(bytes: [u8; 8]) -> Self {
|
||||
MacAddr::V8(MacAddr8::from(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MacAddr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
MacAddr::V6(v6) => fmt::Display::fmt(v6, f),
|
||||
MacAddr::V8(v8) => fmt::Display::fmt(v8, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+229
@@ -0,0 +1,229 @@
|
||||
use core::{fmt, str::FromStr};
|
||||
|
||||
use crate::parser;
|
||||
|
||||
/// MAC address in *EUI-48* format.
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct MacAddr6([u8; 6]);
|
||||
|
||||
impl MacAddr6 {
|
||||
/// Creates a new `MacAddr6` address from the bytes.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB);
|
||||
/// ```
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
pub const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> MacAddr6 {
|
||||
MacAddr6([a, b, c, d, e, f])
|
||||
}
|
||||
|
||||
/// Create a new nil `MacAddr6`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::nil();
|
||||
/// assert!(addr.is_nil());
|
||||
/// ```
|
||||
pub const fn nil() -> MacAddr6 {
|
||||
MacAddr6([0x00; 6])
|
||||
}
|
||||
|
||||
/// Create a new broadcast `MacAddr6`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::broadcast();
|
||||
/// assert!(addr.is_broadcast());
|
||||
/// ```
|
||||
pub const fn broadcast() -> MacAddr6 {
|
||||
MacAddr6([0xFF; 6])
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is nil.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||
///
|
||||
/// assert_eq!(addr.is_nil(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub fn is_nil(&self) -> bool {
|
||||
self.0.iter().all(|&b| b == 0)
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is broadcast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
///
|
||||
/// assert_eq!(addr.is_broadcast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub fn is_broadcast(&self) -> bool {
|
||||
self.0.iter().all(|&b| b == 0xFF)
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is unicast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x00, 0x01, 0x44, 0x55, 0x66, 0x77);
|
||||
///
|
||||
/// assert_eq!(addr.is_unicast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_unicast(&self) -> bool {
|
||||
self.0[0] & 1 == 0
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is multicast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_multicast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_multicast(&self) -> bool {
|
||||
self.0[0] & 1 == 1
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is universally administered address (UAA).
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_universal(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_universal(&self) -> bool {
|
||||
self.0[0] & 1 << 1 == 0
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is locally administered (LAA).
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0x02, 0x00, 0x0C, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_local(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_local(&self) -> bool {
|
||||
self.0[0] & 1 << 1 == 2
|
||||
}
|
||||
|
||||
/// Converts a `MacAddr6` address to a byte slice.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67);
|
||||
///
|
||||
/// assert_eq!(addr.as_bytes(), &[0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
|
||||
/// ```
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Consumes `MacAddr6` address and returns raw bytes array.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67);
|
||||
///
|
||||
/// assert_eq!(addr.into_array(), [0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
|
||||
/// ```
|
||||
pub const fn into_array(self) -> [u8; 6] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for MacAddr6 {
|
||||
type Err = parser::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
parser::Parser::new(s).read_v6_addr()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 6]> for MacAddr6 {
|
||||
fn from(bytes: [u8; 6]) -> Self {
|
||||
MacAddr6(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for MacAddr6 {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsMut<[u8]> for MacAddr6 {
|
||||
fn as_mut(&mut self) -> &mut [u8] {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// `MacAddr6` can be displayed in different formats.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use macaddr::MacAddr6;
|
||||
/// let addr = MacAddr6::new(0xab, 0x0d, 0xef, 0x12, 0x34, 0x56);
|
||||
///
|
||||
/// assert_eq!(&format!("{}", addr), "AB:0D:EF:12:34:56");
|
||||
/// assert_eq!(&format!("{:-}", addr), "AB-0D-EF-12-34-56");
|
||||
/// assert_eq!(&format!("{:#}", addr), "AB0.DEF.123.456");
|
||||
/// ```
|
||||
impl fmt::Display for MacAddr6 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if f.sign_minus() {
|
||||
f.write_fmt(format_args!(
|
||||
"{:02X}-{:02X}-{:02X}-{:02X}-{:02X}-{:02X}",
|
||||
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5],
|
||||
))
|
||||
} else if f.alternate() {
|
||||
let p1 = u16::from(self.0[0]) * 16 + u16::from(self.0[1] / 16);
|
||||
let p2 = u16::from(self.0[1] % 16) * 256 + u16::from(self.0[2]);
|
||||
let p3 = u16::from(self.0[3]) * 16 + u16::from(self.0[4] / 16);
|
||||
let p4 = u16::from(self.0[4] % 16) * 256 + u16::from(self.0[5]);
|
||||
|
||||
f.write_fmt(format_args!("{:03X}.{:03X}.{:03X}.{:03X}", p1, p2, p3, p4,))
|
||||
} else {
|
||||
f.write_fmt(format_args!(
|
||||
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
|
||||
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+227
@@ -0,0 +1,227 @@
|
||||
use core::{fmt, str::FromStr};
|
||||
|
||||
use crate::parser;
|
||||
|
||||
/// MAC address in *EUI-64* format.
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct MacAddr8([u8; 8]);
|
||||
|
||||
impl MacAddr8 {
|
||||
/// Creates a new `MacAddr8` address from the bytes.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF);
|
||||
/// ```
|
||||
#[allow(clippy::many_single_char_names, clippy::too_many_arguments)]
|
||||
pub const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8, g: u8, h: u8) -> MacAddr8 {
|
||||
MacAddr8([a, b, c, d, e, f, g, h])
|
||||
}
|
||||
|
||||
/// Create a new nil `MacAddr8`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::nil();
|
||||
/// assert!(addr.is_nil());
|
||||
/// ```
|
||||
pub const fn nil() -> MacAddr8 {
|
||||
MacAddr8([0x00; 8])
|
||||
}
|
||||
|
||||
/// Create a new broadcast `MacAddr8`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::broadcast();
|
||||
/// assert!(addr.is_broadcast());
|
||||
/// ```
|
||||
pub const fn broadcast() -> MacAddr8 {
|
||||
MacAddr8([0xFF; 8])
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is nil.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||
///
|
||||
/// assert_eq!(addr.is_nil(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub fn is_nil(&self) -> bool {
|
||||
self.0.iter().all(|&b| b == 0)
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is broadcast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
///
|
||||
/// assert_eq!(addr.is_broadcast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub fn is_broadcast(&self) -> bool {
|
||||
self.0.iter().all(|&b| b == 0xFF)
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is unicast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x00, 0x01, 0x44, 0x55, 0x66, 0x77, 0xCD, 0xEF);
|
||||
///
|
||||
/// assert_eq!(addr.is_unicast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_unicast(&self) -> bool {
|
||||
self.0[0] & 1 == 0
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is multicast.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_multicast(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_multicast(&self) -> bool {
|
||||
self.0[0] & 1 == 1
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is universally administered address (UAA).
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_universal(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_universal(&self) -> bool {
|
||||
self.0[0] & 1 << 1 == 0
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is locally administered (LAA).
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0x02, 0x00, 0x0C, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC);
|
||||
///
|
||||
/// assert_eq!(addr.is_local(), true);
|
||||
/// ```
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
pub const fn is_local(&self) -> bool {
|
||||
self.0[0] & 1 << 1 == 2
|
||||
}
|
||||
|
||||
/// Converts a `MacAddr8` address to a byte slice.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB);
|
||||
///
|
||||
/// assert_eq!(addr.as_bytes(), &[0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB]);
|
||||
/// ```
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Consumes a `MacAddr8` address and returns raw bytes.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB);
|
||||
///
|
||||
/// assert_eq!(addr.into_array(), [0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB]);
|
||||
/// ```
|
||||
pub const fn into_array(self) -> [u8; 8] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for MacAddr8 {
|
||||
type Err = parser::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
parser::Parser::new(s).read_v8_addr()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; 8]> for MacAddr8 {
|
||||
fn from(bytes: [u8; 8]) -> Self {
|
||||
MacAddr8(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for MacAddr8 {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsMut<[u8]> for MacAddr8 {
|
||||
fn as_mut(&mut self) -> &mut [u8] {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// `MacAddr8` can be displayed in different formats.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use macaddr::MacAddr8;
|
||||
/// let addr = MacAddr8::new(0xab, 0x0d, 0xef, 0x12, 0x34, 0x56, 0x78, 0x9A);
|
||||
///
|
||||
/// assert_eq!(&format!("{}", addr), "AB:0D:EF:12:34:56:78:9A");
|
||||
/// assert_eq!(&format!("{:-}", addr), "AB-0D-EF-12-34-56-78-9A");
|
||||
/// assert_eq!(&format!("{:#}", addr), "AB0D.EF12.3456.789A");
|
||||
/// ```
|
||||
impl fmt::Display for MacAddr8 {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if f.sign_minus() {
|
||||
f.write_fmt(format_args!(
|
||||
"{:02X}-{:02X}-{:02X}-{:02X}-{:02X}-{:02X}-{:02X}-{:02X}",
|
||||
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7],
|
||||
))
|
||||
} else if f.alternate() {
|
||||
f.write_fmt(format_args!(
|
||||
"{:02X}{:02X}.{:02X}{:02X}.{:02X}{:02X}.{:02X}{:02X}",
|
||||
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7],
|
||||
))
|
||||
} else {
|
||||
f.write_fmt(format_args!(
|
||||
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
|
||||
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7],
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
//! This crate provides types for a [MAC address] identifiers,
|
||||
//! both in IEEE *EUI-48* and *EUI-64* formats.
|
||||
//!
|
||||
//! It is like a `std::net::SocketAddr` enum
|
||||
//! with `std::net::SocketAddrV4` and `std::net::SocketAddrV6` members,
|
||||
//! but for MAC addresses instead.
|
||||
//!
|
||||
//! Obviously, MAC address can be represented as a `[u8; 6]` or `[u8; 8]`,
|
||||
//! but it is error-prone and inconvenient, so here they are —
|
||||
//! [MacAddr6] and [MacAddr8] structs with helpful methods and
|
||||
//! standard Rust traits implementations to make them first-class
|
||||
//! Rust objects.
|
||||
//!
|
||||
//! ## Serde support
|
||||
//!
|
||||
//! [Serde] support can be enabled with a `"serde_std"` feature
|
||||
//! (disabled by default) if used in `std`-enabled builds.
|
||||
//!
|
||||
//! This feature is called like this because of [this Cargo bug].\
|
||||
//! `"serde"` feature is exists also, but it is intended to be used
|
||||
//! in the `no_std` builds.
|
||||
//!
|
||||
//! ## No-std support
|
||||
//!
|
||||
//! This crate can be used in a `no_std` builds with
|
||||
//! disabled `"std"` feature (enabled by default).
|
||||
//!
|
||||
//! Enabled `"serde"` feature will add support for `no_std`
|
||||
//! serde serialization and deserialization.
|
||||
//!
|
||||
//! [Serde]: https://serde.rs
|
||||
//! [MAC address]: https://en.wikipedia.org/wiki/MAC_address
|
||||
//! [this Cargo bug]: https://github.com/rust-lang/cargo/issues/3494
|
||||
//! [MacAddr6]: struct.MacAddr6.html
|
||||
//! [MacAddr8]: struct.MacAddr8.html
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![doc(html_root_url = "https://docs.rs/macaddr/1.0.0")]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod addr;
|
||||
mod addr6;
|
||||
mod addr8;
|
||||
mod parser;
|
||||
|
||||
pub use self::addr::MacAddr;
|
||||
pub use self::addr6::MacAddr6;
|
||||
pub use self::addr8::MacAddr8;
|
||||
pub use self::parser::ParseError;
|
||||
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
#[cfg(feature = "std")]
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use core::fmt;
|
||||
|
||||
use crate::{MacAddr, MacAddr6, MacAddr8};
|
||||
|
||||
/// An error which can be returned when parsing MAC address.
|
||||
///
|
||||
/// This error is used as the error type for the `FromStr` implementation
|
||||
/// for [MacAddr6] and [MacAddr8].
|
||||
///
|
||||
/// [MacAddr6]: ./struct.MacAddr6.html
|
||||
/// [MacAddr8]: ./struct.MacAddr8.html
|
||||
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
|
||||
pub enum ParseError {
|
||||
/// Provided string can't be parsed into the given type,
|
||||
/// because it is either too short or too long.
|
||||
///
|
||||
/// For example, any trailing symbols will result in the error,
|
||||
/// as in `"12-34-56-78-9A-BC\n"`.
|
||||
///
|
||||
/// This enum member will contain the provided string length when returned.
|
||||
InvalidLength(usize),
|
||||
|
||||
/// Invalid character occurred in the provided string.
|
||||
///
|
||||
/// Allowed characters are `0123456789abcdefABCDEF-:.`.
|
||||
///
|
||||
/// This enum member will contain the wrong char and it's position when returned.
|
||||
InvalidCharacter(char, usize),
|
||||
}
|
||||
|
||||
impl fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ParseError::InvalidLength(len) => f.write_fmt(format_args!("Invalid length of {} characters", len,)),
|
||||
ParseError::InvalidCharacter(chr, pos) => {
|
||||
f.write_fmt(format_args!("Unexpected character '{}' at position {}", chr, pos,))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for ParseError {}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
enum Delimiter {
|
||||
Hyphen,
|
||||
Colon,
|
||||
Dot,
|
||||
}
|
||||
|
||||
// Heavily based on the Rust' `std/net/parser.rs` sources.
|
||||
#[derive(Debug)]
|
||||
pub struct Parser<'a> {
|
||||
source: &'a [u8],
|
||||
pos: usize,
|
||||
delimiter: Option<Delimiter>,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
pub fn new(s: &'a str) -> Parser<'a> {
|
||||
Parser {
|
||||
source: s.as_bytes(),
|
||||
pos: 0,
|
||||
delimiter: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_eof(&self) -> bool {
|
||||
self.pos == self.source.len()
|
||||
}
|
||||
|
||||
fn move_next(&mut self) {
|
||||
if !self.is_eof() {
|
||||
self.pos += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn peek_char(&mut self) -> Option<char> {
|
||||
if self.is_eof() {
|
||||
None
|
||||
} else {
|
||||
Some(self.source[self.pos] as char)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_char(&mut self) -> Result<char, ParseError> {
|
||||
if self.is_eof() {
|
||||
Err(ParseError::InvalidLength(self.pos))
|
||||
} else {
|
||||
let r = self.source[self.pos] as char;
|
||||
self.pos += 1;
|
||||
Ok(r)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_digit(&mut self) -> Result<u8, ParseError> {
|
||||
let chr = self.read_char()?;
|
||||
|
||||
match chr as u8 {
|
||||
byte @ b'0'..=b'9' => Ok(byte - b'0'),
|
||||
byte @ b'a'..=b'f' => Ok(byte - b'a' + 10),
|
||||
byte @ b'A'..=b'F' => Ok(byte - b'A' + 10),
|
||||
_ => Err(ParseError::InvalidCharacter(chr, self.pos)),
|
||||
}
|
||||
}
|
||||
|
||||
fn probe_delimiter(&mut self) -> Result<Option<()>, ParseError> {
|
||||
match self.peek_char() {
|
||||
Some('-') if self.delimiter.is_none() => {
|
||||
self.delimiter = Some(Delimiter::Hyphen);
|
||||
Ok(Some(()))
|
||||
}
|
||||
Some('-') if self.delimiter != Some(Delimiter::Hyphen) => Err(ParseError::InvalidCharacter('-', self.pos)),
|
||||
Some('-') => Ok(Some(())),
|
||||
|
||||
Some(':') if self.delimiter.is_none() => {
|
||||
self.delimiter = Some(Delimiter::Colon);
|
||||
Ok(Some(()))
|
||||
}
|
||||
Some(':') if self.delimiter != Some(Delimiter::Colon) => Err(ParseError::InvalidCharacter(':', self.pos)),
|
||||
Some(':') => Ok(Some(())),
|
||||
|
||||
Some('.') if self.delimiter.is_none() => {
|
||||
self.delimiter = Some(Delimiter::Dot);
|
||||
Ok(Some(()))
|
||||
}
|
||||
Some('.') if self.delimiter != Some(Delimiter::Dot) => Err(ParseError::InvalidCharacter('.', self.pos)),
|
||||
Some('.') => Ok(Some(())),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_v6_addr(&mut self) -> Result<MacAddr6, ParseError> {
|
||||
let mut bytes = [0; 6];
|
||||
let mut i = 0;
|
||||
|
||||
while i < 6 {
|
||||
if self.probe_delimiter()?.is_some() {
|
||||
self.move_next();
|
||||
}
|
||||
|
||||
let mut digit = self.read_digit()? * 16;
|
||||
digit += self.read_digit()?;
|
||||
|
||||
bytes[i] = digit;
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if self.is_eof() {
|
||||
Ok(MacAddr6::from(bytes))
|
||||
} else {
|
||||
Err(ParseError::InvalidLength(self.source.len()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_v8_addr(&mut self) -> Result<MacAddr8, ParseError> {
|
||||
let mut bytes = [0; 8];
|
||||
let mut i = 0;
|
||||
|
||||
while i < 8 {
|
||||
if self.probe_delimiter()?.is_some() {
|
||||
self.move_next();
|
||||
}
|
||||
|
||||
let mut digit = self.read_digit()? * 16;
|
||||
digit += self.read_digit()?;
|
||||
|
||||
bytes[i] = digit;
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if self.is_eof() {
|
||||
Ok(MacAddr8::from(bytes))
|
||||
} else {
|
||||
Err(ParseError::InvalidLength(self.source.len()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_addr(&mut self) -> Result<MacAddr, ParseError> {
|
||||
match self.read_v6_addr() {
|
||||
Ok(addr) => return Ok(addr.into()),
|
||||
Err(err @ ParseError::InvalidCharacter(..)) => return Err(err),
|
||||
Err(ParseError::InvalidLength(..)) => {}
|
||||
}
|
||||
|
||||
// Rolling back to the start.
|
||||
self.pos = 0;
|
||||
|
||||
self.read_v8_addr().map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
Vendored
+176
@@ -0,0 +1,176 @@
|
||||
#[cfg(feature = "std")]
|
||||
use std::str::FromStr;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use core::str::FromStr;
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
|
||||
use crate::{MacAddr, MacAddr6, MacAddr8};
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_upper_case_canonical_format() {
|
||||
let addr = MacAddr6::from_str("12-34-56-78-9A-BC");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_lower_case_canonical_format() {
|
||||
let addr = MacAddr6::from_str("ab-cd-ef-ab-cd-ef");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_mixed_case_canonical_format() {
|
||||
let addr = MacAddr6::from_str("AB-cd-Ef-Ab-cD-EF");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_colon_format() {
|
||||
let addr = MacAddr6::from_str("12:34:56:78:9A:BC");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_cisco_format() {
|
||||
let addr = MacAddr6::from_str("1234.5678.9ABC");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_canonical_format() {
|
||||
let addr = MacAddr8::from_str("12-34-56-78-9A-BC-DE-F0");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_colon_format() {
|
||||
let addr = MacAddr8::from_str("12:34:56:78:9A:BC:DE:F0");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_canonical_format() {
|
||||
let addr = MacAddr::from_str("12-34-56-78-9A-BC-DE-F0");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
assert_matches!(addr, MacAddr::V8(..));
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_colon_format() {
|
||||
let addr = MacAddr::from_str("12:34:56:78:9A:BC:DE:F0");
|
||||
|
||||
assert!(addr.is_ok());
|
||||
let addr = addr.unwrap();
|
||||
assert_matches!(addr, MacAddr::V8(..));
|
||||
assert_eq!(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], addr.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_empty() {
|
||||
let addr = MacAddr6::from_str("");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_empty() {
|
||||
let addr = MacAddr8::from_str("");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_empty() {
|
||||
let addr = MacAddr::from_str("");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_partial_start() {
|
||||
let addr = MacAddr6::from_str("b-cd-ef-12-34-56");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_partial_start() {
|
||||
let addr = MacAddr8::from_str("b-cd-ef-12-34-56-78-9A");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_partial_end() {
|
||||
let addr = MacAddr6::from_str("ab-cd-ef-12-34-5");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_partial_end() {
|
||||
let addr = MacAddr8::from_str("ab-cd-ef-12-34-56-78-9");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_invalid_char() {
|
||||
let addr = MacAddr6::from_str("ab-Qd-ef-12-34-56");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_invalid_char() {
|
||||
let addr = MacAddr8::from_str("ab-Qd-ef-12-34-56-78-9A");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v6_different_delimiters() {
|
||||
let addr = MacAddr6::from_str("ab-cd:ef-12-34-56");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_v8_different_delimiters() {
|
||||
let addr = MacAddr8::from_str("ab-cd-ef-12-34-56-78:9A");
|
||||
|
||||
assert!(addr.is_err());
|
||||
}
|
||||
Reference in New Issue
Block a user