Skip to main content

JsonMatchResult

Struct JsonMatchResult 

Source
pub struct JsonMatchResult {
    pub text: String,
    pub offset: usize,
    pub value: String,
    pub tags: Vec<String>,
    pub score: u8,
}
Expand description

JSON representation of a magic rule match result

This structure follows the original libmagic JSON specification format, providing a standardized way to represent file type detection results in JSON format for programmatic consumption.

§Fields

  • text - Human-readable description of the file type or pattern match
  • offset - Byte offset in the file where the match occurred
  • value - Hexadecimal representation of the matched bytes
  • tags - Array of classification tags derived from the rule hierarchy
  • score - Confidence score for this match (0-100)

§Examples

use libmagic_rs::output::json::JsonMatchResult;

let json_result = JsonMatchResult {
    text: "ELF 64-bit LSB executable".to_string(),
    offset: 0,
    value: "7f454c46".to_string(),
    tags: vec!["executable".to_string(), "elf".to_string()],
    score: 90,
};

assert_eq!(json_result.text, "ELF 64-bit LSB executable");
assert_eq!(json_result.offset, 0);
assert_eq!(json_result.value, "7f454c46");
assert_eq!(json_result.tags.len(), 2);
assert_eq!(json_result.score, 90);

Fields§

§text: String

Human-readable description of the file type or pattern match

This field contains the same descriptive text that would appear in the traditional text output format, providing context about what type of file or pattern was detected.

§offset: usize

Byte offset in the file where the match occurred

Indicates the exact position in the file where the magic rule found the matching pattern. This is useful for understanding the structure of the file and for debugging rule evaluation.

§value: String

Hexadecimal representation of the matched bytes

Contains the actual byte values that were matched, encoded as a hexadecimal string without separators. For string matches, this represents the UTF-8 bytes of the matched text.

§tags: Vec<String>

Array of classification tags derived from the rule hierarchy

These tags are extracted from the rule path and provide machine-readable classification information about the detected file type. Tags are typically ordered from general to specific.

§score: u8

Confidence score for this match (0-100)

Indicates how confident the detection algorithm is about this particular match. Higher scores indicate more specific or reliable patterns, while lower scores may indicate generic or ambiguous matches.

Implementations§

Source§

impl JsonMatchResult

Source

pub fn from_match_result(match_result: &MatchResult) -> Self

Create a new JSON match result from a MatchResult

Converts the internal MatchResult representation to the JSON format specified in the original libmagic specification, including proper formatting of the value field and extraction of tags from the rule path.

§Arguments
  • match_result - The internal match result to convert
§Examples
use libmagic_rs::output::{MatchResult, json::JsonMatchResult};
use libmagic_rs::parser::ast::Value;

let match_result = MatchResult::with_metadata(
    "PNG image".to_string(),
    0,
    8,
    Value::Bytes(vec![0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
    vec!["image".to_string(), "png".to_string()],
    85,
    Some("image/png".to_string())
);

let json_result = JsonMatchResult::from_match_result(&match_result);

assert_eq!(json_result.text, "PNG image");
assert_eq!(json_result.offset, 0);
assert_eq!(json_result.value, "89504e470d0a1a0a");
assert_eq!(json_result.tags, vec!["image", "png"]);
assert_eq!(json_result.score, 85);
Source

pub fn new( text: String, offset: usize, value: String, tags: Vec<String>, score: u8, ) -> Self

Create a new JSON match result with explicit values

§Arguments
  • text - Human-readable description
  • offset - Byte offset where match occurred
  • value - Hexadecimal string representation of matched bytes
  • tags - Classification tags
  • score - Confidence score (0-100)
§Examples
use libmagic_rs::output::json::JsonMatchResult;

let json_result = JsonMatchResult::new(
    "JPEG image".to_string(),
    0,
    "ffd8".to_string(),
    vec!["image".to_string(), "jpeg".to_string()],
    80
);

assert_eq!(json_result.text, "JPEG image");
assert_eq!(json_result.value, "ffd8");
assert_eq!(json_result.score, 80);
Source

pub fn add_tag(&mut self, tag: String)

Add a tag to the tags array

§Examples
use libmagic_rs::output::json::JsonMatchResult;

let mut json_result = JsonMatchResult::new(
    "Archive".to_string(),
    0,
    "504b0304".to_string(),
    vec!["archive".to_string()],
    75
);

json_result.add_tag("zip".to_string());
assert_eq!(json_result.tags, vec!["archive", "zip"]);
Source

pub fn set_score(&mut self, score: u8)

Set the confidence score, clamping to valid range

§Examples
use libmagic_rs::output::json::JsonMatchResult;

let mut json_result = JsonMatchResult::new(
    "Text".to_string(),
    0,
    "48656c6c6f".to_string(),
    vec![],
    50
);

json_result.set_score(95);
assert_eq!(json_result.score, 95);

// Values over 100 are clamped
json_result.set_score(150);
assert_eq!(json_result.score, 100);

Trait Implementations§

Source§

impl Clone for JsonMatchResult

Source§

fn clone(&self) -> JsonMatchResult

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 JsonMatchResult

Source§

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

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

impl<'de> Deserialize<'de> for JsonMatchResult

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 JsonMatchResult

Source§

fn eq(&self, other: &JsonMatchResult) -> 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 JsonMatchResult

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 JsonMatchResult

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