pub enum Query {
Exact(String, bool),
StartsWith(String, bool),
Contains(String, bool),
EndsWith(String, bool),
}Expand description
Represents different ways to match input text.
Exact(String, bool)matches if the text is exactly equal to the query string.StartsWith(String, bool)matches if the text starts with the query string.Contains(String, bool)matches if the text contains the query string.EndsWith(String, bool)matches if the text ends with the query string.
The bool field indicates whether the match should be case-sensitive (true)
or case-insensitive (false). By default, it is case-insensitive unless the
query string is prefixed with c: (for “case-sensitive”) or i: (for “case-insensitive”).
Variants§
Exact(String, bool)
Variant for an exact match. The second parameter specifies if it’s case-sensitive.
StartsWith(String, bool)
Variant for matching if the text starts with the query. The second parameter specifies if it’s case-sensitive.
Contains(String, bool)
Variant for matching if the text contains the query. The second parameter specifies if it’s case-sensitive.
EndsWith(String, bool)
Variant for matching if the text ends with the query. The second parameter specifies if it’s case-sensitive.
Implementations§
Source§impl Query
impl Query
Sourcepub fn matches(&self, text: &str) -> bool
pub fn matches(&self, text: &str) -> bool
Checks whether the given text matches this query.
By default, matches are case-insensitive unless specified otherwise in the query.
If case_sensitive is true, the match must respect exact casing.
§Arguments
text- The text to check against the query.
§Returns
Returns true if the text matches according to the query variant (respecting case sensitivity),
or false otherwise.
§Examples
// Case-insensitive exact match example:
let query = Query::from_str("=Hello").unwrap();
assert!(query.matches("hello"));
// Case-sensitive contains example:
let query = Query::from_str("c:ell").unwrap();
assert!(query.matches("Well")); // false
assert!(query.matches("ell")); // true if text is exactly "ell"Trait Implementations§
Source§impl FromStr for Query
impl FromStr for Query
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a string slice into a Query with optional case sensitivity.
The parsing follows a simple syntax:
-
Case Sensitivity:
- If the string starts with
"c:", the query is case-sensitive. - If it starts with
"i:", the query is explicitly case-insensitive. - Otherwise, it defaults to case-insensitive.
- If the string starts with
-
Match Variants:
- If, after removing any
c:ori:prefix, the string starts with==> parsed asExact(...). - If it starts with
^=> parsed asStartsWith(...). - If it ends with
$=> parsed asEndsWith(...). - Otherwise, it is parsed as
Contains(...).
- If, after removing any
Whitespace is trimmed, and an empty input results in an error.
§Examples
// Case-insensitive exact match
let query = Query::from_str("=Exact").unwrap();
if let Query::Exact(ref s, false) = query {
assert_eq!(s, "Exact");
} else {
panic!("Expected case-insensitive Exact variant");
}
// Case-sensitive starts-with match
let query = Query::from_str("c:^Hello").unwrap();
if let Query::StartsWith(ref s, true) = query {
assert_eq!(s, "Hello");
} else {
panic!("Expected case-sensitive StartsWith variant");
}impl StructuralPartialEq for Query
Auto Trait Implementations§
impl Freeze for Query
impl RefUnwindSafe for Query
impl Send for Query
impl Sync for Query
impl Unpin for Query
impl UnwindSafe for Query
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more