| Crates.io | tommy |
| lib.rs | tommy |
| version | 0.1.3 |
| created_at | 2026-01-11 00:06:00.81831+00 |
| updated_at | 2026-01-11 17:04:44.879643+00 |
| description | Simple toml parser built to parse configuration files for rust projects |
| homepage | https://www.simondanielsson.se |
| repository | https://github.com/simon-danielsson/tommy |
| max_upload_size | |
| id | 2034976 |
| size | 22,016 |
A light-weight toml parser for
configuration files in rust projects.
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.
use tommy::*;
const FALLBACK_CONF: &str = include_str!("../fallback.toml");
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,
});
struct Config {
cursor: Cursor,
window: Window,
icons: Icons,
}
impl Config {
fn new(cursor: Cursor, window: Window, icons: Icons) -> Self {
Self {
cursor,
window,
icons,
}
}
}
fn main() {
let user_input: String = "test.toml".to_string();
let parsed_user = ParseConfig::from_file(user_input.into()).unwrap();
let parsed_fabk = ParseConfig::from_file(FALLBACK_CONF.into()).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);
let config: Config = Config::new(cursor_conf, window_conf, icons_conf);
println!("{:#?}", config.cursor);
println!("{:#?}", config.window);
println!("{:#?}", config.icons);
}
This project is licensed under the MIT License.