Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for a tree(skip) field attribute #183

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* Deserializing with borrowed data is now supported.
* [derive] Added `#[tree(skip)]` macro attribute to allow skipping entries.

## [0.8.0](https://github.com/quartiq/miniconf/compare/v0.7.1...v0.8.0) - 2023-08-03

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ struct Settings {
array: [i32; 2],
option: Option<i32>,

// Exclude an element (not Deserialize/Serialize)
#[tree(skip)]
skipped: (),

// Exposing elements of containers
// ... by field name
#[tree]
Expand Down
19 changes: 11 additions & 8 deletions miniconf_derive/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ impl StructField {
}
.iter()
.cloned()
.map(Self::new)
.filter_map(Self::new)
.collect()
}

pub fn new(field: syn::Field) -> Self {
let depth = Self::parse_depth(&field);
Self { field, depth }
}

fn parse_depth(field: &syn::Field) -> usize {
pub fn new(field: syn::Field) -> Option<Self> {
let mut depth = 0;
let mut skip = false;

for attr in field.attrs.iter() {
if attr.path().is_ident("tree") {
Expand All @@ -39,14 +35,21 @@ impl StructField {
let lit: LitInt = content.parse()?;
depth = lit.base10_parse()?;
Ok(())
} else if meta.path.is_ident("skip") {
skip = true;
Ok(())
} else {
Err(meta.error(format!("unrecognized miniconf attribute {:?}", meta.path)))
}
})
.unwrap();
}
}
depth
if skip {
None
} else {
Some(Self { field, depth })
}
}

fn walk_type_params<F>(
Expand Down