Skip to main content

RadixRule

Enum RadixRule 

Source
pub enum RadixRule {
    Plain {
        frag: Bytes,
    },
    Param {
        frag: Bytes,
        name: Bytes,
    },
    Glob {
        frag: Bytes,
        glob: Pattern,
    },
    Regex {
        frag: Bytes,
        name: Bytes,
        expr: Regex,
    },
}
Expand description

An enum representing various matching patterns

Variants§

§

Plain

Plain rule that accepts arbitrary strings

§Syntax

  • /
  • /api

Fields

§frag: Bytes

fragment

§

Param

Named param matches a segment of the route

§Syntax

  • :
  • :id

Fields

§frag: Bytes

fragment

§name: Bytes

param’s name

§

Glob

Unix glob style matcher, note that it must be the last component of a route

§Syntax

Fields

§frag: Bytes

fragment

§glob: Pattern

glob pattern

§

Regex

Perl-like regular expressions

§Syntax

  • {}
  • {:}
  • {\d+}
  • {:\d+}
  • {id:\d+}

Fields

§frag: Bytes

fragment

§name: Bytes

regex’s name

§expr: Regex

the regex

Implementations§

Source§

impl RadixRule

Source

pub fn from_plain(frag: impl Into<Bytes>) -> RadixResult<Self>

Create a plain text rule

§Examples
use radixmap::{rule::RadixRule};

assert!(RadixRule::from_plain("").is_ok());
assert!(RadixRule::from_plain("id").is_ok());
Source

pub fn from_param(frag: impl Into<Bytes>) -> RadixResult<Self>

Create a named param rule

§Examples
use radixmap::{rule::RadixRule};

assert!(RadixRule::from_param(":").is_ok());   // segment placeholder
assert!(RadixRule::from_param(":id").is_ok()); // param with a name
assert!(RadixRule::from_param("").is_err());   // missing :
assert!(RadixRule::from_param("id").is_err()); // missing :
Source

pub fn from_glob(frag: impl Into<Bytes>) -> RadixResult<Self>

Create a unix glob style rule

§Examples
use radixmap::{rule::RadixRule};

assert!(RadixRule::from_glob("*").is_ok());      // match entire string
assert!(RadixRule::from_glob("*id").is_ok());    // match strings ending with 'id'
assert!(RadixRule::from_glob("").is_err());      // missing rule chars
assert!(RadixRule::from_glob("id").is_err());    // missing rule chars
Source

pub fn from_regex(frag: impl Into<Bytes>) -> RadixResult<Self>

Create a regular expression rule

§Examples
use radixmap::{rule::RadixRule};

assert!(RadixRule::from_regex(r"{}").is_ok());       // useless but valid
assert!(RadixRule::from_regex(r"{:}").is_ok());      // same as above
assert!(RadixRule::from_regex(r"{\d+}").is_ok());    // name is empty
assert!(RadixRule::from_regex(r"{:\d+}").is_ok());   // same as above
assert!(RadixRule::from_regex(r"{id:\d+}").is_ok()); // regex with a name
assert!(RadixRule::from_regex(r"").is_err());        // missing {}
assert!(RadixRule::from_regex(r"\d+").is_err());     // missing {}
assert!(RadixRule::from_regex(r"{").is_err());       // missing }
assert!(RadixRule::from_regex(r"{[0-9}").is_err());  // missing ]
assert!(RadixRule::from_regex(r"{:(0}").is_err());   // missing )
assert!(RadixRule::from_regex(r"{id:(0}").is_err()); // missing )

#[allow(invalid_from_utf8_unchecked)]
let invalid = format!("{{{}}}", unsafe { std::str::from_utf8_unchecked(&[0xffu8, 0xfe, 0x65]) });
assert!(RadixRule::from_regex(invalid).is_err());
Source

pub fn is_plain(&self) -> bool

Check if the rule is plain text

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("")?.is_plain(), true);
    assert_eq!(RadixRule::from_param(":id")?.is_plain(), false);
    assert_eq!(RadixRule::from_glob("*")?.is_plain(), false);
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.is_plain(), false);

    Ok(())
}
Source

pub fn is_special(&self) -> bool

Check if the rule is special

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("")?.is_special(), false);
    assert_eq!(RadixRule::from_param(":id")?.is_special(), true);
    assert_eq!(RadixRule::from_glob("*")?.is_special(), true);
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.is_special(), true);

    Ok(())
}
Source

pub fn longest<'u>(&self, path: &'u [u8], raw: bool) -> Option<&'u [u8]>

Match the path to find the longest shared segment

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("")?.longest(b"", false), Some("".as_bytes()));
    assert_eq!(RadixRule::from_plain("")?.longest(b"api", false), Some("".as_bytes()));
    assert_eq!(RadixRule::from_plain("api")?.longest(b"api", false), Some("api".as_bytes()));
    assert_eq!(RadixRule::from_plain("api/v1")?.longest(b"api", false), Some("api".as_bytes()));
    assert_eq!(RadixRule::from_plain("api/v1")?.longest(b"api/v2", false), Some("api/v".as_bytes()));
    assert_eq!(RadixRule::from_plain("roadmap/issues/events/6430295168")?.longest(b"roadmap/issues/events/6635165802", false), Some("roadmap/issues/events/6".as_bytes()));

    assert_eq!(RadixRule::from_param(":")?.longest(b"12345/rest", false), Some("12345".as_bytes()));
    assert_eq!(RadixRule::from_param(":id")?.longest(b"12345/rest", false), Some("12345".as_bytes()));
    assert_eq!(RadixRule::from_param(":id")?.longest(b"12345/rest", true), Some("".as_bytes()));
    assert_eq!(RadixRule::from_param(":id")?.longest(b":id", true), Some(":id".as_bytes()));

    assert_eq!(RadixRule::from_glob("*")?.longest(b"12345/rest", false), Some("12345/rest".as_bytes()));
    assert_eq!(RadixRule::from_glob("*id")?.longest(b"12345/rest", false), None);
    assert_eq!(RadixRule::from_glob("*id")?.longest(b"12345/rest", true), Some("".as_bytes()));
    assert_eq!(RadixRule::from_glob("*id")?.longest(b"*id", true), Some("*id".as_bytes()));

    assert_eq!(RadixRule::from_regex(r"{}")?.longest(b"12345/rest", false), Some(r"".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{:}")?.longest(b"12345/rest", false), Some(r"".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{\d+}")?.longest(b"12345/rest", false), Some(r"12345".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{:\d+}")?.longest(b"12345/rest", false), Some(r"12345".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.longest(b"12345/update", false), Some(r"12345".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.longest(b"abcde", false), None);
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.longest(b"abcde", true), Some(r"".as_bytes()));
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.longest(br"{id:\d+}", true), Some(r"{id:\d+}".as_bytes()));

    Ok(())
}
Source

pub fn divide(&mut self, len: usize) -> RadixResult<RadixRule>

Divide the rule into two parts

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    let mut rule = RadixRule::from_plain("/api")?;

    assert_eq!(rule.divide(1)?, "api");
    assert_eq!(rule, "/");

    assert!(RadixRule::from_param(":id")?.divide(1).is_err());
    assert!(RadixRule::from_glob("*")?.divide(1).is_err());
    assert!(RadixRule::from_regex(r"{id:\d+}")?.divide(1).is_err());

    Ok(())
}
Source

pub fn origin(&self) -> &Bytes

Origin fragment of the rule

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("/api")?.origin(), "/api");
    assert_eq!(RadixRule::from_param(":id")?.origin(), ":id");
    assert_eq!(RadixRule::from_glob("*")?.origin(), "*");
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.origin(), r"{id:\d+}");

    Ok(())
}
Source

pub fn identity(&self) -> &Bytes

The name of the named param and regex

§Examples
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_param(":id")?.identity(), "id");
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?.identity(), r"id");

    assert_eq!(RadixRule::from_plain("/api")?.identity(), "");
    assert_eq!(RadixRule::from_param(":")?.identity(), "");
    assert_eq!(RadixRule::from_glob("*")?.identity(), "*");
    assert_eq!(RadixRule::from_regex(r"{\d+}")?.identity(), r"");

    Ok(())
}

Trait Implementations§

Source§

impl Clone for RadixRule

Source§

fn clone(&self) -> RadixRule

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RadixRule

Debug trait

§Examples

use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(format!("{:?}", RadixRule::from_plain("/api")?).as_str(), "Plain(/api)");
    assert_eq!(format!("{:?}", RadixRule::from_param(":id")?).as_str(), "Param(:id)");
    assert_eq!(format!("{:?}", RadixRule::from_glob("*")?).as_str(), "Glob(*)");
    assert_eq!(format!("{:?}", RadixRule::from_regex(r"{id:\d+}")?).as_str(), r"Regex({id:\d+})");

    Ok(())
}
Source§

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

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

impl Default for RadixRule

Default trait

§Examples

use radixmap::{rule::RadixRule};

assert_eq!(RadixRule::default(), "");
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for RadixRule

== & !=

Source§

impl<V> From<RadixRule> for RadixNode<V>

Create a node from a rule

§Examples

use radixmap::{node::RadixNode, rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixNode::<()>::from(RadixRule::try_from("/api")?).rule, b"/api");
    assert_eq!(RadixNode::<()>::from(RadixRule::try_from(":id")?).rule, b":id");

    Ok(())
}
Source§

fn from(rule: RadixRule) -> Self

Converts to this type from the input type.
Source§

impl Hash for RadixRule

Hash trait

§Examples

use std::collections::HashMap;
use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    let mut map = HashMap::new();
    map.insert(RadixRule::from_plain("/api")?, "/api");
    map.insert(RadixRule::from_param(":id")?, ":id");
    map.insert(RadixRule::from_glob("*")?, "*");
    map.insert(RadixRule::from_regex(r"{id:\d+}")?, r"{id:\d+}");

    assert_eq!(map[&RadixRule::from_plain("/api")?], "/api");
    assert_eq!(map[&RadixRule::from_param(":id")?], ":id");
    assert_eq!(map[&RadixRule::from_glob("*")?], "*");
    assert_eq!(map[&RadixRule::from_regex(r"{id:\d+}")?], r"{id:\d+}");

    Ok(())
}
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 PartialEq for RadixRule

== & !=

§Examples

use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("/api")?, RadixRule::from_plain("/api")?);
    assert_eq!(RadixRule::from_param(":id")?, RadixRule::from_param(":id")?);
    assert_eq!(RadixRule::from_glob("*")?, RadixRule::from_glob("*")?);
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?, RadixRule::from_regex(r"{id:\d+}")?);

    assert_ne!(RadixRule::from_plain("/api")?, RadixRule::from_plain("")?);
    assert_ne!(RadixRule::from_param(":id")?, RadixRule::from_param(":")?);
    assert_ne!(RadixRule::from_glob("*")?, RadixRule::from_glob("**")?);
    assert_ne!(RadixRule::from_regex(r"{id:\d+}")?, RadixRule::from_regex(r"{}")?);

    // type mismatch
    assert_ne!(RadixRule::from_plain("{}")?, RadixRule::from_regex(r"{}")?);

    Ok(())
}
Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<const N: usize> PartialEq<&[u8; N]> for RadixRule

== & !=

Source§

fn eq(&self, other: &&[u8; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<&[u8]> for RadixRule

== & !=

§Examples

use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert_eq!(RadixRule::from_plain("/api")?, "/api");
    assert_eq!(RadixRule::from_param(":id")?, ":id");
    assert_eq!(RadixRule::from_glob("*")?, "*");
    assert_eq!(RadixRule::from_regex(r"{id:\d+}")?, r"{id:\d+}");

    assert_ne!(RadixRule::from_plain("/api")?, "");
    assert_ne!(RadixRule::from_param(":id")?, ":");
    assert_ne!(RadixRule::from_glob("*")?, "**");
    assert_ne!(RadixRule::from_regex(r"{id:\d+}")?, r"{}");

    Ok(())
}
Source§

fn eq(&self, other: &&[u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<&str> for RadixRule

== & !=

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 TryFrom<&'static [u8]> for RadixRule

Analyze a path as long as possible and construct a rule

Source§

type Error = RadixError

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

fn try_from(path: &'static [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&'static str> for RadixRule

Analyze a path as long as possible and construct a rule

Source§

type Error = RadixError

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

fn try_from(path: &'static str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Bytes> for RadixRule

Analyze a path as long as possible and construct a rule

§Examples

use radixmap::{rule::RadixRule, RadixResult};

fn main() -> RadixResult<()> {
    assert!(RadixRule::try_from("").is_err());

    assert_eq!(RadixRule::try_from("api")?, "api");
    assert_eq!(RadixRule::try_from("api/v1")?, "api/v1");
    assert_eq!(RadixRule::try_from("/api/v1")?, "/api/v1");

    assert_eq!(RadixRule::try_from(":")?, ":");
    assert_eq!(RadixRule::try_from(":id")?, ":id");
    assert_eq!(RadixRule::try_from(":id/rest")?, ":id");

    assert_eq!(RadixRule::try_from("*")?, "*");
    assert_eq!(RadixRule::try_from("*rest")?, "*rest");
    assert_eq!(RadixRule::try_from("*/rest")?, "*/rest");

    assert_eq!(RadixRule::try_from(r"{id:\d+}")?, r"{id:\d+}");
    assert_eq!(RadixRule::try_from(r"{id:\d+}/rest")?, r"{id:\d+}");
    assert!(RadixRule::try_from(r"{id:\d+").is_err());
    assert!(RadixRule::try_from(r"{id:\d+/rest").is_err());

    Ok(())
}
Source§

type Error = RadixError

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

fn try_from(path: Bytes) -> Result<Self, Self::Error>

Performs the conversion.

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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, 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.