add README description

This commit is contained in:
Max Känner 2024-04-15 21:05:19 +02:00
parent babd99d24c
commit 3ad76d1c00
2 changed files with 45 additions and 7 deletions

View File

@ -1,3 +1,45 @@
# lib-rs # macroconf
Template for rust libraries This library provides a macro to create configs with metadata attached to the values.
The [miniconf](https://github.com/quartiq/miniconf) library is used to make the metadata and the values accessible.
## Example
```rust
use miniconf::Tree;
use macroconf::config;
#[config]
#[derive(Tree)]
struct Config {
ordinary: i32,
#[min = 10]
#[max = 20]
clamped: u8
#[default = 42]
has_default: i32;
/// Doc comments are used as description
with_description: i32
}
/* The resulting miniconf tree looks like this:
* /
* |-ordinary
* |
* |-clamped
* | |-value
* | |-min
* | |-max
* |
* |-has_default
* | |-value
* | |-default
* |
* |-with_description
* |-value
* |-description
*/
```

View File

@ -62,7 +62,6 @@ fn generate_helper_struct(
let mut new_type_miniconf_names = vec![]; let mut new_type_miniconf_names = vec![];
let mut new_type_miniconf_consts = vec![]; let mut new_type_miniconf_consts = vec![];
let mut extra_new_checks = TokenStream2::new(); let mut extra_new_checks = TokenStream2::new();
let mut generate_new_type = false;
for attr in &field.attrs { for attr in &field.attrs {
if let Some((new_type_impl, new_check, const_ident, key)) = if let Some((new_type_impl, new_check, const_ident, key)) =
parse_min(&new_type_ident, attr, &ty) parse_min(&new_type_ident, attr, &ty)
@ -71,7 +70,6 @@ fn generate_helper_struct(
new_type_miniconf_consts.push(const_ident); new_type_miniconf_consts.push(const_ident);
new_type_miniconf_names.push(key); new_type_miniconf_names.push(key);
extra_new_checks.extend(new_check); extra_new_checks.extend(new_check);
generate_new_type = true;
} }
if let Some((new_type_impl, new_check, const_ident, key)) = if let Some((new_type_impl, new_check, const_ident, key)) =
parse_max(&new_type_ident, attr, &ty) parse_max(&new_type_ident, attr, &ty)
@ -80,13 +78,11 @@ fn generate_helper_struct(
new_type_miniconf_consts.push(const_ident); new_type_miniconf_consts.push(const_ident);
new_type_miniconf_names.push(key); new_type_miniconf_names.push(key);
extra_new_checks.extend(new_check); extra_new_checks.extend(new_check);
generate_new_type = true;
} }
if let Some((new_type_impl, const_ident, key)) = parse_default(&new_type_ident, attr, &ty) { if let Some((new_type_impl, const_ident, key)) = parse_default(&new_type_ident, attr, &ty) {
new_type_impls.extend(new_type_impl); new_type_impls.extend(new_type_impl);
new_type_miniconf_consts.push(const_ident); new_type_miniconf_consts.push(const_ident);
new_type_miniconf_names.push(key); new_type_miniconf_names.push(key);
generate_new_type = true;
} }
if let Some((new_type_impl, const_ident, key)) = parse_description(&new_type_ident, attr) { if let Some((new_type_impl, const_ident, key)) = parse_description(&new_type_ident, attr) {
new_type_impls.extend(new_type_impl); new_type_impls.extend(new_type_impl);
@ -94,7 +90,7 @@ fn generate_helper_struct(
new_type_miniconf_names.push(key); new_type_miniconf_names.push(key);
} }
} }
if !generate_new_type { if new_type_miniconf_names.is_empty() {
return None; return None;
} }
field.attrs.retain(|attr| { field.attrs.retain(|attr| {