Expand description
§Confetti-rs
A configuration language and parser library for Rust, with a flexible mapper for converting between configuration files and Rust structs.
§Features
- Simple, intuitive configuration syntax
- A powerful parser with customizable options
- Automatic mapping between configuration and Rust structs
- Support for custom data types
- Comprehensive error handling
§Basic Usage
Here’s a simple example of how to use Confetti-rs:
use confetti_rs::{ConfMap, from_str, to_string};
use std::error::Error;
// Define a configuration structure
#[derive(ConfMap, Debug)]
struct ServerConfig {
host: String,
port: i32,
#[conf_map(name = "ssl-enabled")]
ssl_enabled: bool,
max_connections: Option<i32>,
}
fn main() -> Result<(), Box<dyn Error>> {
// Configuration string in Confetti syntax
let config_str = r#"
ServerConfig {
host "localhost";
port 8080;
ssl-enabled false;
max_connections 100;
}
"#;
// Parse the configuration
let server_config = from_str::<ServerConfig>(config_str)?;
println!("Loaded config: {:?}", server_config);
// Modify the configuration
let new_config = ServerConfig {
host: "0.0.0.0".to_string(),
port: 443,
ssl_enabled: true,
max_connections: Some(200),
};
// Serialize to a string
let serialized = to_string(&new_config)?;
println!("Serialized config:\n{}", serialized);
Ok(())
}
§Configuration Syntax
Confetti-rs uses a simple, readable syntax:
ⓘ
DirectiveName {
nested_directive "value";
another_directive 123;
block_directive {
setting true;
array 1, 2, 3, 4;
}
}
§Documentation
For more examples and detailed documentation, please visit:
Re-exports§
pub use crate::mapper::FromConf;
pub use crate::mapper::MapperError;
pub use crate::mapper::MapperOptions;
pub use crate::mapper::ToConf;
pub use crate::mapper::ValueConverter;
Modules§
Structs§
- Conf
Argument - Represents a configuration argument.
- Conf
Comment - Represents a comment in the configuration.
- Conf
Directive - Represents a configuration directive.
- Conf
Options - Options for parsing configuration.
- Conf
Unit - Represents a configuration unit.
Enums§
- Conf
Error - Represents an error that can occur during parsing.
Functions§
- from_
file - Load configuration from a file into a struct
- from_
str - Load configuration from a string into a struct
- parse
- Parses a configuration string.
- to_file
- Save a struct to a configuration file
- to_
string - Convert a struct to a configuration string
Derive Macros§
- ConfMap
- Derives the FromConf and ToConf traits for struct types