pub struct Namespace {
pub s: String,
pub segments: Vec<usize>,
}
Expand description
Namespace
cannot be empty. It always contains at least one segment.
Fields§
§s: String
§segments: Vec<usize>
Implementations§
Source§impl Namespace
impl Namespace
Sourcepub fn append(&self, other: &Namespace) -> Namespace
pub fn append(&self, other: &Namespace) -> Namespace
Appends a new namespace to the end of Self
. For example,
let ns1 = Namespace::new("a::b").unwrap();
let ns2 = Namespace::new("b::c").unwrap();
let ns = ns1.append(&ns2);
assert!(ns, "a::b::b::c");
Sourcepub fn append_at(&self, other: &Namespace, n: usize) -> Result<Namespace, NSErr>
pub fn append_at(&self, other: &Namespace, n: usize) -> Result<Namespace, NSErr>
Appends a new namespace at index. Returns an error if the n > self.len()
.
let ns1 = Namespace::new("a::b::c").unwrap();
let ns2 = Namespace::new("d").unwrap();
assert_eq!(ns1.append_at(&ns2, 1), "a::d");
Sourcepub fn remove(&self, n: usize) -> Result<Namespace, NSErr>
pub fn remove(&self, n: usize) -> Result<Namespace, NSErr>
Remove n
segments from the right-hand side of Self
. Namespaces cannot be empty so
returns an error if all segments are removed.
assert_eq!(
Namespace::new("a::b::c").unwrap()
.remove(2)
.unwrap()
.to_string(),
"a"
);
Sourcepub fn truncate(&self, n: usize) -> Result<Namespace, NSErr>
pub fn truncate(&self, n: usize) -> Result<Namespace, NSErr>
Truncate the namespace to the first n
segments. In other words truncate to length n
.
Returns an error if n > self.len()
.
Sourcepub fn sliding_match(&self, other: &Namespace) -> Vec<isize>
pub fn sliding_match(&self, other: &Namespace) -> Vec<isize>
Tries to find a sliding match. Returns the index of the position in self aligned to the
first segment of other. Note that this value can be negative. If there are no matches
returns an empty Vec
.
let ns1 = Namespace::new("a::b::c").unwrap();
let ns2 = Namespace::new("c::d").unwrap();
assert_eq!(
ns1.sliding_match(&ns),
vec!(2)
);
Sourcepub fn offset_match(&self, other: &Namespace, offset: isize) -> bool
pub fn offset_match(&self, other: &Namespace, offset: isize) -> bool
The offset is the index of the position in self aligned to the first segment of other. Returns true if all aligned segments match.
Sourcepub fn remainder(&self, other: &Namespace) -> Result<Namespace, NSErr>
pub fn remainder(&self, other: &Namespace) -> Result<Namespace, NSErr>
Returns the remainder of other
. If there is no match, multiple matches or no remainder,
returns an error.
let ns1 = Namespace::new("a::b::c").unwrap();
let ns2 = Namespace::new("c::d::e").unwrap();
assert_eq!(
ns.remainder(&ns2).unwrap().to_string(),
"d::e"
);
Sourcepub fn sliding_join(&self, other: &Namespace) -> Result<Namespace, NSErr>
pub fn sliding_join(&self, other: &Namespace) -> Result<Namespace, NSErr>
If there is a unique sliding_match, returns self
appended with the the remainder of
other
, otherwise returns an error.
let ns = Namespace::new("a::b").unwrap();
let other = Namespace::new("b::c").unwrap();
ns.sliding_join(&other);
assert_eq!(
ns.sliding_join(&other).unwrap().to_string(),
"a::b::c",
);