Skip to main content

EvaluationResult

Struct EvaluationResult 

Source
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: PathBuf

Path 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: EvaluationMetadata

Metadata 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

Source

pub fn new( filename: PathBuf, matches: Vec<MatchResult>, metadata: EvaluationMetadata, ) -> Self

Create a new evaluation result

§Arguments
  • filename - Path to the analyzed file
  • matches - Vector of successful matches
  • metadata - 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());
Source

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 convert
  • filename - Path to the file that was evaluated
Source

pub fn with_error( filename: PathBuf, error: String, metadata: EvaluationMetadata, ) -> Self

Create an evaluation result with an error

§Arguments
  • filename - Path to the analyzed file
  • error - Error message describing what went wrong
  • metadata - 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());
Source

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);
Source

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);
Source

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

Source§

fn clone(&self) -> EvaluationResult

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 EvaluationResult

Source§

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

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

impl<'de> Deserialize<'de> for EvaluationResult

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for EvaluationResult

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,