#[non_exhaustive]pub enum TypeKind {
Show 14 variants
Byte {
signed: bool,
},
Short {
endian: Endianness,
signed: bool,
},
Long {
endian: Endianness,
signed: bool,
},
Quad {
endian: Endianness,
signed: bool,
},
Float {
endian: Endianness,
},
Double {
endian: Endianness,
},
Date {
endian: Endianness,
utc: bool,
},
QDate {
endian: Endianness,
utc: bool,
},
String {
max_length: Option<usize>,
},
String16 {
endian: Endianness,
},
PString {
max_length: Option<usize>,
length_width: PStringLengthWidth,
length_includes_itself: bool,
},
Regex {
flags: RegexFlags,
count: RegexCount,
},
Search {
range: NonZeroUsize,
},
Meta(MetaType),
}Expand description
Data type specifications for interpreting bytes
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Byte
Single byte
§Examples
use libmagic_rs::parser::ast::TypeKind;
let byte = TypeKind::Byte { signed: true };
assert_eq!(byte, TypeKind::Byte { signed: true });Short
16-bit integer
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let short = TypeKind::Short { endian: Endianness::Little, signed: true };
assert_eq!(short, TypeKind::Short { endian: Endianness::Little, signed: true });Long
32-bit integer
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let long = TypeKind::Long { endian: Endianness::Big, signed: false };
assert_eq!(long, TypeKind::Long { endian: Endianness::Big, signed: false });Quad
64-bit integer
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let quad = TypeKind::Quad { endian: Endianness::Big, signed: true };
assert_eq!(quad, TypeKind::Quad { endian: Endianness::Big, signed: true });Float
32-bit IEEE 754 floating-point
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let float = TypeKind::Float { endian: Endianness::Big };
assert_eq!(float, TypeKind::Float { endian: Endianness::Big });Fields
endian: EndiannessByte order
Double
64-bit IEEE 754 double-precision floating-point
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let double = TypeKind::Double { endian: Endianness::Big };
assert_eq!(double, TypeKind::Double { endian: Endianness::Big });Fields
endian: EndiannessByte order
Date
32-bit Unix timestamp (seconds since epoch)
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let date = TypeKind::Date { endian: Endianness::Big, utc: true };
assert_eq!(date, TypeKind::Date { endian: Endianness::Big, utc: true });QDate
64-bit Unix timestamp (seconds since epoch)
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let qdate = TypeKind::QDate { endian: Endianness::Little, utc: false };
assert_eq!(qdate, TypeKind::QDate { endian: Endianness::Little, utc: false });String
String data
§Examples
use libmagic_rs::parser::ast::TypeKind;
let s = TypeKind::String { max_length: None };
assert_eq!(s, TypeKind::String { max_length: None });
let capped = TypeKind::String { max_length: Some(32) };
assert_eq!(capped, TypeKind::String { max_length: Some(32) });String16
UCS-2 (16-bit Unicode) string with explicit byte order.
Backs the magic(5) lestring16 (little-endian) and bestring16
(big-endian) keywords. Each character occupies two bytes in the
file; the reader stops at a U+0000 terminator (encoded as the
2-byte sequence 0x00 0x00) or at the end of the buffer. The
decoded value is returned as a Rust String (so non-ASCII
characters are preserved when valid UCS-2).
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
let le = TypeKind::String16 { endian: Endianness::Little };
assert_eq!(le, TypeKind::String16 { endian: Endianness::Little });
let be = TypeKind::String16 { endian: Endianness::Big };
assert_eq!(be, TypeKind::String16 { endian: Endianness::Big });Fields
endian: EndiannessEndianness for the 16-bit code units.
PString
Pascal string (length-prefixed, supports 1/2/4-byte prefix, with optional max length)
Pascal strings store the length as a prefix (1, 2, or 4 bytes, with configurable endianness), followed by that many bytes of string data. Unlike C strings, they are not null-terminated.
§Examples
use libmagic_rs::parser::ast::{TypeKind, PStringLengthWidth};
let pstring = TypeKind::PString { max_length: None, length_width: PStringLengthWidth::OneByte, length_includes_itself: false };
assert_eq!(pstring, TypeKind::PString { max_length: None, length_width: PStringLengthWidth::OneByte, length_includes_itself: false });
let limited = TypeKind::PString { max_length: Some(64), length_width: PStringLengthWidth::TwoByteBE, length_includes_itself: false };
assert_eq!(limited, TypeKind::PString { max_length: Some(64), length_width: PStringLengthWidth::TwoByteBE, length_includes_itself: false });
// /J flag: stored length includes the length field itself
let jpeg = TypeKind::PString { max_length: None, length_width: PStringLengthWidth::TwoByteBE, length_includes_itself: true };
assert_eq!(jpeg, TypeKind::PString { max_length: None, length_width: PStringLengthWidth::TwoByteBE, length_includes_itself: true });Fields
length_width: PStringLengthWidthWidth of the length prefix
Regex
Regular expression matching against file contents
Regex rules match a POSIX-extended regular expression pattern against the
file buffer. Patterns are compiled with multi-line mode always enabled
(matching libmagic’s unconditional REG_NEWLINE), so ^ and $ match
at line boundaries and . does not match \n. The flags control
case sensitivity and anchor advance semantics; the count field
controls the scan window (byte or line bounds). The scan window is
always capped at 8192 bytes (matching GNU file’s FILE_REGEX_MAX;
enforced in the evaluator).
§Examples
use libmagic_rs::parser::ast::{RegexCount, RegexFlags, TypeKind};
use std::num::NonZeroU32;
// Plain `regex` -- no flags, default 8192-byte scan window.
let plain = TypeKind::Regex {
flags: RegexFlags::default(),
count: RegexCount::Default,
};
// `regex/1l` -- scan the first line only.
let first_line = TypeKind::Regex {
flags: RegexFlags::default(),
count: RegexCount::Lines(NonZeroU32::new(1)),
};
// `regex/cs` -- case-insensitive, anchor advances to match-start.
let case_insensitive_start = TypeKind::Regex {
flags: RegexFlags {
case_insensitive: true,
start_offset: true,
},
count: RegexCount::Default,
};Fields
flags: RegexFlagsModifier flags from the /[cs] suffix (/c case-insensitive,
/s start-offset anchor). Line-mode is encoded by the
RegexCount::Lines variant of count, not a flag.
count: RegexCountScan window specifier: default 8192 bytes, explicit byte
count, or explicit line count. See RegexCount for the
three cases.
Search
Multi-byte pattern search within a bounded range
Search rules look for a literal byte pattern within range bytes of
the offset. Unlike TypeKind::String, which only matches at the
exact offset, search scans forward up to range bytes for the
first occurrence. The range is mandatory per GNU file’s
magic(5) specification and is stored as a NonZeroUsize so a
zero-range search is unrepresentable.
§Examples
use libmagic_rs::parser::ast::TypeKind;
use std::num::NonZeroUsize;
// `search/256` -- scan up to 256 bytes for the literal pattern.
let bounded = TypeKind::Search {
range: NonZeroUsize::new(256).unwrap(),
};Fields
range: NonZeroUsizeScan window width in bytes, starting at the rule’s offset.
Meta(MetaType)
Control-flow directive (default, clear, name, use,
indirect, offset).
These magic(5) keywords do not read or compare bytes; they modify
how a rule set is traversed. All six variants are fully evaluated:
default fires as a fallback when no sibling at the same level
has matched; clear resets that flag; name/use support
subroutine definition and invocation; indirect re-enters the
rule set at a resolved offset; offset emits the resolved file
position as Value::Uint for printf-style message substitution.
See MetaType for the individual variants.
§Examples
use libmagic_rs::parser::ast::{MetaType, TypeKind};
let default_rule = TypeKind::Meta(MetaType::Default);
assert_eq!(default_rule, TypeKind::Meta(MetaType::Default));Implementations§
Source§impl TypeKind
impl TypeKind
Sourcepub const fn bit_width(&self) -> Option<u32>
pub const fn bit_width(&self) -> Option<u32>
Returns the bit width of integer types, or None for non-integer types (e.g., String).
§Examples
use libmagic_rs::parser::ast::{TypeKind, Endianness};
assert_eq!(TypeKind::Byte { signed: false }.bit_width(), Some(8));
assert_eq!(TypeKind::Short { endian: Endianness::Native, signed: true }.bit_width(), Some(16));
assert_eq!(TypeKind::Long { endian: Endianness::Native, signed: true }.bit_width(), Some(32));
assert_eq!(TypeKind::Quad { endian: Endianness::Native, signed: true }.bit_width(), Some(64));
assert_eq!(TypeKind::Float { endian: Endianness::Native }.bit_width(), Some(32));
assert_eq!(TypeKind::Double { endian: Endianness::Native }.bit_width(), Some(64));
assert_eq!(TypeKind::String { max_length: None }.bit_width(), None);