is_ipv6

Function is_ipv6 

Source
pub fn is_ipv6(s: &str) -> bool
Expand description

Checks if a string is a valid IPv6 address.

Performs basic IPv6 validation including:

  • Must contain colons (distinguishing from IPv4)
  • Supports brackets for URLs (e.g., [2001:db8::1])
  • Validates compressed notation with :: (at most one occurrence)
  • Each segment must be valid hexadecimal (1-4 characters)
  • At most 8 segments total

§Arguments

  • s - The string to validate as an IPv6 address

§Returns

true if the string is a valid IPv6 address, false otherwise

§Examples

use atproto_identity::validation::is_ipv6;

// Valid IPv6 addresses
assert!(is_ipv6("2001:db8::1"));
assert!(is_ipv6("::1"));
assert!(is_ipv6("fe80::1"));
assert!(is_ipv6("[2001:db8::1]")); // With brackets
assert!(is_ipv6("2001:0db8:0000:0000:0000:ff00:0042:8329"));

// Invalid IPv6 addresses
assert!(!is_ipv6("192.168.1.1")); // IPv4, not IPv6
assert!(!is_ipv6("example.com")); // No colons
assert!(!is_ipv6("2001:gggg::1")); // Invalid hex characters