Skip to main content

EvaluationConfig

Struct EvaluationConfig 

Source
pub struct EvaluationConfig {
    pub max_recursion_depth: u32,
    pub max_string_length: usize,
    pub stop_at_first_match: bool,
    pub enable_mime_types: bool,
    pub timeout_ms: Option<u64>,
}
Expand description

Configuration for rule evaluation

This struct controls various aspects of magic rule evaluation behavior, including performance limits, output options, and matching strategies.

§Examples

use libmagic_rs::EvaluationConfig;

// Use default configuration
let config = EvaluationConfig::default();

// Create custom configuration
let custom_config = EvaluationConfig {
    max_recursion_depth: 10,
    max_string_length: 4096,
    stop_at_first_match: false, // Get all matches
    enable_mime_types: true,
    timeout_ms: Some(5000), // 5 second timeout
};

Fields§

§max_recursion_depth: u32

Maximum recursion depth for nested rules

This prevents infinite recursion in malformed magic files and limits the depth of rule hierarchy traversal. Default is 20.

§max_string_length: usize

Maximum string length to read

This limits the amount of data read for string types to prevent excessive memory usage. Default is 8192 bytes.

§stop_at_first_match: bool

Stop at first match or continue for all matches

When true, evaluation stops after the first matching rule. When false, all rules are evaluated to find all matches. Default is true for performance.

§enable_mime_types: bool

Enable MIME type mapping in results

When true, the evaluator will attempt to map file type descriptions to standard MIME types. Default is false.

§timeout_ms: Option<u64>

Timeout for evaluation in milliseconds

If set, evaluation will be aborted if it takes longer than this duration. None means no timeout. Default is None.

Implementations§

Source§

impl EvaluationConfig

Source

pub fn new() -> Self

Create a new configuration with default values

§Examples
use libmagic_rs::EvaluationConfig;

let config = EvaluationConfig::new();
assert_eq!(config.max_recursion_depth, 20);
assert_eq!(config.max_string_length, 8192);
assert!(config.stop_at_first_match);
assert!(!config.enable_mime_types);
assert_eq!(config.timeout_ms, None);
Source

pub const fn performance() -> Self

Create a configuration optimized for performance

This configuration prioritizes speed over completeness:

  • Lower recursion depth limit
  • Smaller string length limit
  • Stop at first match
  • No MIME type mapping
  • Short timeout
§Examples
use libmagic_rs::EvaluationConfig;

let config = EvaluationConfig::performance();
assert_eq!(config.max_recursion_depth, 10);
assert_eq!(config.max_string_length, 1024);
assert!(config.stop_at_first_match);
assert!(!config.enable_mime_types);
assert_eq!(config.timeout_ms, Some(1000));
Source

pub const fn comprehensive() -> Self

Create a configuration optimized for completeness

This configuration prioritizes finding all matches over speed:

  • Higher recursion depth limit
  • Larger string length limit
  • Find all matches
  • Enable MIME type mapping
  • Longer timeout
§Examples
use libmagic_rs::EvaluationConfig;

let config = EvaluationConfig::comprehensive();
assert_eq!(config.max_recursion_depth, 50);
assert_eq!(config.max_string_length, 32768);
assert!(!config.stop_at_first_match);
assert!(config.enable_mime_types);
assert_eq!(config.timeout_ms, Some(30000));
Source

pub fn validate(&self) -> Result<()>

Validate the configuration settings

Performs comprehensive security validation of all configuration values to prevent malicious configurations that could lead to resource exhaustion, denial of service, or other security issues.

§Security

This validation prevents:

  • Stack overflow attacks through excessive recursion depth
  • Memory exhaustion through oversized string limits
  • Denial of service through excessive timeouts
  • Integer overflow in configuration calculations
§Errors

Returns LibmagicError::InvalidFormat if any configuration values are invalid or out of reasonable bounds.

§Examples
use libmagic_rs::EvaluationConfig;

let config = EvaluationConfig::default();
assert!(config.validate().is_ok());

let invalid_config = EvaluationConfig {
    max_recursion_depth: 0, // Invalid: must be > 0
    ..Default::default()
};
assert!(invalid_config.validate().is_err());

Trait Implementations§

Source§

impl Clone for EvaluationConfig

Source§

fn clone(&self) -> EvaluationConfig

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 EvaluationConfig

Source§

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

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

impl Default for EvaluationConfig

Source§

fn default() -> Self

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

impl PartialEq for EvaluationConfig

Source§

fn eq(&self, other: &EvaluationConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Eq for EvaluationConfig

Source§

impl StructuralPartialEq for EvaluationConfig

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.