2 unstable releases
Uses new Rust 2024
| 0.2.0 | Nov 18, 2025 |
|---|---|
| 0.1.0 | Aug 24, 2025 |
#158 in No standard library
8KB
141 lines
smac
A small MAC address parsing library in no_std Rust
The MacAddress struct is six bytes, and implements FromStr and
Display. The FromStr implementation expects a string of six hex bytes
unseparated, or separated by space (' ') or colon (':'). The following
is a basic example of parsing a MAC address using the ip tool on Linux.
use once_cell::sync::Lazy;
use regex::Regex;
use subprocess::Exec;
use smac::{MacAddress, ParseError};
fn read_mac(interface: &str) -> Result<MacAddress, ParseError> {
static MAC_PATTERN: Lazy<Regex> =
Lazy::new(|| Regex::new("link/ether ((([a-fA-F0-9]){2}[:]){5}[a-fA-F0-9]{2})").unwrap());
let command_result = match Exec::cmd("ip").args(&["link", "show", interface]).capture() {
Ok(capture) => capture.stdout_str(),
Err(_) => return Err(ParseError),
};
match MAC_PATTERN.captures(&command_result) {
Some(matches) => matches[1].parse::<MacAddress>(),
None => return Err(ParseError),
}
}
smac uses no_std and only the unit tests depend on alloc.
Dependencies
~250KB