pub struct Label<'a> { /* private fields */ }Expand description
Represents a label in a domain name
A label is a sequence of 1 to 63 octets. On the wire it can hold any octet, including dots (RFC 1035 section 3.1), so no escaping exists in the binary format.
What counts as a valid label depends on the role it plays in a name:
| Method | Role | Rule |
|---|---|---|
Label::is_valid_hostname | host name | letters, digits and hyphens |
Label::is_valid_service_label | service name | _ + RFC 6335 service name |
Label::is_valid_instance_label | DNS-SD instance or subtype | any content |
Label::new only checks the length. To build labels with content that is not valid for a
host name (unicode, dots, spaces), use Label::new or Label::new_unchecked and
Name::new_with_labels.
The Display implementation uses std::string::String::from_utf8_lossy to display the
label, and escapes . as \. and \ as \\, so that labels joined in a Name
can be told apart from the dots that separate them (RFC 6763 section 4.3).
Implementations§
Source§impl<'a> Label<'a>
impl<'a> Label<'a>
Sourcepub fn new<T: Into<Cow<'a, [u8]>>>(data: T) -> Result<Self>
pub fn new<T: Into<Cow<'a, [u8]>>>(data: T) -> Result<Self>
Create a new Label from text that may contain escape sequences.
The escapes follow the presentation format of RFC 1035 section 5.1 and RFC 4343 section 2.1:
\DDD, exactly three decimal digits (at most255), is the octet with that value\X, where X is not a digit, is the character X (\.and\\included)
Returns SimpleDnsError::InvalidServiceLabel
if the label is empty or longer than 63 octets after unescaping, or if an escape is
malformed (a trailing \, or \ followed by fewer than three digits or a value above 255).
The content is not checked against a naming rule. Use the is_valid_* methods for that.
let label = Label::new(r"My\.Device".as_bytes()).unwrap();
assert_eq!(b"My.Device", label.as_ref());
assert!(Label::new(r"bad\1".as_bytes()).is_err());Sourcepub fn new_unchecked<T: Into<Cow<'a, [u8]>>>(data: T) -> Self
pub fn new_unchecked<T: Into<Cow<'a, [u8]>>>(data: T) -> Self
Create a new Label from raw bytes, without unescaping and without checking for size limits or valid content. This function can be used to create labels with unicode characters
Sourcepub fn into_owned<'b>(self) -> Label<'b>
pub fn into_owned<'b>(self) -> Label<'b>
Transforms the inner data into its owned type
Sourcepub fn is_valid_hostname(&self) -> bool
pub fn is_valid_hostname(&self) -> bool
Returns true if the label is a valid host name label (RFC 952, RFC 1123).
It has 1 to 63 octets, made only of ASCII letters, digits and hyphens, and starts and ends with a letter or a digit.
assert!(Label::new_unchecked(&b"my-host1"[..]).is_valid_hostname());
assert!(!Label::new_unchecked(&b"_srv"[..]).is_valid_hostname());
assert!(!Label::new_unchecked(&b"-host"[..]).is_valid_hostname());Sourcepub fn is_valid_service_label(&self) -> bool
pub fn is_valid_service_label(&self) -> bool
Returns true if the label is a valid DNS-SD service label, such as _http
(RFC 6763 section 7).
It is an underscore followed by a service name as defined in RFC 6335 section 5.1: 1 to 15 ASCII letters, digits or hyphens, with at least one letter, that does not begin or end with a hyphen and has no adjacent hyphens.
This only checks the service name. The _tcp or _udp label that follows it is checked
by Name::is_valid_service_name.
assert!(Label::new_unchecked(&b"_http"[..]).is_valid_service_label());
assert!(Label::new_unchecked(&b"_9p"[..]).is_valid_service_label());
assert!(!Label::new_unchecked(&b"http"[..]).is_valid_service_label());
assert!(!Label::new_unchecked(&b"_123"[..]).is_valid_service_label());Sourcepub fn is_valid_instance_label(&self) -> bool
pub fn is_valid_instance_label(&self) -> bool
Returns true if the label can be used as a DNS-SD instance name or subtype
(RFC 6763 sections 4.3 and 7.1).
Any content is accepted, including dots, spaces and UTF-8, as long as the label has 1 to 63 octets. The limit counts octets, not characters.
assert!(Label::new_unchecked(&b"My Printer. Floor 2"[..]).is_valid_instance_label());
assert!(!Label::new_unchecked(&b""[..]).is_valid_instance_label());