clia-macaddr
A Rust library for MAC address parsing, formatting, and calculation utilities.
Features
- Parse MAC addresses from string format
- Format MAC addresses to standard lowercase format
- Calculate the number of addresses between two MAC addresses
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Examples
use ;
// Parse a MAC address from string
let addr1 = parse_mac_addr.unwrap;
let addr2 = parse_mac_addr.unwrap;
// Format MAC address to lowercase
println!; // "aa:bb:cc:dd:ee:ff"
// Calculate number of addresses between two MAC addresses
let start = parse_mac_addr.unwrap;
let end = parse_mac_addr.unwrap;
let count = count_addresses_between;
println!; // 2 (inclusive)
// Calculate using string parameters directly (more convenient)
let count = count_addresses_between_str.unwrap;
println!; // 2 (inclusive)
// Works with larger ranges
let count = count_addresses_between_str.unwrap;
println!; // 256
// Error handling for invalid MAC addresses
match count_addresses_between_str
API Reference
parse_mac_addr(s: &str) -> Result<MacAddress, MacParseError>
Parses a MAC address from a string. Supports various formats including:
aa:bb:cc:dd:ee:ffAA:BB:CC:DD:EE:FF- And other formats supported by the
mac_addresscrate
format_mac_addr(addr: &MacAddress) -> String
Formats a MAC address to the standard lowercase colon-separated format: aa:bb:cc:dd:ee:ff
count_addresses_between(addr1: &MacAddress, addr2: &MacAddress) -> u64
Calculates the number of MAC addresses between two addresses (inclusive). The order of addresses doesn't matter - the function automatically handles both directions.
count_addresses_between_str(addr1: &str, addr2: &str) -> Result<u64, MacParseError>
A convenient version of count_addresses_between that accepts MAC address strings directly. Parses the strings internally and returns an error if either string is not a valid MAC address format. The order of addresses doesn't matter.
Dependencies
This library uses the mac_address crate for MAC address representation and parsing.
License
This project is licensed under the same terms as your choice.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.