pub enum LocationPatternKind {
Exact = 0,
Prefix = 1,
Regex = 2,
NormalPrefix = 3,
Common = 4,
}Expand description
位置模式类型,类似 Nginx location 配置
§示例
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
// 精确匹配
let pattern: LocationPattern = "= /api/v1".parse().unwrap();
assert_eq!(pattern.kind(), &LocationPatternKind::Exact);
assert!(pattern.is_match("/api/v1"));
assert!(!pattern.is_match("/api/v1/users"));
// 字面量前缀匹配
let pattern: LocationPattern = "^~ /static/".parse().unwrap();
assert_eq!(pattern.kind(), &LocationPatternKind::Prefix);
assert!(pattern.is_match("/static/css/style.css"));
assert!(!pattern.is_match("/images/logo.png"));
// 正则表达式匹配
let pattern: LocationPattern = r"~ ^/api/\d+$".parse().unwrap();
assert_eq!(pattern.kind(), &LocationPatternKind::Regex);
assert!(pattern.is_match("/api/123"));
assert!(!pattern.is_match("/api/abc"));
// 普通前缀匹配
let pattern: LocationPattern = "/uploads".parse().unwrap();
assert_eq!(pattern.kind(), &LocationPatternKind::NormalPrefix);
assert!(pattern.is_match("/uploads/file.jpg"));
// 通用匹配(根路径)
let pattern: LocationPattern = "/".parse().unwrap();
assert_eq!(pattern.kind(), &LocationPatternKind::Common);
assert!(pattern.is_match("/anything"));Variants§
Exact = 0
精确匹配 - 语法:= pattern
§Examples
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
let pattern: LocationPattern = "= /home".parse().unwrap();
assert!(matches!(pattern.kind(), LocationPatternKind::Exact));
assert!(pattern.is_match("/home"));
assert!(!pattern.is_match("/home/"));
assert!(!pattern.is_match("/home/user"));Prefix = 1
字面量前缀匹配 - 语法:^~ pattern
§Examples
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
let pattern: LocationPattern = "^~ /api".parse().unwrap();
assert!(matches!(pattern.kind(), LocationPatternKind::Prefix));
assert!(pattern.is_match("/api"));
assert!(pattern.is_match("/api/"));
assert!(pattern.is_match("/api/users"));
assert!(!pattern.is_match("/app"));Regex = 2
正则表达式匹配 - 语法:~ regex 或 ~* regex (不区分大小写)
§Examples
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
// 区分大小写的正则
let pattern: LocationPattern = "~ /api/\\d+".parse().unwrap();
assert!(matches!(pattern.kind(), LocationPatternKind::Regex));
assert!(pattern.is_match("/api/123"));
assert!(!pattern.is_match("/API/123"));
// 不区分大小写的正则
let pattern: LocationPattern = "~* /api/\\d+".parse().unwrap();
assert!(pattern.is_match("/api/123"));
assert!(pattern.is_match("/API/123"));NormalPrefix = 3
普通前缀匹配 - 语法:/xxx (以 / 开头的路径)
§Examples
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
let pattern: LocationPattern = "/admin".parse().unwrap();
assert!(matches!(pattern.kind(), LocationPatternKind::NormalPrefix));
assert!(pattern.is_match("/admin"));
assert!(pattern.is_match("/admin/"));
assert!(pattern.is_match("/admin/users"));
assert!(!pattern.is_match("/app"));Common = 4
通用匹配 - 语法:/ (根路径)
§Examples
use dhttp_access::pattern::{LocationPattern, LocationPatternKind};
let pattern: LocationPattern = "/".parse().unwrap();
assert!(matches!(pattern.kind(), LocationPatternKind::Common));
assert!(pattern.is_match("/"));
assert!(pattern.is_match("/anything"));
assert!(pattern.is_match("/deeply/nested/path"));Trait Implementations§
Source§impl Clone for LocationPatternKind
impl Clone for LocationPatternKind
Source§fn clone(&self) -> LocationPatternKind
fn clone(&self) -> LocationPatternKind
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for LocationPatternKind
Source§impl Debug for LocationPatternKind
impl Debug for LocationPatternKind
Source§impl<'de> Deserialize<'de> for LocationPatternKind
impl<'de> Deserialize<'de> for LocationPatternKind
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for LocationPatternKind
Source§impl Ord for LocationPatternKind
impl Ord for LocationPatternKind
Source§fn cmp(&self, other: &LocationPatternKind) -> Ordering
fn cmp(&self, other: &LocationPatternKind) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for LocationPatternKind
impl PartialEq for LocationPatternKind
Source§fn eq(&self, other: &LocationPatternKind) -> bool
fn eq(&self, other: &LocationPatternKind) -> bool
Tests for
self and other values to be equal, and is used by ==.Source§impl PartialOrd for LocationPatternKind
impl PartialOrd for LocationPatternKind
Source§impl Serialize for LocationPatternKind
impl Serialize for LocationPatternKind
impl StructuralPartialEq for LocationPatternKind
Auto Trait Implementations§
impl Freeze for LocationPatternKind
impl RefUnwindSafe for LocationPatternKind
impl Send for LocationPatternKind
impl Sync for LocationPatternKind
impl Unpin for LocationPatternKind
impl UnsafeUnpin for LocationPatternKind
impl UnwindSafe for LocationPatternKind
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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