packkit/detect

Types

Opaque detection result. Callers inspect it through accessors rather than through public constructors.

pub opaque type Detected

Values

pub fn archive(
  detected: Detected,
) -> option.Option(archive.ArchiveFormat)

Read the detected archive family if one was found.

pub fn codec(detected: Detected) -> option.Option(codec.Codec)

Read the detected codec if one was found.

pub fn extension(detected: Detected) -> option.Option(String)

Read the matched extension label, if any.

pub fn from_bytes(
  bytes: BitArray,
) -> Result(Detected, error.DetectError)

Detect a format from the leading bytes of an input stream.

Signatures are matched as strictly as practical:

  • gzip (1F 8B) also requires the compression-method byte to be 08 (DEFLATE), since RFC 1952 reserves the other values and no production gzip stream uses them.
  • zlib (78 _) requires CMF.CM == 8 (DEFLATE), CMF.CINFO ≤ 7 (15-bit window), and (CMF*256 + FLG) % 31 == 0 per RFC 1950.
  • bzip2 (BZh) additionally requires the block-size byte to be an ASCII digit 1..9.
  • lz4 (04 22 4D 18) and .Z (1F 9D) keep their fixed magic.
  • zstd skippable frames (magic 184D2A50..184D2A5F) are recognised as zstd so wrappers that embed user metadata in skippable frames at the start of the stream do not fail to detect.
  • snappy framed format starts with a stream identifier chunk (FF 06 00 00 sNaPpY); the raw snappy block format has no magic so it can only be detected from filename.

Looser signatures like a bare 0x78 _ would false-positive on any byte stream whose first byte happens to be 0x78.

pub fn from_filename(
  path: String,
) -> Result(Detected, error.DetectError)

Detect a format from a filename or path suffix.

pub fn from_path_or_bytes(
  path path: String,
  bytes bytes: BitArray,
) -> Result(Detected, error.DetectError)

Try filename detection first, then fall back to magic-byte detection on the supplied content. Mirrors the resolution order most CLI tools use: a meaningful extension is a strong signal, but when the path is uninformative (-, /dev/stdin, an arbitrary upload, etc.) the file’s first bytes still pin the format.

On total failure (neither the filename nor the magic bytes classified the input) the returned DetectUnknownFormat carries the original caller-supplied path in its input field — never the internal "byte-signature scan" sentinel — so the message stays specific to the user’s input.

pub fn recipe(detected: Detected) -> option.Option(recipe.Recipe)

Read the detected recipe if one was found.

Search Document