pub struct EvaluationResult {
pub filename: PathBuf,
pub matches: Vec<MatchResult>,
pub metadata: EvaluationMetadata,
pub error: Option<String>,
}Expand description
Complete evaluation result for a file
Contains all matches found during rule evaluation, along with metadata about the evaluation process and the file being analyzed.
§Examples
use libmagic_rs::output::{EvaluationResult, MatchResult, EvaluationMetadata};
use libmagic_rs::parser::ast::Value;
use std::path::PathBuf;
let result = EvaluationResult {
filename: PathBuf::from("example.bin"),
matches: vec![
MatchResult {
message: "ELF executable".to_string(),
offset: 0,
length: 4,
value: Value::Bytes(vec![0x7f, 0x45, 0x4c, 0x46]),
rule_path: vec!["elf".to_string()],
confidence: 95,
mime_type: Some("application/x-executable".to_string()),
}
],
metadata: EvaluationMetadata {
file_size: 8192,
evaluation_time_ms: 2.5,
rules_evaluated: 42,
rules_matched: 1,
},
error: None,
};
assert_eq!(result.matches.len(), 1);
assert_eq!(result.metadata.file_size, 8192);Fields§
§filename: PathBufPath to the file that was analyzed
matches: Vec<MatchResult>All successful rule matches found during evaluation
Matches are typically ordered by offset, then by confidence score. The first match is often considered the primary file type.
metadata: EvaluationMetadataMetadata about the evaluation process
error: Option<String>Error that occurred during evaluation, if any
When present, indicates that evaluation was incomplete or failed. Partial results may still be available in the matches vector.
Implementations§
Source§impl EvaluationResult
impl EvaluationResult
Sourcepub fn new(
filename: PathBuf,
matches: Vec<MatchResult>,
metadata: EvaluationMetadata,
) -> Self
pub fn new( filename: PathBuf, matches: Vec<MatchResult>, metadata: EvaluationMetadata, ) -> Self
Create a new evaluation result
§Arguments
filename- Path to the analyzed filematches- Vector of successful matchesmetadata- Evaluation metadata
§Examples
use libmagic_rs::output::{EvaluationResult, EvaluationMetadata};
use std::path::PathBuf;
let result = EvaluationResult::new(
PathBuf::from("test.txt"),
vec![],
EvaluationMetadata {
file_size: 1024,
evaluation_time_ms: 1.2,
rules_evaluated: 10,
rules_matched: 0,
}
);
assert_eq!(result.filename, PathBuf::from("test.txt"));
assert!(result.matches.is_empty());
assert!(result.error.is_none());Sourcepub fn from_library_result(result: &EvaluationResult, filename: &Path) -> Self
pub fn from_library_result(result: &EvaluationResult, filename: &Path) -> Self
Convert from a library EvaluationResult to an output EvaluationResult
This adapts the library’s evaluation result into the output format used for JSON and structured output. Converts all matches and metadata, and enriches the first match’s rule path with tags extracted from the overall description.
§Arguments
result- The library evaluation result to convertfilename- Path to the file that was evaluated
Sourcepub fn with_error(
filename: PathBuf,
error: String,
metadata: EvaluationMetadata,
) -> Self
pub fn with_error( filename: PathBuf, error: String, metadata: EvaluationMetadata, ) -> Self
Create an evaluation result with an error
§Arguments
filename- Path to the analyzed fileerror- Error message describing what went wrongmetadata- Evaluation metadata (may be partial)
§Examples
use libmagic_rs::output::{EvaluationResult, EvaluationMetadata};
use std::path::PathBuf;
let result = EvaluationResult::with_error(
PathBuf::from("missing.txt"),
"File not found".to_string(),
EvaluationMetadata {
file_size: 0,
evaluation_time_ms: 0.0,
rules_evaluated: 0,
rules_matched: 0,
}
);
assert_eq!(result.error, Some("File not found".to_string()));
assert!(result.matches.is_empty());Sourcepub fn add_match(&mut self, match_result: MatchResult)
pub fn add_match(&mut self, match_result: MatchResult)
Add a match result to this evaluation
§Examples
use libmagic_rs::output::{EvaluationResult, MatchResult, EvaluationMetadata};
use libmagic_rs::parser::ast::Value;
use std::path::PathBuf;
let mut result = EvaluationResult::new(
PathBuf::from("data.bin"),
vec![],
EvaluationMetadata {
file_size: 512,
evaluation_time_ms: 0.8,
rules_evaluated: 5,
rules_matched: 0,
}
);
let match_result = MatchResult::new(
"Binary data".to_string(),
0,
Value::Bytes(vec![0x00, 0x01, 0x02])
);
result.add_match(match_result);
assert_eq!(result.matches.len(), 1);Sourcepub fn primary_match(&self) -> Option<&MatchResult>
pub fn primary_match(&self) -> Option<&MatchResult>
Get the primary match (first match with highest confidence)
Returns the match that is most likely to represent the primary file type. This is typically the first match, but if multiple matches exist, the one with the highest confidence score is preferred.
§Examples
use libmagic_rs::output::{EvaluationResult, MatchResult, EvaluationMetadata};
use libmagic_rs::parser::ast::Value;
use std::path::PathBuf;
let mut result = EvaluationResult::new(
PathBuf::from("test.exe"),
vec![
MatchResult::with_metadata(
"Executable".to_string(),
0, 2,
Value::String("MZ".to_string()),
vec!["pe".to_string()],
60,
None
),
MatchResult::with_metadata(
"PE32 executable".to_string(),
60, 4,
Value::String("PE\0\0".to_string()),
vec!["pe".to_string(), "pe32".to_string()],
90,
Some("application/x-msdownload".to_string())
),
],
EvaluationMetadata {
file_size: 4096,
evaluation_time_ms: 1.5,
rules_evaluated: 15,
rules_matched: 2,
}
);
let primary = result.primary_match();
assert!(primary.is_some());
assert_eq!(primary.unwrap().confidence, 90);Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Check if the evaluation was successful (no errors)
§Examples
use libmagic_rs::output::{EvaluationResult, EvaluationMetadata};
use std::path::PathBuf;
let success = EvaluationResult::new(
PathBuf::from("good.txt"),
vec![],
EvaluationMetadata {
file_size: 100,
evaluation_time_ms: 0.5,
rules_evaluated: 3,
rules_matched: 0,
}
);
let failure = EvaluationResult::with_error(
PathBuf::from("bad.txt"),
"Parse error".to_string(),
EvaluationMetadata {
file_size: 0,
evaluation_time_ms: 0.0,
rules_evaluated: 0,
rules_matched: 0,
}
);
assert!(success.is_success());
assert!(!failure.is_success());Trait Implementations§
Source§impl Clone for EvaluationResult
impl Clone for EvaluationResult
Source§fn clone(&self) -> EvaluationResult
fn clone(&self) -> EvaluationResult
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more