#[non_exhaustive]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.
§Forward compatibility
This struct is marked #[non_exhaustive]: new configuration fields may
be added in any release without it being a breaking change. Construct
instances via one of the factory constructors
(EvaluationConfig::default(), EvaluationConfig::new(),
EvaluationConfig::performance(),
EvaluationConfig::comprehensive()) and then chain with_*
builder-style setters:
use libmagic_rs::EvaluationConfig;
let custom_config = EvaluationConfig::default()
.with_max_recursion_depth(10)
.with_timeout_ms(Some(5_000));Direct struct-literal construction (EvaluationConfig { .. }) is
rejected by the compiler from outside this crate because of
#[non_exhaustive].
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.max_recursion_depth: u32Maximum 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: usizeMaximum 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: boolStop 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.
§Semantics
“First match” refers to the first top-level rule that matches.
Children of the first matching top-level rule are always evaluated
before the stop check; the stop check applies to subsequent
top-level rules. In other words, stop_at_first_match = true does
not truncate the child subtree of the matching rule – it only
prevents later sibling top-level rules from being evaluated. A
successful top-level match therefore returns one parent RuleMatch
plus any descendant RuleMatch values its children produced.
enable_mime_types: boolEnable 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
impl EvaluationConfig
Sourcepub fn new() -> Self
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);Sourcepub const fn performance() -> Self
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));Sourcepub const fn comprehensive() -> Self
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));Sourcepub const fn with_max_recursion_depth(self, depth: u32) -> Self
pub const fn with_max_recursion_depth(self, depth: u32) -> Self
Sets the maximum recursion depth for nested rule evaluation.
Builder-style setter for consumers outside this crate. Direct
struct-literal construction is blocked by #[non_exhaustive], so
chain with_* calls after one of the factory constructors
(default, performance, comprehensive, new).
Sourcepub const fn with_max_string_length(self, length: usize) -> Self
pub const fn with_max_string_length(self, length: usize) -> Self
Sets the maximum string length (in bytes) read for string types.
Sourcepub const fn with_stop_at_first_match(self, stop: bool) -> Self
pub const fn with_stop_at_first_match(self, stop: bool) -> Self
Sets whether evaluation stops after the first top-level match.
Sourcepub const fn with_mime_types(self, enable: bool) -> Self
pub const fn with_mime_types(self, enable: bool) -> Self
Enables or disables MIME type mapping in results.
Sourcepub const fn with_timeout_ms(self, timeout_ms: Option<u64>) -> Self
pub const fn with_timeout_ms(self, timeout_ms: Option<u64>) -> Self
Sets the evaluation timeout in milliseconds. Pass None for
unbounded evaluation (not recommended on untrusted input).
Sourcepub fn validate(&self) -> Result<()>
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::ConfigError 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::default().with_max_recursion_depth(0);
assert!(invalid_config.validate().is_err());Trait Implementations§
Source§impl Clone for EvaluationConfig
impl Clone for EvaluationConfig
Source§fn clone(&self) -> EvaluationConfig
fn clone(&self) -> EvaluationConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for EvaluationConfig
impl Debug for EvaluationConfig
Source§impl Default for EvaluationConfig
impl Default for EvaluationConfig
Source§fn default() -> Self
fn default() -> Self
Returns the default evaluation configuration.
§Security
The default configuration has no timeout. When processing untrusted
input, use EvaluationConfig::performance() or set timeout_ms
explicitly to prevent denial of service.