### Installation Source: https://github.com/svartalf/rust-macaddr/blob/master/README.md Instructions on how to add the macaddr crate to your Rust project's dependencies. ```APIDOC ## Installation Add this to your `Cargo.toml` ```toml [dependencies] macaddr = "1.0" ``` ``` -------------------------------- ### MacAddr8: Format MAC Addresses to Strings Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates how to format a MacAddr8 instance into different string representations. The examples cover the default colon-separated format, hyphen-separated, and dot-separated (Cisco style) notations using format specifiers. ```rust use macaddr::MacAddr8; let addr = MacAddr8::new(0xAB, 0x0D, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x9A); // Default format: colon-separated assert_eq!(format("{}", addr), "AB:0D:EF:12:34:56:78:9A"); // Hyphen-separated format assert_eq!(format("{:-"}", addr), "AB-0D-EF-12-34-56-78-9A"); // Dot-separated format (Cisco style) assert_eq!(format("{:#}", addr), "AB0D.EF12.3456.789A"); ``` -------------------------------- ### Install macaddr dependency Source: https://github.com/svartalf/rust-macaddr/blob/master/README.md Add the macaddr crate to your project by including it in the dependencies section of your Cargo.toml file. ```toml [dependencies] macaddr = "1.0" ``` -------------------------------- ### MacAddr8: Parse MAC Addresses from Strings Source: https://context7.com/svartalf/rust-macaddr/llms.txt Provides examples of parsing 8-byte MAC addresses from various string formats, including colon-separated, hyphen-separated, and dot-separated (Cisco style). The `parse()` method handles these different notations, returning a MacAddr8 instance upon successful parsing. ```rust use macaddr::MacAddr8; // Parse colon-separated format let addr1: MacAddr8 = "AB:CD:EF:01:23:45:67:89".parse().unwrap(); // Parse hyphen-separated format let addr2: MacAddr8 = "AB-CD-EF-01-23-45-67-89".parse().unwrap(); // Parse dot-separated format let addr3: MacAddr8 = "ABCD.EF01.2345.6789".parse().unwrap(); assert_eq!(addr1, addr2); assert_eq!(addr2, addr3); ``` -------------------------------- ### Create MacAddr6 from Bytes Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates creating a 6-byte MAC address (EUI-48) using the `MacAddr6::new` constructor. Shows how to access the raw bytes and convert to an array. ```rust use macaddr::MacAddr6; // Create a MAC address from individual bytes let addr = MacAddr6::new(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB); // Access raw bytes assert_eq!(addr.as_bytes(), &[0x01, 0x23, 0x45, 0x67, 0x89, 0xAB]); // Convert to owned array let bytes: [u8; 6] = addr.into_array(); assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB]); ``` -------------------------------- ### Rust: MAC Address Comparison and Hashing Source: https://context7.com/svartalf/rust-macaddr/llms.txt Illustrates how MAC addresses in Rust implement standard comparison and hashing traits. This enables their use in collections like HashMap and HashSet, and allows for sorting. ```rust use macaddr::MacAddr6; use std::collections::{HashMap, HashSet}; let addr1 = MacAddr6::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55); let addr2 = MacAddr6::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x66); let addr3 = MacAddr6::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55); // Equality assert_eq!(addr1, addr3); assert_ne!(addr1, addr2); // Ordering assert!(addr1 < addr2); // Use in HashSet let mut seen: HashSet = HashSet::new(); seen.insert(addr1); seen.insert(addr2); assert!(seen.contains(&addr3)); // addr3 == addr1 // Use in HashMap let mut devices: HashMap = HashMap::new(); devices.insert(addr1, "router"); devices.insert(addr2, "switch"); assert_eq!(devices.get(&addr3), Some(&"router")); // Sorting let mut addrs = vec![addr2, addr1]; addrs.sort(); assert_eq!(addrs, vec![addr1, addr2]); ``` -------------------------------- ### Create Nil MacAddr6 Source: https://context7.com/svartalf/rust-macaddr/llms.txt Shows how to create a nil MAC address (all zeros) using `MacAddr6::nil`. Verifies its nil state and string representation. ```rust use macaddr::MacAddr6; let nil_addr = MacAddr6::nil(); assert!(nil_addr.is_nil()); assert_eq!(nil_addr.as_bytes(), &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); assert_eq!(format!("{}", nil_addr), "00:00:00:00:00:00"); ``` -------------------------------- ### MacAddr8: Create Nil and Broadcast Addresses Source: https://context7.com/svartalf/rust-macaddr/llms.txt Illustrates the creation of special 8-byte MAC addresses: the nil address (all zeros) and the broadcast address (all ones) using MacAddr8::nil() and MacAddr8::broadcast() respectively. It also shows how to verify their properties and their string representations. ```rust use macaddr::MacAddr8; // Nil address let nil_addr = MacAddr8::nil(); assert!(nil_addr.is_nil()); assert_eq!(format("{}", nil_addr), "00:00:00:00:00:00:00:00"); // Broadcast address let broadcast_addr = MacAddr8::broadcast(); assert!(broadcast_addr.is_broadcast()); assert_eq!(format("{}", broadcast_addr), "FF:FF:FF:FF:FF:FF:FF:FF"); ``` -------------------------------- ### Usage Source: https://github.com/svartalf/rust-macaddr/blob/master/README.md Guidance on how to use the MAC address types provided by the macaddr crate. ```APIDOC ## Usage Check out the [documentation](https://docs.rs/macaddr) for each type available, all of them have a plenty of examples. ``` -------------------------------- ### Create Broadcast MacAddr6 Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates creating a broadcast MAC address (all 0xFF bytes) using `MacAddr6::broadcast`. Verifies its broadcast state and string representation. ```rust use macaddr::MacAddr6; let broadcast_addr = MacAddr6::broadcast(); assert!(broadcast_addr.is_broadcast()); assert_eq!(broadcast_addr.as_bytes(), &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); assert_eq!(format!("{}", broadcast_addr), "FF:FF:FF:FF:FF:FF"); ``` -------------------------------- ### Create MacAddr6 from Byte Array Source: https://context7.com/svartalf/rust-macaddr/llms.txt Shows how to construct a `MacAddr6` directly from a 6-byte array using the `From` trait, providing a convenient way to convert raw byte data. ```rust use macaddr::MacAddr6; // Create from byte array let bytes: [u8; 6] = [0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]; let addr = MacAddr6::from(bytes); assert_eq!(addr.as_bytes(), &bytes); // Use AsRef/AsMut for byte slice access let slice: &[u8] = addr.as_ref(); assert_eq!(slice, &bytes); ``` -------------------------------- ### Rust: Serde Serialization for MAC Addresses Source: https://context7.com/svartalf/rust-macaddr/llms.txt Shows how to serialize and deserialize MAC addresses using the serde library when the `serde_std` feature is enabled. This allows MAC addresses to be easily converted to and from formats like JSON. ```rust // Cargo.toml: macaddr = { version = "1.0", features = ["serde_std"] } use macaddr::MacAddr6; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] struct NetworkDevice { name: String, mac: MacAddr6, } let device = NetworkDevice { name: "eth0".to_string(), mac: MacAddr6::new(0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E), }; // Serialize to JSON // let json = serde_json::to_string(&device).unwrap(); // Deserialize from JSON // let parsed: NetworkDevice = serde_json::from_str(&json).unwrap(); ``` -------------------------------- ### Format MacAddr6 to String Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates formatting a `MacAddr6` into different string notations (colon-separated, hyphen-separated, dot-separated) using Rust's formatting traits. ```rust use macaddr::MacAddr6; let addr = MacAddr6::new(0xAB, 0x0D, 0xEF, 0x12, 0x34, 0x56); // Default format: colon-separated assert_eq!(format!("{}", addr), "AB:0D:EF:12:34:56"); // Hyphen-separated format (using minus flag) assert_eq!(format!("{:-}", addr), "AB-0D-EF-12-34-56"); // Dot-separated format (Cisco style, using alternate flag) assert_eq!(format!("{:#}", addr), "AB0.DEF.123.456"); ``` -------------------------------- ### Serde Serialization Source: https://context7.com/svartalf/rust-macaddr/llms.txt Shows how to integrate MacAddr6 with Serde for JSON serialization and deserialization. ```APIDOC ## Serde Integration ### Description Enables serialization and deserialization of MAC addresses using the `serde` framework, useful for network configuration files or API payloads. ### Requirements - Enable `serde_std` or `serde` feature in `Cargo.toml`. ### Usage Derive `Serialize` and `Deserialize` on structs containing `MacAddr6` fields to automatically handle conversion to/from JSON or other formats. ``` -------------------------------- ### MacAddr8: Create EUI-64 MAC Address Source: https://context7.com/svartalf/rust-macaddr/llms.txt Shows how to create a new 8-byte MAC address (EUI-64 format) using the MacAddr8::new function by providing individual byte values. It also demonstrates accessing the raw bytes as a slice and converting the address into an owned byte array. ```rust use macaddr::MacAddr8; // Create an EUI-64 MAC address let addr = MacAddr8::new(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF); // Access raw bytes assert_eq!(addr.as_bytes(), &[0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); // Convert to owned array let bytes: [u8; 8] = addr.into_array(); assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]); ``` -------------------------------- ### ParseError Handling Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates how to handle and inspect parsing errors when converting strings to MacAddr6 objects. ```APIDOC ## Parsing MAC Addresses ### Description This section describes how to parse MAC address strings and handle potential errors such as invalid lengths or characters. ### Method Rust `parse()` trait ### Parameters #### Request Body - **input** (string) - Required - The MAC address string to parse (e.g., "00:11:22:33:44:55") ### Response #### Success Response (Ok) - **MacAddr6** (struct) - A valid 6-byte MAC address object. #### Error Response (Err) - **ParseError** (enum) - Contains variants like `InvalidLength` or `InvalidCharacter` providing details on the failure. ``` -------------------------------- ### License Source: https://github.com/svartalf/rust-macaddr/blob/master/README.md Information about the licensing of the macaddr crate. ```APIDOC ## License Licensed under either of [Apache License 2.0](https://github.com/svartalf/rust-macaddr/blob/master/LICENSE-APACHE) or [MIT license](https://github.com/svartalf/rust-macaddr/blob/master/LICENSE-MIT) at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. ``` -------------------------------- ### Comparison and Hashing Source: https://context7.com/svartalf/rust-macaddr/llms.txt Explains how MacAddr types implement standard traits for use in collections like HashMap and HashSet. ```APIDOC ## Comparison and Hashing ### Description MAC address types implement `Eq`, `PartialEq`, `Ord`, `PartialOrd`, and `Hash`, allowing them to be used as keys in maps or stored in sets. ### Features - **Equality**: Supports `==` and `!=` operators. - **Ordering**: Supports comparison operators for sorting. - **Collections**: Compatible with `std::collections::HashMap` and `std::collections::HashSet`. ``` -------------------------------- ### Rust: Handling MAC Address Parse Errors Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates how to handle various parsing errors when converting strings to MacAddr6 addresses. It covers InvalidLength and InvalidCharacter errors, as well as general error display. ```rust use macaddr::{MacAddr6, ParseError}; // InvalidLength error - string too short let result = "AB:CD:EF".parse::(); if let Err(ParseError::InvalidLength(len)) = result { println!("String has invalid length: {} characters", len); } // InvalidLength error - string too long let result = "AB:CD:EF:01:23:45:67".parse::(); if let Err(ParseError::InvalidLength(len)) = result { println!("String has invalid length: {} characters", len); } // InvalidCharacter error - unexpected character let result = "GG:CD:EF:01:23:45".parse::(); if let Err(ParseError::InvalidCharacter(chr, pos)) = result { println!("Invalid character '{}' at position {}", chr, pos); } // Mixed delimiters cause errors let result = "AB:CD-EF:01:23:45".parse::(); assert!(result.is_err()); // Display error messages let err = "invalid".parse::().unwrap_err(); println!("Error: {}", err); // "Invalid length of 7 characters" ``` -------------------------------- ### Parse MacAddr6 from String Source: https://context7.com/svartalf/rust-macaddr/llms.txt Illustrates parsing MAC addresses from various string formats (colon, hyphen, dot separated) using the `FromStr` trait. Includes error handling for invalid formats. ```rust use macaddr::MacAddr6; use std::str::FromStr; // Parse colon-separated format let addr1: MacAddr6 = "AB:CD:EF:01:23:45".parse().unwrap(); // Parse hyphen-separated format let addr2: MacAddr6 = "AB-CD-EF-01-23-45".parse().unwrap(); // Parse dot-separated format (Cisco style) let addr3: MacAddr6 = "ABCD.EF01.2345".parse().unwrap(); // All formats produce the same address assert_eq!(addr1, addr2); assert_eq!(addr2, addr3); // Using FromStr::from_str directly let addr4 = MacAddr6::from_str("ab:cd:ef:01:23:45").unwrap(); assert_eq!(addr1, addr4); // Case insensitive // Handle parse errors use macaddr::ParseError; let result = "invalid".parse::(); assert!(matches!(result, Err(ParseError::InvalidLength(_)))); let result = "GG:CD:EF:01:23:45".parse::(); assert!(matches!(result, Err(ParseError::InvalidCharacter('G', _)))); ``` -------------------------------- ### MacAddr Enum: Unified Handling of EUI-48 and EUI-64 Source: https://context7.com/svartalf/rust-macaddr/llms.txt Explains the MacAddr enum, which provides a unified type for handling both EUI-48 (MacAddr6) and EUI-64 (MacAddr8) MAC addresses. It shows how to create MacAddr instances from byte arrays, parse them from strings (automatically detecting format), inspect their variant, and convert specific types into the general MacAddr enum. ```rust use macaddr::{MacAddr, MacAddr6, MacAddr8}; // Create from 6-byte array (becomes MacAddr::V6) let addr6 = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]); assert!(addr6.is_v6()); assert!(!addr6.is_v8()); // Create from 8-byte array (becomes MacAddr::V8) let addr8 = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB]); assert!(addr8.is_v8()); assert!(!addr8.is_v6()); // Parse automatically detects format let parsed6: MacAddr = "AB:CD:EF:01:23:45".parse().unwrap(); assert!(parsed6.is_v6()); let parsed8: MacAddr = "AB:CD:EF:01:23:45:67:89".parse().unwrap(); assert!(parsed8.is_v8()); // Access bytes (slice length depends on variant) assert_eq!(addr6.as_bytes().len(), 6); assert_eq!(addr8.as_bytes().len(), 8); // Convert from specific types let specific = MacAddr6::new(0x01, 0x02, 0x03, 0x04, 0x05, 0x06); let general: MacAddr = specific.into(); assert!(general.is_v6()); ``` -------------------------------- ### MacAddr6: Check MAC Address Type (Unicast, Multicast, UAA, LAA) Source: https://context7.com/svartalf/rust-macaddr/llms.txt Demonstrates how to use bit flag methods on the MacAddr6 type to determine if a MAC address is unicast, multicast, universally administered (UAA), or locally administered (LAA). This is crucial for network protocol compliance and analysis. ```rust use macaddr::MacAddr6; // Unicast address (LSB of first byte is 0) let unicast = MacAddr6::new(0x00, 0x01, 0x44, 0x55, 0x66, 0x77); assert!(unicast.is_unicast()); assert!(!unicast.is_multicast()); // Multicast address (LSB of first byte is 1) let multicast = MacAddr6::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC); assert!(multicast.is_multicast()); assert!(!multicast.is_unicast()); // Universally administered address (UAA - bit 1 of first byte is 0) let universal = MacAddr6::new(0x01, 0x00, 0x0C, 0xCC, 0xCC, 0xCC); assert!(universal.is_universal()); assert!(!universal.is_local()); // Locally administered address (LAA - bit 1 of first byte is 1) let local = MacAddr6::new(0x02, 0x00, 0x0C, 0xCC, 0xCC, 0xCC); assert!(local.is_local()); assert!(!local.is_universal()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.