tommy 0.1.26

Simple toml parser built to parse configuration files for rust projects
Documentation

📦 Information

For my rust programs I was using various serde/toml crates for parsing (what ultimately only were) simple configuration files, and at some point I decided that it was unnecessary.

Tommy is dumb, blunt and clunky. It's built for parsing simple configuration files containing tables of integers, strings, chars, floats and booleans - it can't do anything more and it doesn't need to do anything more.


✨ Usage

use tommy::*;

fn main() {
    macro_rules! config_table {
        ($nme:ident { $($fld:ident : $typ:ty),* $(,)? }) => {
            #[derive(Debug)]
            #[allow(unused)]
            struct $nme {
            $($fld: $typ),*
            }
            from_table_struct!($nme {
            $($fld: $typ),*
            });
        };
    }

    config_table!(Cursor {
        blink: bool,
        blink_duration: i32,
    });

    config_table!(Window {
        title: String,
        width: f64,
        height: f64,
    });

    config_table!(Icons {
        entry: char,
        exit: char,
        controls: char,
    });

    let parsed_user = ParseConfig::from_file("test.toml".to_string()).unwrap();
    let parsed_fabk = ParseConfig::from_file("fallback.toml".to_string()).unwrap();

    /// # or instead of using macro
    /// let cursor_conf: Cursor = parsed_user
    ///     .table("cursor")
    ///     .or_else(|| parsed_fabk.table("cursor"))
    ///     .unwrap();
    macro_rules! load_conf {
        ($var:ident : $ty:ty) => {
            let $var: $ty = parsed_user
            .table(stringify!($ty).to_lowercase().as_str())
            .or_else(|| {
            println!(
            "WARNING: fallback was used for table: {}",
            stringify!($ty)
            );
            parsed_fabk.table(stringify!($ty).to_lowercase().as_str())
            })
            .unwrap();
        };
    }

    load_conf!(cursor_conf: Cursor);
    load_conf!(window_conf: Window);
    load_conf!(icons_conf: Icons);

    println!("{:#?}", cursor_conf);
    println!("{:#?}", window_conf);
    println!("{:#?}", icons_conf);
}

📜 License

This project is licensed under the MIT License.