pub struct MatchResult {
pub message: String,
pub offset: usize,
pub length: usize,
pub value: Value,
pub rule_path: Vec<String>,
pub confidence: u8,
pub mime_type: Option<String>,
}Expand description
Result of a single magic rule match
Contains all information about a successful rule match, including the matched value, its location in the file, and metadata about the rule that matched.
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let result = MatchResult {
message: "ELF 64-bit LSB executable".to_string(),
offset: 0,
length: 4,
value: Value::Bytes(vec![0x7f, 0x45, 0x4c, 0x46]),
rule_path: vec!["elf".to_string(), "elf64".to_string()],
confidence: 90,
mime_type: Some("application/x-executable".to_string()),
};
assert_eq!(result.message, "ELF 64-bit LSB executable");
assert_eq!(result.offset, 0);Fields§
§message: StringHuman-readable description of the file type or pattern match
offset: usizeByte offset in the file where the match occurred
length: usizeNumber of bytes that were examined for this match
value: ValueThe actual value that was matched at the specified offset
rule_path: Vec<String>Hierarchical path of rule names that led to this match
For nested rules, this contains the sequence of rule identifiers from the root rule down to the specific rule that matched.
confidence: u8Confidence score for this match (0-100)
Higher values indicate more specific or reliable matches. Generic patterns typically have lower confidence scores.
mime_type: Option<String>Optional MIME type associated with this match
When available, provides the standard MIME type corresponding to the detected file format.
Implementations§
Source§impl MatchResult
impl MatchResult
Sourcepub fn new(message: String, offset: usize, value: Value) -> Self
pub fn new(message: String, offset: usize, value: Value) -> Self
Create a new match result with basic information
§Arguments
message- Human-readable description of the matchoffset- Byte offset where the match occurredvalue- The matched value
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let result = MatchResult::new(
"PNG image".to_string(),
0,
Value::Bytes(vec![0x89, 0x50, 0x4e, 0x47])
);
assert_eq!(result.message, "PNG image");
assert_eq!(result.offset, 0);
assert_eq!(result.confidence, 50); // Default confidenceSourcepub fn with_metadata(
message: String,
offset: usize,
length: usize,
value: Value,
rule_path: Vec<String>,
confidence: u8,
mime_type: Option<String>,
) -> Self
pub fn with_metadata( message: String, offset: usize, length: usize, value: Value, rule_path: Vec<String>, confidence: u8, mime_type: Option<String>, ) -> Self
Create a new match result with full metadata
§Arguments
message- Human-readable description of the matchoffset- Byte offset where the match occurredlength- Number of bytes examinedvalue- The matched valuerule_path- Hierarchical path of rules that led to this matchconfidence- Confidence score (0-100)mime_type- Optional MIME type
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let result = MatchResult::with_metadata(
"JPEG image".to_string(),
0,
2,
Value::Bytes(vec![0xff, 0xd8]),
vec!["image".to_string(), "jpeg".to_string()],
85,
Some("image/jpeg".to_string())
);
assert_eq!(result.rule_path.len(), 2);
assert_eq!(result.confidence, 85);
assert_eq!(result.mime_type, Some("image/jpeg".to_string()));Sourcepub fn from_evaluator_match(m: &RuleMatch, mime_type: Option<&str>) -> Self
pub fn from_evaluator_match(m: &RuleMatch, mime_type: Option<&str>) -> Self
Convert from an evaluator RuleMatch to an output MatchResult
This adapts the internal evaluation result format to the richer output format used for JSON and structured output. It extracts rule paths from match messages and converts confidence from 0.0-1.0 to 0-100 scale.
§Arguments
m- The evaluator rule match to convertmime_type- Optional MIME type to associate with this match
Sourcepub fn set_confidence(&mut self, confidence: u8)
pub fn set_confidence(&mut self, confidence: u8)
Set the confidence score for this match
The confidence score is automatically clamped to the range 0-100.
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let mut result = MatchResult::new(
"Text file".to_string(),
0,
Value::String("Hello".to_string())
);
result.set_confidence(75);
assert_eq!(result.confidence, 75);
// Values over 100 are clamped
result.set_confidence(150);
assert_eq!(result.confidence, 100);Sourcepub fn add_rule_path(&mut self, rule_name: String)
pub fn add_rule_path(&mut self, rule_name: String)
Add a rule name to the rule path
This is typically used during evaluation to build up the hierarchical path of rules that led to a match.
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let mut result = MatchResult::new(
"Archive".to_string(),
0,
Value::String("PK".to_string())
);
result.add_rule_path("archive".to_string());
result.add_rule_path("zip".to_string());
assert_eq!(result.rule_path, vec!["archive", "zip"]);Sourcepub fn set_mime_type(&mut self, mime_type: Option<String>)
pub fn set_mime_type(&mut self, mime_type: Option<String>)
Set the MIME type for this match
§Examples
use libmagic_rs::output::MatchResult;
use libmagic_rs::parser::ast::Value;
let mut result = MatchResult::new(
"PDF document".to_string(),
0,
Value::String("%PDF".to_string())
);
result.set_mime_type(Some("application/pdf".to_string()));
assert_eq!(result.mime_type, Some("application/pdf".to_string()));Trait Implementations§
Source§impl Clone for MatchResult
impl Clone for MatchResult
Source§fn clone(&self) -> MatchResult
fn clone(&self) -> MatchResult
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more