Struct Namespace

Source
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

Source

pub fn new(s: &str) -> Result<Self, NSErr>

let ns = Namespace::new("a::b::c").unwrap();
Source

pub fn len(&self) -> usize

Returns the number of segments.

Source

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");
Source

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");
Source

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"
);
Source

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().

Source

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)
);
Source

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.

Source

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"
);
Source

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",
);
Source

pub fn iter(&self) -> NamespaceIter<'_>

Trait Implementations§

Source§

impl Clone for Namespace

Source§

fn clone(&self) -> Namespace

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Namespace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Namespace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Namespace

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for Namespace

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, n: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl PartialEq for Namespace

Source§

fn eq(&self, other: &Namespace) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Namespace

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.