2 releases

new 0.1.1 Apr 18, 2025
0.1.0 Apr 18, 2025

#1038 in Procedural macros


Used in confetti-rs

MIT license

15KB
188 lines

Confetti-rs

Rust crates.io Documentation License: MIT

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

Installation

Add this to your Cargo.toml:

[dependencies]
confetti-rs = "0.1.0"

# If you need derive macros
confetti-rs = { version = "0.1.0", features = ["derive"] }

Basic Usage

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);

    // Serialize to a string
    let serialized = to_string(&server_config)?;
    
    Ok(())
}

More Information

For more examples and detailed documentation, please visit:

Dependencies

~1.5MB
~38K SLoC