file-identify
File identification library for Rust.
Given a file (or some information about a file), return a set of standardized tags identifying what the file is.
This is a Rust port of the Python identify library.
Features
- 🚀 Fast: Built in Rust with Perfect Hash Functions (PHF) for compile-time optimization
- 📁 Comprehensive: Identifies 315+ file types and formats
- 🔍 Smart detection: Uses file extensions, content analysis, and shebang parsing
- 📦 Library + CLI: Use as a Rust library or command-line tool
- ⚡ Zero overhead: PHF provides O(1) lookups with no runtime hash computation
- 🎯 Memory efficient: Static data structures with no lazy initialization
- ✅ Well-tested: Extensive test suite ensuring reliability
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
Usage
Library Usage
use ;
// Identify a file from its path
let tags = tags_from_path.unwrap;
println!; // {"file", "text", "python", "non-executable"}
// Identify from filename only
let tags = tags_from_filename;
println!; // {"text", "shell", "bash"}
// Identify from interpreter
let tags = tags_from_interpreter;
println!; // {"python", "python3"}
Command Line Usage
# Install the CLI tool
# Identify a file
# Use filename only (don't read file contents)
# Get help
How it works
A call to tags_from_path does this:
- What is the type: file, symlink, directory? If it's not file, stop here.
- Is it executable? Add the appropriate tag.
- Do we recognize the file extension? If so, add the appropriate tags, stop here. These tags would include binary/text.
- Peek at the first 1KB of the file. Use these to determine whether it is binary or text, add the appropriate tag.
- If identified as text above, try to read and interpret the shebang, and add appropriate tags.
By design, this means we don't need to partially read files where we recognize the file extension.
Development
Setup
# Clone the repository
# Build the project
# Run tests
# Run the CLI
Pre-commit hooks
This project uses pre-commit hooks to ensure code quality:
Testing
# Run all tests
# Run with coverage (requires cargo-tarpaulin)
Releasing
To create a new release:
- Update version in
Cargo.toml - Update
CHANGELOG.mdwith new version notes - Commit changes:
git commit -am "Bump version to x.y.z" - Create and push tag:
git tag vx.y.z && git push origin vx.y.z
The release workflow will automatically:
- Run full test suite
- Publish to crates.io using
RELEASE_TOKENsecret - Create GitHub release with auto-generated notes
License
MIT