| Crates.io | config_to_rs |
| lib.rs | config_to_rs |
| version | 0.2.3 |
| created_at | 2025-02-16 23:09:57.007346+00 |
| updated_at | 2025-05-19 10:11:42.260677+00 |
| description | Convert config files to Rust code |
| homepage | |
| repository | https://github.com/TomLonergan03/config_to_rs |
| max_upload_size | |
| id | 1558224 |
| size | 27,325 |
For converting config files to Rust structs, so they can be compiled in to a binary. Allows for using config files in no-std no-alloc environments.
Work in progress.
Initially supports YAML.
config_to_rs attribute instead of paths relative to
the crate root. This is useful if you have a complex project structure where you
have multiple local crates in a single cargo project.Given a YAML file:
parsing: "working"
age: 22
enabled: true
time: 22.34
recurse:
thing: 1
recurse:
thing: 3
array: ["a", "b", "c"]
empty_array: []
array_of_arrays:
- ["a", "b", "c"]
- ["d", "e", "f"]
array_of_objects:
- name: "a"
age: 1
- name: "b"
age: 2
- name: "c"
age: 3
and a Rust struct:
#[config_to_rs(yaml, path/to/config.yaml)]
pub struct Config;
the macro will generate the following Rust code:
struct Config {
pub parsing: &'static str,
pub age: i64,
pub enabled: bool,
pub time: f64,
pub recurse: ConfigRecurse,
pub array: [&'static str; 3usize],
pub empty_array: [(); 0usize],
pub array_of_arrays: [[&'static str; 3usize]; 2usize],
pub array_of_objects: [ConfigArrayOfObjects; 3usize],
}
struct ConfigRecurse {
pub thing: i64,
pub recurse: ConfigRecurseRecurse,
}
struct ConfigRecurseRecurse {
pub thing: i64,
}
struct ConfigArrayOfObjects {
pub name: &'static str,
pub age: i64,
}
pub const CONFIG: Config = Config {
parsing: "working",
age: 22i64,
enabled: true,
time: 22.34f64,
recurse: ConfigRecurse {
thing: 1i64,
recurse: ConfigRecurseRecurse { thing: 3i64 },
},
array: ["a", "b", "c"],
empty_array: [],
array_of_arrays: [["a", "b", "c"], ["d", "e", "f"]],
array_of_objects: [
ConfigArrayOfObjects {
name: "a",
age: 1i64,
},
ConfigArrayOfObjects {
name: "b",
age: 2i64,
},
ConfigArrayOfObjects {
name: "c",
age: 3i64,
},
],
};
Which then allows for accessing the config values as you would any other Rust struct:
// items in a hashmap
assert_eq!(CONFIG.parsing, "working");
// items in a list
assert_eq!(CONFIG.array[1], "b");
// nested hashmaps
assert_eq!(CONFIG.recurse.recurse.thing, 3);