#[non_exhaustive]pub struct RuleMatch {
pub message: String,
pub offset: usize,
pub level: u32,
pub value: Value,
pub type_kind: TypeKind,
pub confidence: f64,
}Expand description
Result of evaluating a magic rule
Contains information extracted from a successful rule match, including the matched value, position, and confidence score.
This type derives Serialize so callers can convert evaluation results
to JSON, but intentionally does NOT derive Deserialize: a
reconstructed RuleMatch would lack the buffer context it was
produced against, so deserialization is not a meaningful operation.
The output-side conversion layer (output::MatchResult /
output::json::JsonMatchResult) is the documented JSON contract.
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.message: StringThe message associated with the matching rule
offset: usizeThe offset where the match occurred
level: u32The rule level (depth in hierarchy)
value: ValueThe matched value
type_kind: TypeKindThe type used to read the matched value
Carries the source TypeKind so downstream consumers (e.g., output
formatting) can determine the on-disk width of the matched value.
#[serde(skip)] keeps the parser AST out of JSON output produced
by serializing EvaluationResult directly via
serde_json::to_string(&result). The documented JSON contract is
JsonMatchResult in src/output/json.rs, which omits this field.
Origin findings 1B-H2 / 2A-M1 (CWE-200 information exposure).
Rust-side consumers continue to access type_kind via field access
for runtime needs (format_magic_message width-masking,
bit_width() derivation).
confidence: f64Confidence score (0.0 to 1.0)
Calculated based on match depth in the rule hierarchy. Deeper matches indicate more specific file type identification and thus higher confidence.
Implementations§
Source§impl RuleMatch
impl RuleMatch
Sourcepub fn new(
message: String,
offset: usize,
level: u32,
value: Value,
type_kind: TypeKind,
confidence: f64,
) -> Self
pub fn new( message: String, offset: usize, level: u32, value: Value, type_kind: TypeKind, confidence: f64, ) -> Self
Construct a new RuleMatch.
confidence is typically derived from level via
RuleMatch::calculate_confidence; pass it explicitly here so
callers can supply an alternative score when needed (e.g. when
post-processing a series of matches).
Sourcepub fn calculate_confidence(level: u32) -> f64
pub fn calculate_confidence(level: u32) -> f64
Calculate confidence score based on rule depth
Formula: min(1.0, 0.3 + (level * 0.2))
- Level 0 (root): 0.3
- Level 1: 0.5
- Level 2: 0.7
- Level 3: 0.9
- Level 4+: 1.0 (capped)
§Examples
use libmagic_rs::evaluator::RuleMatch;
assert!((RuleMatch::calculate_confidence(0) - 0.3).abs() < 0.001);
assert!((RuleMatch::calculate_confidence(3) - 0.9).abs() < 0.001);
assert!((RuleMatch::calculate_confidence(10) - 1.0).abs() < 0.001);