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

releace 0.17.0 #257

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/quartiq/miniconf/compare/v0.16.3...HEAD) - DATE
## [0.17.0](https://github.com/quartiq/miniconf/compare/v0.16.3...v0.17.0) - 2024-10-25

### Changed

Expand Down
4 changes: 2 additions & 2 deletions miniconf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "miniconf"
# Sync all crate versions and the py client
version = "0.16.3"
version = "0.17.0"
authors = [
"James Irwin <irwineffect@gmail.com>",
"Ryan Summers <ryan.summers@vertigo-designs.com>",
Expand All @@ -16,7 +16,7 @@ categories = ["embedded", "config", "data-structures", "parsing"]

[dependencies]
serde = { version = "1.0.120", default-features = false }
miniconf_derive = { path = "../miniconf_derive", version = "0.16.1", optional = true }
miniconf_derive = { path = "../miniconf_derive", version = "0.17.0", optional = true }
itoa = "1.0.4"
serde-json-core = { version = "0.6.0", optional = true }
postcard = { version = "1.0.8", optional = true }
Expand Down
5 changes: 4 additions & 1 deletion miniconf/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl ::core::error::Error for Traversal {}

impl Traversal {
/// Pass it up one hierarchy depth level, incrementing its usize depth field by one.
#[inline]
pub fn increment(self) -> Self {
match self {
Self::Absent(i) => Self::Absent(i + 1),
Expand Down Expand Up @@ -144,6 +143,7 @@ impl<E: core::error::Error + 'static> core::error::Error for Error<E> {
// Try to extract the Traversal from an Error
impl<E> TryFrom<Error<E>> for Traversal {
type Error = Error<E>;
#[inline]
fn try_from(value: Error<E>) -> Result<Self, Self::Error> {
match value {
Error::Traversal(e) => Ok(e),
Expand All @@ -153,13 +153,15 @@ impl<E> TryFrom<Error<E>> for Traversal {
}

impl<E> From<Traversal> for Error<E> {
#[inline]
fn from(value: Traversal) -> Self {
Self::Traversal(value)
}
}

impl<E> Error<E> {
/// Pass an `Error<E>` up one hierarchy depth level, incrementing its usize depth field by one.
#[inline]
pub fn increment(self) -> Self {
match self {
Self::Traversal(t) => Self::Traversal(t.increment()),
Expand All @@ -169,6 +171,7 @@ impl<E> Error<E> {
}

/// Pass a `Result<usize, Error<E>>` up one hierarchy depth level, incrementing its usize depth field by one.
#[inline]
pub fn increment_result(result: Result<usize, Self>) -> Result<usize, Self> {
match result {
Ok(i) => Ok(i + 1),
Expand Down
15 changes: 14 additions & 1 deletion miniconf/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ macro_rules! impl_tuple {
#[allow(unreachable_code, unused_mut, unused)]
impl<$($t: TreeKey),+> TreeKey for ($($t,)+) {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let mut walk = W::internal();
let k = KeyLookup::homogeneous($n);
let mut walk = W::internal();
$(walk = walk.merge(&$t::traverse_all()?, Some($i), &k)?;)+
Ok(walk)
}
Expand Down Expand Up @@ -104,8 +104,14 @@ impl_tuple!(8 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7);

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

struct Assert<const L: usize, const R: usize>;
impl<const L: usize, const R: usize> Assert<L, R> {
const GREATER: () = assert!(L > R);
}

impl<T: TreeKey, const N: usize> TreeKey for [T; N] {
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
let () = Assert::<N, 0>::GREATER; // internal nodes must have at least one leaf
W::internal().merge(&T::traverse_all()?, None, &KeyLookup::homogeneous(N))
}

Expand All @@ -114,6 +120,7 @@ impl<T: TreeKey, const N: usize> TreeKey for [T; N] {
K: Keys,
F: FnMut(usize, Option<&'static str>, NonZero<usize>) -> Result<(), E>,
{
let () = Assert::<N, 0>::GREATER; // internal nodes must have at least one leaf
let k = KeyLookup::homogeneous(N);
let index = keys.next(&k)?;
func(index, None, k.len).map_err(|err| Error::Inner(1, err))?;
Expand Down Expand Up @@ -168,10 +175,12 @@ impl<T: TreeAny, const N: usize> TreeAny for [T; N] {
/////////////////////////////////////////////////////////////////////////////////////////

impl<T: TreeKey> TreeKey for Option<T> {
#[inline]
fn traverse_all<W: Walk>() -> Result<W, W::Error> {
T::traverse_all()
}

#[inline]
fn traverse_by_key<K, F, E>(keys: K, func: F) -> Result<usize, Error<E>>
where
K: Keys,
Expand All @@ -182,6 +191,7 @@ impl<T: TreeKey> TreeKey for Option<T> {
}

impl<T: TreeSerialize> TreeSerialize for Option<T> {
#[inline]
fn serialize_by_key<K, S>(&self, keys: K, ser: S) -> Result<usize, Error<S::Error>>
where
K: Keys,
Expand All @@ -194,6 +204,7 @@ impl<T: TreeSerialize> TreeSerialize for Option<T> {
}

impl<'de, T: TreeDeserialize<'de>> TreeDeserialize<'de> for Option<T> {
#[inline]
fn deserialize_by_key<K, D>(&mut self, keys: K, de: D) -> Result<usize, Error<D::Error>>
where
K: Keys,
Expand All @@ -206,6 +217,7 @@ impl<'de, T: TreeDeserialize<'de>> TreeDeserialize<'de> for Option<T> {
}

impl<T: TreeAny> TreeAny for Option<T> {
#[inline]
fn ref_any_by_key<K>(&self, keys: K) -> Result<&dyn Any, Traversal>
where
K: Keys,
Expand All @@ -215,6 +227,7 @@ impl<T: TreeAny> TreeAny for Option<T> {
.ref_any_by_key(keys)
}

#[inline]
fn mut_any_by_key<K>(&mut self, keys: K) -> Result<&mut dyn Any, Traversal>
where
K: Keys,
Expand Down
17 changes: 9 additions & 8 deletions miniconf/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ pub struct ExactSize<T> {
count: usize,
}

impl<T> ExactSize<T> {
// Not pub since the caller needs to ensure that the count contract holds.
fn new(iter: T, count: usize) -> Self {
Self { iter, count }
}
}

impl<T: Iterator> Iterator for ExactSize<T> {
type Item = T::Item;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(v) = self.iter.next() {
self.count -= 1; // checks for overflow in debug
Expand All @@ -29,6 +23,7 @@ impl<T: Iterator> Iterator for ExactSize<T> {
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.count, Some(self.count))
}
Expand All @@ -47,17 +42,20 @@ impl<T: Iterator> core::iter::FusedIterator for ExactSize<T> {}
/// A Keys wrapper that can always finalize()
struct Consume<T>(T);
impl<T: Keys> Keys for Consume<T> {
#[inline]
fn next(&mut self, lookup: &KeyLookup) -> Result<usize, Traversal> {
self.0.next(lookup)
}

#[inline]
fn finalize(&mut self) -> Result<(), Traversal> {
Ok(())
}
}
impl<T: Keys> IntoKeys for Consume<T> {
type IntoKeys = Self;

#[inline]
fn into_keys(self) -> Self::IntoKeys {
self
}
Expand Down Expand Up @@ -123,7 +121,10 @@ impl<M: TreeKey + ?Sized, N, const D: usize> NodeIter<M, N, D> {
"depth D = {D} must be at least {}",
meta.max_depth
);
ExactSize::new(self, meta.count)
ExactSize {
iter: self,
count: meta.count,
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions miniconf/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::{Error, IntoKeys, Path, TreeDeserialize, TreeSerialize};
///
/// # Returns
/// The number of bytes consumed from `data` or an [Error].
#[inline]
pub fn set<'de, T: TreeDeserialize<'de> + ?Sized>(
tree: &mut T,
path: &str,
Expand All @@ -51,6 +52,7 @@ pub fn set<'de, T: TreeDeserialize<'de> + ?Sized>(
///
/// # Returns
/// The number of bytes used in the `data` buffer or an [Error].
#[inline]
pub fn get<T: TreeSerialize + ?Sized>(
tree: &T,
path: &str,
Expand All @@ -63,6 +65,7 @@ pub fn get<T: TreeSerialize + ?Sized>(
///
/// # Returns
/// The number of bytes consumed from `data` or an [Error].
#[inline]
pub fn set_by_key<'de, T: TreeDeserialize<'de> + ?Sized, K: IntoKeys>(
tree: &mut T,
keys: K,
Expand All @@ -77,6 +80,7 @@ pub fn set_by_key<'de, T: TreeDeserialize<'de> + ?Sized, K: IntoKeys>(
///
/// # Returns
/// The number of bytes used in the `data` buffer or an [Error].
#[inline]
pub fn get_by_key<T: TreeSerialize + ?Sized, K: IntoKeys>(
tree: &T,
keys: K,
Expand Down
7 changes: 7 additions & 0 deletions miniconf/src/jsonpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ impl<'a, T> From<&'a T> for JsonPathIter<'a>
where
T: AsRef<str> + ?Sized,
{
#[inline]
fn from(value: &'a T) -> Self {
Self(value.as_ref())
}
}

impl<'a> From<JsonPathIter<'a>> for &'a str {
#[inline]
fn from(value: JsonPathIter<'a>) -> Self {
value.0
}
Expand Down Expand Up @@ -94,33 +96,38 @@ impl<'a> core::iter::FusedIterator for JsonPathIter<'a> {}
pub struct JsonPath<T: ?Sized>(pub T);

impl<T> From<T> for JsonPath<T> {
#[inline]
fn from(value: T) -> Self {
Self(value)
}
}

impl<T> JsonPath<T> {
/// Extract the inner value
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}

impl<T: ?Sized> Deref for JsonPath<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T: ?Sized> DerefMut for JsonPath<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<'a, T: AsRef<str> + ?Sized> IntoKeys for &'a JsonPath<T> {
type IntoKeys = KeysIter<JsonPathIter<'a>>;
#[inline]
fn into_keys(self) -> Self::IntoKeys {
JsonPathIter::from(self.0.as_ref()).into_keys()
}
Expand Down
Loading
Loading