X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Frr.rs;h=1755685cabb527075526cb8ec14a28d490117c1a;hb=91c19b82695c29881ab5b809c681be786b942f09;hp=c3367a642d3f38044b9226ad20e80360a4fb5a92;hpb=8b836e295ec81c50907fe1f60ca43c82df792025;p=dnssec-prover diff --git a/src/rr.rs b/src/rr.rs index c3367a6..1755685 100644 --- a/src/rr.rs +++ b/src/rr.rs @@ -23,6 +23,27 @@ pub struct Name(String); impl Name { /// Gets the underlying human-readable domain name pub fn as_str(&self) -> &str { &self.0 } + /// Gets the number of labels in this name + pub fn labels(&self) -> u8 { + if self.as_str() == "." { + 0 + } else { + self.as_str().chars().filter(|c| *c == '.').count() as u8 + } + } + /// Gets a string containing the last `n` labels in this [`Name`] (which is also a valid name). + pub fn trailing_n_labels(&self, n: u8) -> Option<&str> { + let labels = self.labels(); + if n > labels { + None + } else if n == labels { + Some(self.as_str()) + } else if n == 0 { + Some(".") + } else { + self.as_str().splitn(labels as usize - n as usize + 1, ".").last() + } + } } impl core::ops::Deref for Name { type Target = str;