Create a new domain::Name, i.e. label
Returns the root label, i.e. no labels, can probably make this better in the future.
Returns true if there are no labels, i.e. it's empty.
In DNS the root is represented by .
use trust_dns_proto::rr::domain::Name;
let root = Name::root();
assert_eq!(&root.to_string(), ".");
Returns true if the name is a fully qualified domain name.
If this is true, it has effects like only querying for this single name, as opposed to building
up a search list in resolvers.
warning: this interface is unstable and may change in the future
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let name = Name::from_str("www").unwrap();
assert!(!name.is_fqdn());
let name = Name::from_str("www.example.com").unwrap();
assert!(!name.is_fqdn());
let name = Name::from_str("www.example.com.").unwrap();
assert!(name.is_fqdn());
Specifies this name is a fully qualified domain name
warning: this interface is unstable and may change in the future
Returns an iterator over the labels
Appends the label to the end of this name
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let name = Name::from_str("www.example").unwrap();
let name = name.append_label("com").unwrap();
assert_eq!(name, Name::from_str("www.example.com").unwrap());
Creates a new Name from the specified labels
labels
- vector of items which will be stored as Strings.
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let from_labels = Name::from_labels(vec!["www", "example", "com"]).unwrap();
assert_eq!(from_labels, Name::from_str("www.example.com").unwrap());
let from_labels = Name::from_labels(vec!["bad chars".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
assert_eq!(from_labels[0].as_bytes(), "bad chars".as_bytes());
let root = Name::from_labels(Vec::<&str>::new()).unwrap();
assert!(root.is_root());
Appends other
to self
, returning a new Name
Carries forward is_fqdn
from other
.
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let local = Name::from_str("www").unwrap();
let domain = Name::from_str("example.com").unwrap();
assert!(!domain.is_fqdn());
let name = local.clone().append_name(&domain);
assert_eq!(name, Name::from_str("www.example.com").unwrap());
assert!(!name.is_fqdn());
let domain = Name::from_str("example.com.").unwrap();
assert!(domain.is_fqdn());
let name = local.append_name(&domain);
assert_eq!(name, Name::from_str("www.example.com.").unwrap());
assert!(name.is_fqdn());
pub fn append_domain(self, domain: &Name) -> Name | [src] |
Appends the domain
to self
, making the new Name an FQDN
This is an alias for append_name with the added effect of marking the new Name as
a fully-qualified-domain-name.
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let local = Name::from_str("www").unwrap();
let domain = Name::from_str("example.com").unwrap();
let name = local.append_domain(&domain);
assert_eq!(name, Name::from_str("www.example.com").unwrap());
assert!(name.is_fqdn())
Creates a new Name with all labels lowercased
use std::cmp::Ordering;
use std::str::FromStr;
use trust_dns_proto::rr::domain::{Label, Name};
let example_com = Name::from_ascii("Example.Com").unwrap();
assert_eq!(example_com.cmp_case(&Name::from_str("example.com").unwrap()), Ordering::Less);
assert!(example_com.to_lowercase().eq_case(&Name::from_str("example.com").unwrap()));
Trims off the first part of the name, to help with searching for the domain piece
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let example_com = Name::from_str("example.com.").unwrap();
assert_eq!(example_com.base_name(), Name::from_str("com.").unwrap());
assert_eq!(Name::from_str("com.").unwrap().base_name(), Name::root());
assert_eq!(Name::root().base_name(), Name::root());
Trims to the number of labels specified
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let example_com = Name::from_str("example.com.").unwrap();
assert_eq!(example_com.trim_to(2), Name::from_str("example.com.").unwrap());
assert_eq!(example_com.trim_to(1), Name::from_str("com.").unwrap());
assert_eq!(example_com.trim_to(0), Name::root());
assert_eq!(example_com.trim_to(3), Name::from_str("example.com.").unwrap());
same as zone_of allows for case sensitive call
returns true if the name components of self are all present at the end of name
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let name = Name::from_str("www.example.com").unwrap();
let name = Name::from_str("www.example.com").unwrap();
let zone = Name::from_str("example.com").unwrap();
let another = Name::from_str("example.net").unwrap();
assert!(zone.zone_of(&name));
assert!(!name.zone_of(&zone));
assert!(!another.zone_of(&name));
Returns the number of labels in the name, discounting *
.
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let root = Name::root();
assert_eq!(root.num_labels(), 0);
let example_com = Name::from_str("example.com").unwrap();
assert_eq!(example_com.num_labels(), 2);
let star_example_com = Name::from_str("*.example.com.").unwrap();
assert_eq!(star_example_com.num_labels(), 2);
returns the length in bytes of the labels. '.' counts as 1
This can be used as an estimate, when serializing labels, they will often be compressed
and/or escaped causing the exact length to be different.
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
assert_eq!(Name::from_str("www.example.com.").unwrap().len(), 16);
assert_eq!(Name::from_str(".").unwrap().len(), 1);
assert_eq!(Name::root().len(), 1);
Returns whether the length of the labels, in bytes is 0. In practive, since '.' counts as
1, this is never the case so the method returns false.
attempts to parse a name such as "example.com."
or "subdomain.example.com."
use std::str::FromStr;
use trust_dns_proto::rr::domain::Name;
let name = Name::from_str("example.com.").unwrap();
assert_eq!(name.base_name(), Name::from_str("com.").unwrap());
assert_eq!(name[0].to_string(), "example");
Will convert the string to a name only allowing ascii as valid input
This method will also preserve the case of the name where that's desirable
use trust_dns_proto::rr::Name;
let bytes_name = Name::from_labels(vec!["WWW".as_bytes(), "example".as_bytes(), "COM".as_bytes()]).unwrap();
let ascii_name = Name::from_ascii("WWW.example.COM.").unwrap();
let lower_name = Name::from_ascii("www.example.com.").unwrap();
assert!(bytes_name.eq_case(&ascii_name));
assert!(!lower_name.eq_case(&ascii_name));
let bytes_name = Name::from_labels(vec!["email.name".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
let name = Name::from_ascii("email\\.name.example.com.").unwrap();
assert_eq!(bytes_name, name);
let bytes_name = Name::from_labels(vec!["bad.char".as_bytes(), "example".as_bytes(), "com".as_bytes()]).unwrap();
let name = Name::from_ascii("bad\\056char.example.com.").unwrap();
assert_eq!(bytes_name, name);
Will convert the string to a name using IDNA, punycode, to encode the UTF8 as necessary
When making names IDNA compatible, there is a side-effect of lowercasing the name.
use std::str::FromStr;
use trust_dns_proto::rr::Name;
let bytes_name = Name::from_labels(vec!["WWW".as_bytes(), "example".as_bytes(), "COM".as_bytes()]).unwrap();
let utf8_name = Name::from_str("WWW.example.COM.").unwrap();
let lower_name = Name::from_str("www.example.com.").unwrap();
assert!(!bytes_name.eq_case(&utf8_name));
assert!(lower_name.eq_case(&utf8_name));
First attempts to decode via from_utf8, if that fails IDNA checks, than falls back to
ascii decoding.
use std::str::FromStr;
use trust_dns_proto::rr::Name;
assert!(Name::from_utf8("_allows.example.com.").is_ok());
assert!(Name::from_utf8("dis_allowed.example.com.").is_err());
assert!(Name::from_str_relaxed("allow_in_.example.com.").is_ok());
Emits the canonical version of the name to the encoder.
In canonical form, there will be no pointers written to the encoder (i.e. no compression).
Writes the labels, as lower case, to the encoder
encoder
- encoder for writing this name
lowercase
- if true the name will be lowercased, otherwise it will not be changed when writing
Case sensitive comparison
Compares the Names, in a case sensitive manner
Converts this name into an ascii safe string.
If the name is an IDNA name, then the name labels will be returned with the xn--
prefix.
see to_utf8
or the Display
impl for methods which convert labels to utf8.
Converts the Name labels to the utf8 String form.
This converts the name to an unescaped format, that could be used with parse. If, the name is
is followed by the final .
, e.g. as in www.example.com.
, which represents a fully
qualified Name.
Returns true if the Name
is either localhost or in the localhost zone.
use std::str::FromStr;
use trust_dns_proto::rr::Name;
let name = Name::from_str("localhost").unwrap();
assert!(name.is_localhost());
let name = Name::from_str("localhost.").unwrap();
assert!(name.is_localhost());
let name = Name::from_str("my.localhost.").unwrap();
assert!(name.is_localhost());