pub struct DomainName(/* private fields */);net only.Expand description
A DNS domain name.
This type provides type-safe domain names with RFC 1035 validation.
It uses the newtype pattern with #[repr(transparent)] for zero-cost abstraction.
§Invariants
- Total length is 1-253 characters
- Each label is 1-63 characters
- Only ASCII letters, digits, and hyphens are allowed
- Labels cannot start or end with hyphens
- Labels CAN start with digits (unlike RFC 1123 hostnames)
- Stored in lowercase for case-insensitive comparison
§Examples
use bare_types::net::DomainName;
// Create a domain name
let domain = DomainName::new("example.com")?;
// Access the string representation
assert_eq!(domain.as_str(), "example.com");
// Check depth (number of labels)
assert_eq!(domain.depth(), 2);
// Check if it's a subdomain
let parent = DomainName::new("example.com")?;
let child = DomainName::new("www.example.com")?;
assert!(child.is_subdomain_of(&parent));
// Iterate over labels
let labels: Vec<&str> = domain.labels().collect();
assert_eq!(labels, vec!["example", "com"]);
// Parse from string
let domain: DomainName = "123.example.com".parse()?;Implementations§
Source§impl DomainName
impl DomainName
Sourcepub fn new(s: &str) -> Result<Self, DomainNameError>
pub fn new(s: &str) -> Result<Self, DomainNameError>
Creates a new domain name from a string.
§Errors
Returns DomainNameError if the string does not comply with RFC 1035.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("example.com")?;
assert_eq!(domain.as_str(), "example.com");
// Labels can start with digits (RFC 1035)
let domain = DomainName::new("123.example.com")?;
assert_eq!(domain.as_str(), "123.example.com");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the domain name as a string slice.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("example.com").unwrap();
assert_eq!(domain.as_str(), "example.com");Sourcepub const fn as_inner(&self) -> &String<253>
pub const fn as_inner(&self) -> &String<253>
Returns a reference to the underlying heapless::String.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("example.com").unwrap();
let inner: &heapless::String<253> = domain.as_inner();
assert_eq!(inner.as_str(), "example.com");Sourcepub fn into_inner(self) -> String<253>
pub fn into_inner(self) -> String<253>
Consumes this domain name and returns the underlying string.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("example.com").unwrap();
let inner = domain.into_inner();
assert_eq!(inner.as_str(), "example.com");Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Returns the depth (number of labels) of this domain name.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("example.com").unwrap();
assert_eq!(domain.depth(), 2);
let domain = DomainName::new("www.example.com").unwrap();
assert_eq!(domain.depth(), 3);Sourcepub fn is_subdomain_of(&self, other: &Self) -> bool
pub fn is_subdomain_of(&self, other: &Self) -> bool
Returns true if this domain name is a subdomain of other.
A domain name is considered a subdomain if it has more labels and ends with the parent domain name.
§Examples
use bare_types::net::DomainName;
let parent = DomainName::new("example.com").unwrap();
let child = DomainName::new("www.example.com").unwrap();
assert!(child.is_subdomain_of(&parent));
assert!(!parent.is_subdomain_of(&child));
assert!(!parent.is_subdomain_of(&parent));Sourcepub fn is_tld(&self) -> bool
pub fn is_tld(&self) -> bool
Returns true if this domain name is a top-level domain (TLD).
A TLD is a domain name with only one label.
§Examples
use bare_types::net::DomainName;
let tld = DomainName::new("com").unwrap();
assert!(tld.is_tld());
let domain = DomainName::new("example.com").unwrap();
assert!(!domain.is_tld());Sourcepub fn labels(&self) -> impl Iterator<Item = &str>
pub fn labels(&self) -> impl Iterator<Item = &str>
Returns an iterator over the labels in this domain name.
§Examples
use bare_types::net::DomainName;
let domain = DomainName::new("www.example.com").unwrap();
let labels: Vec<&str> = domain.labels().collect();
assert_eq!(labels, vec!["www", "example", "com"]);Trait Implementations§
Source§impl<'a> Arbitrary<'a> for DomainName
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for DomainName
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for DomainName
impl Clone for DomainName
Source§fn clone(&self) -> DomainName
fn clone(&self) -> DomainName
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more