Skip to main content

MatchResult

Struct MatchResult 

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

Human-readable description of the file type or pattern match

§offset: usize

Byte offset in the file where the match occurred

§length: usize

Number of bytes that were examined for this match

§value: Value

The 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: u8

Confidence 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

Source

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 match
  • offset - Byte offset where the match occurred
  • value - 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 confidence
Source

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 match
  • offset - Byte offset where the match occurred
  • length - Number of bytes examined
  • value - The matched value
  • rule_path - Hierarchical path of rules that led to this match
  • confidence - 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()));
Source

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 convert
  • mime_type - Optional MIME type to associate with this match
Source

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

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

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

Source§

fn clone(&self) -> MatchResult

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 MatchResult

Source§

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

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

impl<'de> Deserialize<'de> for MatchResult

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 PartialEq for MatchResult

Source§

fn eq(&self, other: &MatchResult) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for MatchResult

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

impl StructuralPartialEq for MatchResult

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>,