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 matchoffset- Byte offset in the file where the match occurredvalue- Hexadecimal representation of the matched bytestags- Array of classification tags derived from the rule hierarchyscore- 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: StringHuman-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: usizeByte 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: StringHexadecimal 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.
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: u8Confidence 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
impl JsonMatchResult
Sourcepub fn from_match_result(match_result: &MatchResult) -> Self
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);Sourcepub fn new(
text: String,
offset: usize,
value: String,
tags: Vec<String>,
score: u8,
) -> Self
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 descriptionoffset- Byte offset where match occurredvalue- Hexadecimal string representation of matched bytestags- Classification tagsscore- 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);Sourcepub fn add_tag(&mut self, tag: String)
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"]);Sourcepub fn set_score(&mut self, score: u8)
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
impl Clone for JsonMatchResult
Source§fn clone(&self) -> JsonMatchResult
fn clone(&self) -> JsonMatchResult
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more