From 3ad76d1c0022611c20b1774faef24f0c5ed4c15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Mon, 15 Apr 2024 21:05:19 +0200 Subject: [PATCH] add README description --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 6 +----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cfbac2a..a6374e3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,45 @@ -# lib-rs +# macroconf -Template for rust libraries \ No newline at end of file +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 + */ +``` diff --git a/src/lib.rs b/src/lib.rs index bf6d479..64d25da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,6 @@ fn generate_helper_struct( let mut new_type_miniconf_names = vec![]; let mut new_type_miniconf_consts = vec![]; let mut extra_new_checks = TokenStream2::new(); - let mut generate_new_type = false; for attr in &field.attrs { if let Some((new_type_impl, new_check, const_ident, key)) = 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_names.push(key); extra_new_checks.extend(new_check); - generate_new_type = true; } if let Some((new_type_impl, new_check, const_ident, key)) = 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_names.push(key); 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) { new_type_impls.extend(new_type_impl); new_type_miniconf_consts.push(const_ident); 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) { new_type_impls.extend(new_type_impl); @@ -94,7 +90,7 @@ fn generate_helper_struct( new_type_miniconf_names.push(key); } } - if !generate_new_type { + if new_type_miniconf_names.is_empty() { return None; } field.attrs.retain(|attr| {