Skip to main content

TypeKind

Enum TypeKind 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Byte

Single byte

§Examples

use libmagic_rs::parser::ast::TypeKind;

let byte = TypeKind::Byte { signed: true };
assert_eq!(byte, TypeKind::Byte { signed: true });

Fields

§signed: bool

Whether value is signed

§

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

Fields

§endian: Endianness

Byte order

§signed: bool

Whether value is signed

§

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

Fields

§endian: Endianness

Byte order

§signed: bool

Whether value is signed

§

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

Fields

§endian: Endianness

Byte order

§signed: bool

Whether value is signed

§

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: Endianness

Byte 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: Endianness

Byte 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 });

Fields

§endian: Endianness

Byte order

§utc: bool

true = UTC, false = local time

§

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

Fields

§endian: Endianness

Byte order

§utc: bool

true = UTC, false = local time

§

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

Fields

§max_length: Option<usize>

Maximum length to read

§

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: Endianness

Endianness 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

§max_length: Option<usize>

Maximum length to read (caps the length value)

§length_width: PStringLengthWidth

Width of the length prefix

§length_includes_itself: bool

Whether the stored length includes the length field itself (/J flag)

§

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: RegexFlags

Modifier 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: RegexCount

Scan 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: NonZeroUsize

Scan 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

Source

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

Trait Implementations§

Source§

impl Clone for TypeKind

Source§

fn clone(&self) -> TypeKind

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 TypeKind

Source§

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

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

impl<'de> Deserialize<'de> for TypeKind

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 TypeKind

Source§

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

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 Eq for TypeKind

Source§

impl StructuralPartialEq for TypeKind

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