clia-macaddr 0.1.0

Utility methods for MAC address.
Documentation
  • Coverage
  • 80%
    4 out of 5 items documented0 out of 4 items with examples
  • Size
  • Source code size: 9.37 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.37 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 21s Average build duration of successful builds.
  • all releases: 22s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • clia/clia-util
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • clia

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:

[dependencies]
clia-macaddr = "0.1.0"

Examples

use clia_macaddr::{parse_mac_addr, format_mac_addr, count_addresses_between, count_addresses_between_str};

// Parse a MAC address from string
let addr1 = parse_mac_addr("aa:bb:cc:dd:ee:ff").unwrap();
let addr2 = parse_mac_addr("AA:BB:CC:DD:EE:FF").unwrap();

// Format MAC address to lowercase
println!("{}", format_mac_addr(&addr1)); // "aa:bb:cc:dd:ee:ff"

// Calculate number of addresses between two MAC addresses
let start = parse_mac_addr("00:00:00:00:00:00").unwrap();
let end = parse_mac_addr("00:00:00:00:00:01").unwrap();
let count = count_addresses_between(&start, &end);
println!("Addresses between: {}", count); // 2 (inclusive)

// Calculate using string parameters directly (more convenient)
let count = count_addresses_between_str("00:00:00:00:00:00", "00:00:00:00:00:01").unwrap();
println!("Addresses between: {}", count); // 2 (inclusive)

// Works with larger ranges
let count = count_addresses_between_str("00:00:00:00:00:00", "00:00:00:00:00:ff").unwrap();
println!("Addresses in range: {}", count); // 256

// Error handling for invalid MAC addresses
match count_addresses_between_str("invalid", "00:00:00:00:00:01") {
    Ok(count) => println!("Count: {}", count),
    Err(e) => println!("Parse error: {}", e),
}

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:ff
  • AA:BB:CC:DD:EE:FF
  • And other formats supported by the mac_address crate

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.