Skip to main content

LocationPatternKind

Enum LocationPatternKind 

Source
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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Copy for LocationPatternKind

Source§

impl Debug for LocationPatternKind

Source§

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

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

impl<'de> Deserialize<'de> for LocationPatternKind

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for LocationPatternKind

Source§

impl Ord for LocationPatternKind

Source§

fn cmp(&self, other: &LocationPatternKind) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for LocationPatternKind

Source§

fn eq(&self, other: &LocationPatternKind) -> 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 PartialOrd for LocationPatternKind

Source§

fn partial_cmp(&self, other: &LocationPatternKind) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for LocationPatternKind

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for LocationPatternKind

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.