QueryParser

Struct QueryParser 

Source
pub struct QueryParser { /* private fields */ }
Expand description

查询参数解析器

负责解析查询字符串并提供类型安全的参数访问

Implementations§

Source§

impl QueryParser

Source

pub fn new(query: &str) -> ParseResult<Self>

创建新的查询参数解析器

§参数
  • query - 查询字符串,不包含前导的 ‘?’
§返回值

解析器实例,如果解析失败则返回错误

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("q=rust&page=2&tags=web&tags=backend").unwrap();
Source

pub fn from_params(params: HashMap<String, Vec<String>>) -> Self

从参数映射创建查询解析器

§参数
  • params - 参数映射
§返回值

解析器实例

Source

pub fn get(&self, key: &str) -> Option<&str>

获取单个参数值

§参数
  • key - 参数名
§返回值

参数的第一个值,如果参数不存在则返回 None

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("q=rust&page=2").unwrap();
assert_eq!(parser.get("q"), Some("rust"));
assert_eq!(parser.get("page"), Some("2"));
assert_eq!(parser.get("missing"), None);
Source

pub fn get_all(&self, key: &str) -> &[String]

获取多个参数值

§参数
  • key - 参数名
§返回值

参数的所有值,如果参数不存在则返回空切片

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("tags=web&tags=backend&tags=rust").unwrap();
let tags: Vec<&str> = parser.get_all("tags").iter().map(|s| s.as_str()).collect();
assert_eq!(tags, vec!["web", "backend", "rust"]);
Source

pub fn contains(&self, key: &str) -> bool

检查参数是否存在

§参数
  • key - 参数名
§返回值

如果参数存在则返回 true,否则返回 false

Source

pub fn get_parsed<T>(&self, key: &str) -> ParseResult<T>
where T: FromParam,

获取参数的类型安全值

§参数
  • key - 参数名
§返回值

解析后的值,如果参数不存在或解析失败则返回错误

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("page=2&limit=10").unwrap();
let page: u32 = parser.get_parsed("page").unwrap();
let limit: u32 = parser.get_parsed("limit").unwrap();
assert_eq!(page, 2);
assert_eq!(limit, 10);
Source

pub fn get_optional<T>(&self, key: &str) -> ParseResult<Option<T>>
where T: FromParam,

获取可选的类型安全值

§参数
  • key - 参数名
§返回值

解析后的可选值,如果参数不存在则返回 None,如果解析失败则返回错误

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("page=2").unwrap();
let page: Option<u32> = parser.get_optional("page").unwrap();
let limit: Option<u32> = parser.get_optional("limit").unwrap();
assert_eq!(page, Some(2));
assert_eq!(limit, None);
Source

pub fn get_with_default<T>(&self, key: &str, default: T) -> ParseResult<T>
where T: FromParam,

获取带默认值的类型安全值

§参数
  • key - 参数名
  • default - 默认值
§返回值

解析后的值,如果参数不存在则返回默认值,如果解析失败则返回错误

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("page=2").unwrap();
let page: u32 = parser.get_with_default("page", 1).unwrap();
let limit: u32 = parser.get_with_default("limit", 10).unwrap();
assert_eq!(page, 2);
assert_eq!(limit, 10);
Source

pub fn get_all_parsed<T>(&self, key: &str) -> ParseResult<Vec<T>>
where T: FromParam,

获取多值参数的类型安全值

§参数
  • key - 参数名
§返回值

解析后的值向量,如果任何值解析失败则返回错误

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("ids=1&ids=2&ids=3").unwrap();
let ids: Vec<u32> = parser.get_all_parsed("ids").unwrap();
assert_eq!(ids, vec![1, 2, 3]);
Source

pub fn set<T>(&mut self, key: &str, value: T)
where T: ToParam,

设置参数值

§参数
  • key - 参数名
  • value - 参数值
Source

pub fn add<T>(&mut self, key: &str, value: T)
where T: ToParam,

添加参数值(支持多值)

§参数
  • key - 参数名
  • value - 参数值
Source

pub fn remove(&mut self, key: &str) -> Option<Vec<String>>

移除参数

§参数
  • key - 参数名
§返回值

被移除的参数值,如果参数不存在则返回 None

Source

pub fn clear(&mut self)

清空所有参数

Source

pub fn keys(&self) -> Vec<&str>

获取所有参数名

§返回值

参数名的向量

Source

pub fn is_empty(&self) -> bool

检查是否为空

§返回值

如果没有任何参数则返回 true

Source

pub fn len(&self) -> usize

获取参数数量

§返回值

参数的数量(不是值的数量)

Source

pub fn format(&self) -> String

格式化为查询字符串

§返回值

格式化后的查询字符串,不包含前导的 ‘?’

§示例
use ruled_router::parser::QueryParser;

let parser = QueryParser::new("q=rust&page=2").unwrap();
let formatted = parser.format();
// 注意:HashMap 的迭代顺序不确定
assert!(formatted.contains("q=rust"));
assert!(formatted.contains("page=2"));
Source

pub fn raw(&self) -> &str

获取原始查询字符串

Source

pub fn params(&self) -> &HashMap<String, Vec<String>>

获取参数映射的引用

Trait Implementations§

Source§

impl Clone for QueryParser

Source§

fn clone(&self) -> QueryParser

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryParser

Source§

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

Formats the value using the given formatter. Read more

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<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.