pub struct QueryParser { /* private fields */ }Expand description
查询参数解析器
负责解析查询字符串并提供类型安全的参数访问
Implementations§
Source§impl QueryParser
impl QueryParser
Sourcepub fn new(query: &str) -> ParseResult<Self>
pub fn new(query: &str) -> ParseResult<Self>
Sourcepub fn get_parsed<T>(&self, key: &str) -> ParseResult<T>where
T: FromParam,
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);Sourcepub fn get_optional<T>(&self, key: &str) -> ParseResult<Option<T>>where
T: FromParam,
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);Sourcepub fn get_with_default<T>(&self, key: &str, default: T) -> ParseResult<T>where
T: FromParam,
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);Sourcepub fn get_all_parsed<T>(&self, key: &str) -> ParseResult<Vec<T>>where
T: FromParam,
pub fn get_all_parsed<T>(&self, key: &str) -> ParseResult<Vec<T>>where
T: FromParam,
Trait Implementations§
Source§impl Clone for QueryParser
impl Clone for QueryParser
Source§fn clone(&self) -> QueryParser
fn clone(&self) -> QueryParser
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for QueryParser
impl RefUnwindSafe for QueryParser
impl Send for QueryParser
impl Sync for QueryParser
impl Unpin for QueryParser
impl UnwindSafe for QueryParser
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