1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 20:50:08 +01:00

139 lines
2.8 KiB
Rust
Raw Normal View History

2019-01-05 13:19:25 -08:00
//! Resource path matching library.
mod de;
mod path;
2019-02-09 07:24:35 -08:00
mod resource;
2019-01-05 13:19:25 -08:00
mod router;
pub use self::de::PathDeserializer;
pub use self::path::Path;
2019-02-09 07:24:35 -08:00
pub use self::resource::ResourceDef;
2019-01-05 22:00:38 -08:00
pub use self::router::{ResourceInfo, Router, RouterBuilder};
2019-01-05 13:19:25 -08:00
pub trait Resource<T: ResourcePath> {
fn resource_path(&mut self) -> &mut Path<T>;
}
pub trait ResourcePath {
2019-01-05 13:19:25 -08:00
fn path(&self) -> &str;
}
impl ResourcePath for String {
2019-01-05 13:19:25 -08:00
fn path(&self) -> &str {
self.as_str()
}
}
impl<'a> ResourcePath for &'a str {
2019-01-05 13:19:25 -08:00
fn path(&self) -> &str {
self
}
}
2019-12-07 10:39:33 +06:00
impl ResourcePath for bytestring::ByteString {
2019-01-05 13:19:25 -08:00
fn path(&self) -> &str {
&*self
}
}
2019-01-05 22:00:38 -08:00
2019-12-25 15:34:21 +04:00
/// Helper trait for type that could be converted to path pattern
2019-12-25 19:54:20 +04:00
pub trait IntoPattern {
2019-12-25 15:34:21 +04:00
/// Signle patter
fn is_single(&self) -> bool;
fn patterns(&self) -> Vec<String>;
}
2019-12-25 19:54:20 +04:00
impl IntoPattern for String {
2019-12-25 15:34:21 +04:00
fn is_single(&self) -> bool {
true
}
fn patterns(&self) -> Vec<String> {
vec![self.clone()]
}
}
2019-12-25 19:54:20 +04:00
impl<'a> IntoPattern for &'a str {
2019-12-25 15:34:21 +04:00
fn is_single(&self) -> bool {
true
}
fn patterns(&self) -> Vec<String> {
vec![self.to_string()]
}
}
2019-12-25 19:54:20 +04:00
impl<T: AsRef<str>> IntoPattern for Vec<T> {
2019-12-25 15:34:21 +04:00
fn is_single(&self) -> bool {
self.len() == 1
}
fn patterns(&self) -> Vec<String> {
self.into_iter().map(|v| v.as_ref().to_string()).collect()
}
}
2019-12-25 19:54:20 +04:00
macro_rules! array_patterns (($tp:ty, $num:tt) => {
impl IntoPattern for [$tp; $num] {
fn is_single(&self) -> bool {
$num == 1
}
fn patterns(&self) -> Vec<String> {
self.iter().map(|v| v.to_string()).collect()
}
}
});
array_patterns!(&str, 1);
array_patterns!(&str, 2);
array_patterns!(&str, 3);
array_patterns!(&str, 4);
array_patterns!(&str, 5);
array_patterns!(&str, 6);
array_patterns!(&str, 7);
array_patterns!(&str, 8);
array_patterns!(&str, 9);
array_patterns!(&str, 10);
array_patterns!(&str, 11);
array_patterns!(&str, 12);
array_patterns!(&str, 13);
array_patterns!(&str, 14);
array_patterns!(&str, 15);
array_patterns!(&str, 16);
array_patterns!(String, 1);
array_patterns!(String, 2);
array_patterns!(String, 3);
array_patterns!(String, 4);
array_patterns!(String, 5);
array_patterns!(String, 6);
array_patterns!(String, 7);
array_patterns!(String, 8);
array_patterns!(String, 9);
array_patterns!(String, 10);
array_patterns!(String, 11);
array_patterns!(String, 12);
array_patterns!(String, 13);
array_patterns!(String, 14);
array_patterns!(String, 15);
array_patterns!(String, 16);
2019-02-01 13:25:56 -08:00
#[cfg(feature = "http")]
mod url;
#[cfg(feature = "http")]
pub use self::url::{Quoter, Url};
2019-02-01 13:25:56 -08:00
2019-01-05 22:00:38 -08:00
#[cfg(feature = "http")]
mod http_support {
use super::ResourcePath;
2019-01-05 22:00:38 -08:00
use http::Uri;
impl ResourcePath for Uri {
2019-01-05 22:00:38 -08:00
fn path(&self) -> &str {
self.path()
}
}
}