Skip to content

Commit

Permalink
Merge pull request #153 from quartiq/rj/separator-length
Browse files Browse the repository at this point in the history
rj/separator length
  • Loading branch information
ryan-summers authored Jul 25, 2023
2 parents c8ba5d2 + 5d7634c commit 471b662
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
doesn't enforce `serde-json-core` or `u8` buffers or `/` as the path hierarchy
separator anymore.
* [breaking] `MiniconfIter` takes the path hierarchy separator from `SerDe` and passes it on to
`Miniconf::next_path`.
`Miniconf::next_path` and `Miniconf::metadata`.
* [breaking] The `Miniconf` trait has been stripped of the provided functions that depended
on the `serde`-backend and path hierarchy separator. Those have been
moved into a super trait `SerDe<S>` that is generic over a specification marker
struct `S`. `SerDe<JsonCoreSlash>` has been implemented for all `Miniconf`
to provide the previously existing functionality.
* The only required change for most downstream crates to adapt to the above is to
make sure the `SerDe` trait is in scope (`use miniconf::SerDe`).
* Paths now start with the path separator (unless they are empty).
* Paths now start with the path separator (unless they are empty). This affects the `miniconf_derive`
crate and both `Miniconf` implementation pairs for `Option`/`Array`.
* The path iterator does not need to be `Peekable` anymore.

## [0.7.1] (https://github.com/quartiq/miniconf/compare/v0.7.0...v0.7.1)
Expand Down
10 changes: 4 additions & 6 deletions miniconf_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ fn metadata_arm((i, struct_field): (usize, &StructField)) -> proc_macro2::TokenS
if struct_field.deferred {
quote! {
#i => {
let mut meta = <#field_type>::metadata();
// Length of separator and field name
meta.max_length += 1 + stringify!(#field_name).len();
let mut meta = <#field_type>::metadata(separator_length);
meta.max_length += separator_length + stringify!(#field_name).len();
meta.max_depth += 1;
meta
}
Expand All @@ -129,8 +128,7 @@ fn metadata_arm((i, struct_field): (usize, &StructField)) -> proc_macro2::TokenS
quote! {
#i => {
let mut meta = miniconf::Metadata::default();
// Length of separator and field name
meta.max_length = 1 + stringify!(#field_name).len();
meta.max_length = separator_length + stringify!(#field_name).len();
meta.max_depth = 1;
meta.count = 1;
meta
Expand Down Expand Up @@ -205,7 +203,7 @@ fn derive_struct(
}
}

fn metadata() -> miniconf::Metadata {
fn metadata(separator_length: usize) -> miniconf::Metadata {
let mut meta = miniconf::Metadata::default();

for index in 0.. {
Expand Down
10 changes: 5 additions & 5 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ impl<T: Miniconf, const N: usize> Miniconf for Array<T, N> {
.get_path(path_parts, ser)
}

fn metadata() -> Metadata {
let mut meta = T::metadata();
fn metadata(separator_length: usize) -> Metadata {
let mut meta = T::metadata(separator_length);

// We add separator and index
meta.max_length += 1 + digits(N);
meta.max_length += separator_length + digits(N);
meta.max_depth += 1;
meta.count *= N;

Expand Down Expand Up @@ -212,10 +212,10 @@ impl<T: crate::Serialize + crate::DeserializeOwned, const N: usize> Miniconf for
serde::Serialize::serialize(item, ser).map_err(|_| Error::Serialization)
}

fn metadata() -> Metadata {
fn metadata(separator_length: usize) -> Metadata {
Metadata {
// We add separator and index
max_length: 1 + digits(N),
max_length: separator_length + digits(N),
max_depth: 1,
count: N,
}
Expand Down
6 changes: 4 additions & 2 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ impl<M: ?Sized, const L: usize, const TS: usize, S> Default for MiniconfIter<M,
}
}

impl<M: ?Sized + Miniconf, const L: usize, const TS: usize, S> MiniconfIter<M, L, TS, S> {
impl<M: ?Sized + Miniconf + SerDe<S>, const L: usize, const TS: usize, S>
MiniconfIter<M, L, TS, S>
{
pub fn metadata() -> Result<Metadata, IterError> {
let meta = M::metadata();
let meta = M::metadata(M::SEPARATOR.len_utf8());
if TS < meta.max_length {
return Err(IterError::Length);
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ pub trait Miniconf {
) -> Result<usize, IterError>;

/// Get metadata about the paths in the namespace.
fn metadata() -> Metadata;
///
/// # Args
/// * `separator_length` - The path hierarchy separator length in bytes.
fn metadata(separator_length: usize) -> Metadata;
}

/// Trait for implementing a specific way of serialization/deserialization into/from a slice
Expand Down
6 changes: 3 additions & 3 deletions src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl<T: Miniconf> Miniconf for Option<T> {
}
}

fn metadata() -> Metadata {
T::metadata()
fn metadata(separator_length: usize) -> Metadata {
T::metadata(separator_length)
}

fn next_path(
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<T: crate::Serialize + crate::DeserializeOwned> Miniconf for core::option::O
serde::Serialize::serialize(data, ser).map_err(|_| Error::Serialization)
}

fn metadata() -> Metadata {
fn metadata(_separator_length: usize) -> Metadata {
Metadata {
count: 1,
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions tests/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn simple_array_indexing() {
));

// Test metadata
let metadata = S::metadata();
let metadata = S::metadata(1);
assert_eq!(metadata.max_depth, 2);
assert_eq!(metadata.max_length, "/a/2".len());
assert_eq!(metadata.count, 3);
Expand Down Expand Up @@ -122,7 +122,7 @@ fn array_of_structs_indexing() {
assert_eq!(expected, s);

// Test metadata
let metadata = S::metadata();
let metadata = S::metadata(1);
assert_eq!(metadata.max_depth, 3);
assert_eq!(metadata.max_length, "/a/2/b".len());
assert_eq!(metadata.count, 3);
Expand Down Expand Up @@ -179,7 +179,7 @@ fn short_array() {
}

// Test metadata
let meta = S::metadata();
let meta = S::metadata(1);
assert_eq!(meta.max_depth, 2);
assert_eq!(meta.max_length, "/data/0".len());
assert_eq!(meta.count, 1);
Expand Down
2 changes: 1 addition & 1 deletion tests/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn simple_enum() {
assert_eq!(s.v, Variant::B);

// Test metadata
let metadata = S::metadata();
let metadata = S::metadata(1);
assert_eq!(metadata.max_depth, 1);
assert_eq!(metadata.max_length, "/v".len());
assert_eq!(metadata.count, 1);
Expand Down
8 changes: 4 additions & 4 deletions tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn generic_type() {
assert_eq!(settings.data, 3.0);

// Test metadata
let metadata = Settings::<f32>::metadata();
let metadata = Settings::<f32>::metadata(1);
assert_eq!(metadata.max_depth, 1);
assert_eq!(metadata.max_length, "/data".len());
assert_eq!(metadata.count, 1);
Expand All @@ -33,7 +33,7 @@ fn generic_array() {
assert_eq!(settings.data[0], 3.0);

// Test metadata
let metadata = Settings::<f32>::metadata();
let metadata = Settings::<f32>::metadata(1);
assert_eq!(metadata.max_depth, 2);
assert_eq!(metadata.max_length, "/data/0".len());
assert_eq!(metadata.count, 2);
Expand All @@ -57,7 +57,7 @@ fn generic_struct() {
assert_eq!(settings.inner.data, 3.0);

// Test metadata
let metadata = Settings::<Inner>::metadata();
let metadata = Settings::<Inner>::metadata(1);
assert_eq!(metadata.max_depth, 1);
assert_eq!(metadata.max_length, "/inner".len());
assert_eq!(metadata.count, 1);
Expand All @@ -83,7 +83,7 @@ fn generic_atomic() {
assert_eq!(settings.atomic.inner[0], 3.0);

// Test metadata
let metadata = Settings::<f32>::metadata();
let metadata = Settings::<f32>::metadata(1);
assert_eq!(metadata.max_depth, 1);
assert_eq!(metadata.max_length, "/atomic".len());
}
2 changes: 1 addition & 1 deletion tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Settings {

#[test]
fn insufficient_space() {
let meta = Settings::metadata();
let meta = Settings::metadata(1);
assert_eq!(meta.max_depth, 2);
assert_eq!(meta.max_length, "/c/inner".len());
assert_eq!(meta.count, 3);
Expand Down
4 changes: 2 additions & 2 deletions tests/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn atomic_struct() {
assert_eq!(settings, expected);

// Check that metadata is correct.
let metadata = Settings::metadata();
let metadata = Settings::metadata(1);
assert_eq!(metadata.max_depth, 1);
assert_eq!(metadata.max_length, "/c".len());
assert_eq!(metadata.count, 3);
Expand Down Expand Up @@ -70,7 +70,7 @@ fn recursive_struct() {
assert!(settings.set("/c", b"{\"a\": 5}").is_err());

// Check that metadata is correct.
let metadata = Settings::metadata();
let metadata = Settings::metadata(1);
assert_eq!(metadata.max_depth, 2);
assert_eq!(metadata.max_length, "/c/a".len());
assert_eq!(metadata.count, 3);
Expand Down

0 comments on commit 471b662

Please sign in to comment.