editorconfig-parser
A fast, spec-compliant Rust implementation of an EditorConfig parser.
Features
- Spec-compliant - fully implements the EditorConfig specification
- Zero dependencies - pure Rust implementation with no external dependencies
- Fast and safe - no unsafe code, optimized for performance
- Comprehensive property support - handles all standard EditorConfig properties
- Path resolution - resolves properties for specific file paths
Usage
Add this to your Cargo.toml:
[]
= "0.0.1"
Parsing an EditorConfig file
use EditorConfig;
let config_text = r#"
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
max_line_length = off
[Makefile]
indent_style = tab
"#;
let config = parse;
// Check if this is a root config
assert!;
// Access sections
for section in config.sections
Resolving properties for a file path
use EditorConfig;
use Path;
let config = parse;
let properties = config.resolve;
Supported Properties
The parser supports all standard EditorConfig properties:
| Property | Type | Values |
|---|---|---|
indent_style |
IdentStyle |
tab, space |
indent_size |
usize |
Positive integer |
tab_width |
usize |
Positive integer |
end_of_line |
EndOfLine |
lf, cr, crlf |
charset |
Charset |
latin1, utf-8, utf-8-bom, utf-16be, utf-16le |
trim_trailing_whitespace |
bool |
true, false |
insert_final_newline |
bool |
true, false |
max_line_length |
MaxLineLength |
Positive integer or off |
Note: max_line_length is not part of the official EditorConfig spec but is commonly used by tools like Prettier.
How It Works
The parser follows the EditorConfig specification:
- Reads the file line by line
- Removes leading and trailing whitespace
- Ignores blank lines and comments (
#or;) - Parses
root = truein the preamble (before any sections) - Parses section headers
[pattern]as glob patterns - Parses key-value pairs
key = valuewithin sections - All values are case-insensitive
Development
Building
Running Tests
License
MIT