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§
Implementations§
Source§impl RadixRule
impl RadixRule
Sourcepub fn from_plain(frag: impl Into<Bytes>) -> RadixResult<Self>
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());Sourcepub fn from_param(frag: impl Into<Bytes>) -> RadixResult<Self>
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 :Sourcepub fn from_glob(frag: impl Into<Bytes>) -> RadixResult<Self>
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 charsSourcepub fn from_regex(frag: impl Into<Bytes>) -> RadixResult<Self>
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());Sourcepub fn is_plain(&self) -> bool
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(())
}Sourcepub fn is_special(&self) -> bool
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(())
}Sourcepub fn longest<'u>(&self, path: &'u [u8], raw: bool) -> Option<&'u [u8]>
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(())
}Sourcepub fn divide(&mut self, len: usize) -> RadixResult<RadixRule>
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(())
}Sourcepub fn origin(&self) -> &Bytes
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(())
}Sourcepub fn identity(&self) -> &Bytes
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 Debug for RadixRule
Debug trait
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§impl Default for RadixRule
Default trait
impl Default for RadixRule
Default trait
§Examples
use radixmap::{rule::RadixRule};
assert_eq!(RadixRule::default(), "");impl Eq for RadixRule
== & !=
Source§impl<V> From<RadixRule> for RadixNode<V>
Create a node from a rule
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§impl Hash for RadixRule
Hash trait
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§impl PartialEq for RadixRule
== & !=
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§impl PartialEq<&[u8]> for RadixRule
== & !=
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§impl TryFrom<&'static [u8]> for RadixRule
Analyze a path as long as possible and construct a rule
impl TryFrom<&'static [u8]> for RadixRule
Analyze a path as long as possible and construct a rule
Source§impl TryFrom<&'static str> for RadixRule
Analyze a path as long as possible and construct a rule
impl TryFrom<&'static str> for RadixRule
Analyze a path as long as possible and construct a rule
Source§impl TryFrom<Bytes> for RadixRule
Analyze a path as long as possible and construct a rule
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(())
}Auto Trait Implementations§
impl !Freeze for RadixRule
impl RefUnwindSafe for RadixRule
impl Send for RadixRule
impl Sync for RadixRule
impl Unpin for RadixRule
impl UnsafeUnpin for RadixRule
impl UnwindSafe for RadixRule
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.