Skip to content

Commit

Permalink
Merge pull request #275 from quartiq/keylookup-named-len-check
Browse files Browse the repository at this point in the history
KeyLookup: ensure at least one child in Named
  • Loading branch information
jordens authored Feb 4, 2025
2 parents 5279641 + 6088508 commit 23fc746
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions miniconf/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<T: TreeAny> TreeAny for Option<T> {

/////////////////////////////////////////////////////////////////////////////////////////

const RESULT_LOOKUP: KeyLookup = KeyLookup::Named(&["Ok", "Err"]);
const RESULT_LOOKUP: KeyLookup = KeyLookup::named(&["Ok", "Err"]);

impl<T: TreeKey, E: TreeKey> TreeKey for Result<T, E> {
#[inline]
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<T: TreeAny, E: TreeAny> TreeAny for Result<T, E> {

/////////////////////////////////////////////////////////////////////////////////////////

const BOUND_LOOKUP: KeyLookup = KeyLookup::Named(&["Included", "Excluded"]);
const BOUND_LOOKUP: KeyLookup = KeyLookup::named(&["Included", "Excluded"]);

impl<T: TreeKey> TreeKey for Bound<T> {
#[inline]
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<T: TreeAny> TreeAny for Bound<T> {

/////////////////////////////////////////////////////////////////////////////////////////

const RANGE_LOOKUP: KeyLookup = KeyLookup::Named(&["start", "end"]);
const RANGE_LOOKUP: KeyLookup = KeyLookup::named(&["start", "end"]);

impl<T: TreeKey> TreeKey for Range<T> {
#[inline]
Expand Down Expand Up @@ -517,7 +517,7 @@ impl<T: TreeSerialize> TreeSerialize for RangeInclusive<T> {

/////////////////////////////////////////////////////////////////////////////////////////

const RANGE_FROM_LOOKUP: KeyLookup = KeyLookup::Named(&["start"]);
const RANGE_FROM_LOOKUP: KeyLookup = KeyLookup::named(&["start"]);

impl<T: TreeKey> TreeKey for RangeFrom<T> {
#[inline]
Expand Down Expand Up @@ -594,7 +594,7 @@ impl<T: TreeAny> TreeAny for RangeFrom<T> {

/////////////////////////////////////////////////////////////////////////////////////////

const RANGE_TO_LOOKUP: KeyLookup = KeyLookup::Named(&["end"]);
const RANGE_TO_LOOKUP: KeyLookup = KeyLookup::named(&["end"]);

impl<T: TreeKey> TreeKey for RangeTo<T> {
#[inline]
Expand Down
13 changes: 11 additions & 2 deletions miniconf/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ pub enum KeyLookup {
}

impl KeyLookup {
/// Return a homogenenous unnamed KeyLookup
/// Return a named KeyLookup
#[inline]
pub const fn named(names: &'static [&'static str]) -> Self {
if names.is_empty() {
panic!("Must have at least one child");
}
Self::Named(names)
}

/// Return a homogenenous, unnamed KeyLookup
#[inline]
pub const fn homogeneous(len: usize) -> Self {
match NonZero::new(len) {
Expand All @@ -27,7 +36,7 @@ impl KeyLookup {
}
}

/// Return a homogenenous unnamed KeyLookup
/// Return a heterogeneous numbered KeyLookup
#[inline]
pub const fn numbered(len: usize) -> Self {
match NonZero::new(len) {
Expand Down
2 changes: 1 addition & 1 deletion miniconf_derive/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Tree {
},
)
},
Some(names) => quote!(::miniconf::KeyLookup::Named(&[#(#names ,)*])),
Some(names) => quote!(::miniconf::KeyLookup::named(&[#(#names ,)*])),
};
let traverse_arms = fields.iter().enumerate().map(|(i, f)| f.traverse_by_key(i));
let index = self.index();
Expand Down

0 comments on commit 23fc746

Please sign in to comment.