Skip to main content

SearchFlags

Struct SearchFlags 

Source
#[non_exhaustive]
pub struct SearchFlags { pub compact_whitespace: bool, pub compact_optional_whitespace: bool, pub ignore_lowercase: bool, pub ignore_uppercase: bool, pub text_test: bool, pub trim: bool, pub bin_test: bool, pub full_word: bool, pub start_anchor: bool, }
Expand description

Search modifier flags parsed from the /[sCcWwTtBbf] suffix on a search rule.

Mirrors StringFlags for the eight STRING_* letters that alter the literal-pattern comparison (/c, /C, /w, /W, /t, /T, /b, /f), plus a search-only start_anchor field for /s which shifts the GNU file previous-match anchor to the START of the matched region. The default (all false) preserves byte-exact comparison and match-END anchor advance.

SearchFlags is structurally parallel to StringFlags: when one struct grows a field, the other gains the same field in lockstep so that SearchFlags::to_string_flags can keep handing off to compare_string_with_flags without a generic refactor. The search-only start_anchor field has no analog in string rules.

/c vs /C are asymmetric in the same way as StringFlags: the pattern character controls fold direction. See StringFlags and GOTCHAS S6.5 for the rationale.

§Examples

use libmagic_rs::parser::ast::SearchFlags;

let plain = SearchFlags::default();
assert!(!plain.start_anchor);
assert!(plain.is_empty());
assert!(!plain.needs_byte_compare());

let start = SearchFlags::default().with_start_anchor(true);
assert!(start.start_anchor);
assert!(!start.is_empty());
// /s is anchor-only -- does not force the byte-compare slow path.
assert!(!start.needs_byte_compare());

let case = SearchFlags::default().with_ignore_lowercase(true);
assert!(case.needs_byte_compare());

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§compact_whitespace: bool

/WSTRING_COMPACT_WHITESPACE. Pattern whitespace requires at least one whitespace byte in the file, then any further whitespace in the file is consumed greedily.

§compact_optional_whitespace: bool

/wSTRING_COMPACT_OPTIONAL_WHITESPACE. Pattern whitespace matches zero or more whitespace bytes in the file.

§ignore_lowercase: bool

/cSTRING_IGNORE_LOWERCASE. When the pattern char is lowercase, the file byte is to_ascii_lowercase’d before comparison. Uppercase pattern chars are compared literally.

§ignore_uppercase: bool

/CSTRING_IGNORE_UPPERCASE. When the pattern char is uppercase, the file byte is to_ascii_uppercase’d before comparison. Lowercase pattern chars are compared literally.

§text_test: bool

/tSTRING_TEXTTEST. Hint that this rule applies to text files. Captured for MIME-output integration; does not currently alter comparison.

§trim: bool

/TSTRING_TRIM. Trim leading and trailing ASCII whitespace from the pattern before comparison.

§bin_test: bool

/bSTRING_BINTEST. Hint that this rule applies to binary files. Captured for MIME-output integration; does not currently alter comparison.

§full_word: bool

/fSTRING_FULL_WORD. Post-match check that the byte after the matched region is either end-of-buffer or a non-word character (ASCII alphanumeric or _).

§start_anchor: bool

/s – magic(5) “search-start” flag. When true, the GNU file previous-match anchor advance lands on the match-START index rather than match-END (the default). Mirrors libmagic’s FILE_SEARCH anchor handling in src/softmagic.c::moffset. The dispatch happens in src/evaluator/types/search.rs::search_bytes_consumed.

Implementations§

Source§

impl SearchFlags

Source

pub const fn is_empty(self) -> bool

Returns true when every flag is false (default-constructed).

Source

pub const fn needs_byte_compare(self) -> bool

Returns true when any flag alters the literal-pattern comparison, forcing the byte-walk slow path through compare_string_with_flags. The anchor-only / metadata-only flags (/s, /t, /b) do not trigger byte-compare; they preserve the memchr::memmem::find fast path.

Source

pub const fn to_string_flags(self) -> StringFlags

Project the eight shared flag fields onto a StringFlags for handoff to compare_string_with_flags. The search-only start_anchor field is dropped (it is anchor-advance policy, not comparison policy).

§Examples
use libmagic_rs::parser::ast::SearchFlags;

let sf = SearchFlags::default()
    .with_ignore_lowercase(true)
    .with_trim(true)
    .with_start_anchor(true);
let projected = sf.to_string_flags();
assert!(projected.ignore_lowercase);
assert!(projected.trim);
// /s has no analog in StringFlags.
Source

pub const fn with_compact_whitespace(self, value: bool) -> Self

Builder-style setter for compact_whitespace (/W).

Source

pub const fn with_compact_optional_whitespace(self, value: bool) -> Self

Builder-style setter for compact_optional_whitespace (/w).

Source

pub const fn with_ignore_lowercase(self, value: bool) -> Self

Builder-style setter for ignore_lowercase (/c).

Source

pub const fn with_ignore_uppercase(self, value: bool) -> Self

Builder-style setter for ignore_uppercase (/C).

Source

pub const fn with_text_test(self, value: bool) -> Self

Builder-style setter for text_test (/t).

Source

pub const fn with_trim(self, value: bool) -> Self

Builder-style setter for trim (/T).

Source

pub const fn with_bin_test(self, value: bool) -> Self

Builder-style setter for bin_test (/b).

Source

pub const fn with_full_word(self, value: bool) -> Self

Builder-style setter for full_word (/f).

Source

pub const fn with_start_anchor(self, value: bool) -> Self

Builder-style setter for start_anchor (/s).

Trait Implementations§

Source§

impl Clone for SearchFlags

Source§

fn clone(&self) -> SearchFlags

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 SearchFlags

Source§

impl Debug for SearchFlags

Source§

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

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

impl Default for SearchFlags

Source§

fn default() -> SearchFlags

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for SearchFlags

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 SearchFlags

Source§

impl PartialEq for SearchFlags

Source§

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

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 SearchFlags

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