YINI-RS
A fast, safe Rust parser and writer for the YINI (Yet Another INI) configuration file format. YINI extends the traditional INI format with visual nesting, arrays, and improved readability.
Features
- Zero dependencies: Pure Rust implementation with minimal dependencies
- Memory safe: Written in 100% safe Rust with no risk of buffer overflows
- Type support: Strings, integers, doubles, booleans, and arrays
- Comment support: Both
//line comments and/* */multiline comments - Error handling: Comprehensive error handling with detailed messages
- Round-trip: Parse and write YINI files without data loss
YINI Format
YINI uses a simple but powerful syntax:
# Root-level properties
app_name = 'MyApp'
version = '1.0.0'
debug = true
^ server # Accessed with parser.section("server")
host = 'localhost'
port = 8080
^^ database # Accessed with parser.section("server").section("database")
host = 'db.example.com'
port = 5432
^^^ credentials # Accessed with parser.section("server").section("database").section("credentials")
username = 'admin'
password = 'secret'
^ features
enabled = ['auth', 'logging', 'cache'] # Arrays with []
flags = [true, false, true]
numbers = [1, 2, 3, 4, 5]
Supported Types
- Strings:
'single quotes'or"double quotes" - Integers:
42,-10 - Doubles:
3.14,-2.5 - Booleans:
true,false,yes,no,on,off - Arrays:
[item1, item2, item3]
Comments
- Line comments:
// This is a comment - End-of-line comments:
key = value // Comment here - Multiline comments:
/* This is a multiline comment */ - Block comments spanning multiple lines:
/* * This is a longer comment * that spans multiple lines */
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Example
use ;
API Reference
Parser
Main struct for parsing and writing YINI files.
Methods:
parse_file(&mut self, filename: &str) -> Result<()>- Parse a YINI fileparse_string(&mut self, content: &str) -> Result<()>- Parse YINI content from stringwrite_file(&self, filename: &str) -> Result<()>- Write configuration to filewrite_string(&self) -> String- Write configuration to stringroot(&self) -> &Section- Access the root sectionroot_mut(&mut self) -> &mut Section- Access the root section mutablysection(&mut self, name: &str) -> &mut Section- Access or create a section
Value
Represents a configuration value with automatic type conversion.
Type checking:
is_string(&self) -> boolis_int(&self) -> boolis_double(&self) -> boolis_bool(&self) -> boolis_array(&self) -> bool
Value access:
as_string(&self) -> Result<String>as_int(&self) -> Result<i32>as_double(&self) -> Result<f64>as_bool(&self) -> Result<bool>as_array(&self) -> Result<Vec<Value>>
Section
Represents a configuration section containing values and subsections.
Methods:
- Implements
Index<&str>forvalue[key]access at(&self, key: &str) -> Result<&Value>- Safe value accesshas_value(&self, key: &str) -> bool- Check if value existssection(&mut self, name: &str) -> &mut Section- Access or create subsectionget_section(&self, name: &str) -> Result<&Section>- Get subsection (read-only)has_section(&self, name: &str) -> bool- Check if section exists
Error Types
Error::ParseError- Returned when parsing failsError::FileError- Returned when file operations failError::TypeError- Returned for type conversion errorsError::KeyNotFound- Returned when accessing non-existent keysError::SectionNotFound- Returned when accessing non-existent sections
Advanced Usage
Working with Arrays
use ;
let mut parser = new;
parser.parse_string?;
let numbers = parser.as_array?;
for num in &numbers
Error Handling
use ;
let mut parser = new;
// Parse errors are returned as Results
match parser.parse_string
// Missing key errors
match parser.root.at
Type Conversion
use ;
let mut parser = new;
parser.parse_string?;
// Automatic type conversion
let num: i32 = parser.as_int?; // "42" -> 42
let flag: bool = parser.as_bool?; // "true" -> true
let text: String = parser.as_string?; // 100 -> "100"
Building
Simply add this crate to your Cargo.toml and you're ready to go! To run the tests:
# Run tests
# Run examples
# Build documentation
Requirements
- Rust 1.56.0 or later (2021 edition)
- No additional system dependencies
Performance
YINI-RS is designed for speed and efficiency:
- Fast parsing: Optimized for large configuration files
- Low memory usage: Minimal allocations during parsing
- Zero-copy: String values reference the original input when possible
Benchmark results on a 1000-line configuration file:
- Parse time: ~9ms
- Write time: ~0.4ms
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.