Skip to main content

Crate palate

Crate palate 

Source
Expand description

§Palate

File type detection combining the best of tft and hyperpolyglot.

§Acknowledgments

This project is a reassembly of code from several excellent projects:

  • tft - Tree-sitter File Type, providing fast file type detection using tree-sitter grammars
  • Neovim - The original source of filetype detection heuristics and patterns
  • hyperpolyglot - Language detection library with comprehensive language patterns

This crate essentially combines and curates the detection logic from these sources into a unified, ergonomic API.

§License

GPL-3.0-or-later

This project is derived from tft (GPL-3.0-or-later), which itself incorporates code from Neovim (Apache-2.0/Vim license). As a derivative of GPL-3.0 work, this project is licensed under GPL-3.0-or-later.

§Features

  • Comprehensive file type detection
  • Fast PHF-based lookups
  • Content-based detection with heuristics
  • Shebang interpretation support
  • Naive Bayes classifier fallback (with classifier feature)

§Usage

use palate::{detect, FileType};

// Detect file type with fallback to Text
let ft = detect("main.rs", "");
assert_eq!(FileType::Rust, ft);

// Try detection without fallback
let ft = palate::try_detect("unknown.xyz", "");
assert_eq!(None, ft);

§Detection Pipeline

  1. Path suffix matching
  2. Filename matching
  3. Pattern matching (with priorities)
  4. File extension matching (PHF map)
  5. Negative priority patterns
  6. Content-based detection (dynamic resolvers)

§Features

  • detect (default): Enable file type detection
  • classifier: Enable naive Bayes classifier
  • serde: Enable serde serialization for FileType

Structs§

PeekableReader
A reader that can peek at content without consuming the stream.

Enums§

FileType
A non-exhaustive list of text file types.

Functions§

detect
Same as try_detect but automatically falling back to FileType::Text where try_detect would return None.
detect_with_reader
Detect file type from path and peekable reader using the detection pipeline.
is_text_file
Check if a file is text (not binary).
try_detect
Try to detect a FileType given a file’s path and content.