Trait TreeSchema

Source
pub trait TreeSchema {
    const SCHEMA: &Schema;
}
Expand description

Traversal, iteration of keys in a tree.

See also the sub-traits TreeSerialize, TreeDeserialize, TreeAny.

§Keys

There is a one-to-one relationship between nodes and keys. The keys used to identify nodes support Keys/IntoKeys. They can be obtained from other IntoKeys through Schema::transcode(). An iterator of keys for the nodes is available through Schema::nodes().

  • usize is modelled after ASN.1 Object Identifiers, see crate::Indices.
  • &str keys are sequences of names, like path names. When concatenated, they are separated by some path hierarchy separator, e.g. '/', see crate::Path, or by some more complex notation, see crate::JsonPath.
  • crate::Packed is a bit-packed compact compressed notation of hierarchical compound indices.
  • See the scpi example for how to implement case-insensitive, relative, and abbreviated/partial matches.

§Derive macros

Derive macros to automatically implement the correct traits on a struct or enum are available through crate::TreeSchema, crate::TreeSerialize, crate::TreeDeserialize, and crate::TreeAny. A shorthand derive macro that derives all four trait implementations is also available at crate::Tree.

The derive macros support per-field/per-variant attributes to control the derived trait implementations.

§Rename

The key for named struct fields or enum variants may be changed from the default field ident using the rename derive macro attribute.

use miniconf::{Path, Tree, TreeSchema};
#[derive(Tree, Default)]
struct S {
    #[tree(rename = "OTHER")]
    a: f32,
};
let name = S::SCHEMA.transcode::<Path<String, '/'>>([0usize]).unwrap();
assert_eq!(name.0.as_str(), "/OTHER");

§Skip

Named fields/variants may be omitted from the derived Tree trait implementations using the skip attribute. Note that for tuple structs skipping is only supported for terminal fields:

use miniconf::{Tree};
#[derive(Tree)]
struct S(i32, #[tree(skip)] ());
use miniconf::{Tree};
#[derive(Tree)]
struct S(#[tree(skip)] (), i32);

§Type

The type to use when accessing the field/variant through TreeDeserialize::probe can be overridden using the typ derive macro attribute (#[tree(typ="[f32; 4]")]).

§Implementation overrides

#[tree(with=path)]

This overrides the calls to the child node/variant traits using pub functions and constants in the module at the given path: (SCHEMA, serialize_by_key, deserialize_by_key, probe_by_key, ref_any_by_key, mut_any_by_key).

Also use this to relax bounds and deny operations.

#[derive(Tree, Default)]
struct S {
    #[tree(with=check)]
    b: f32,
}
mod check {
    use miniconf::{SerdeError, Deserializer, TreeDeserialize, ValueError, Keys};
    pub use miniconf::leaf::{SCHEMA, serialize_by_key, probe_by_key, ref_any_by_key, mut_any_by_key};

    pub fn deserialize_by_key<'de, D: Deserializer<'de>>(
        value: &mut f32,
        keys: impl Keys,
        de: D
    ) -> Result<(), SerdeError<D::Error>> {
        let mut new = *value;
        new.deserialize_by_key(keys, de)?;
        if new < 0.0 {
            Err(ValueError::Access("fail").into())
        } else {
            *value = new;
            Ok(())
        }
    }
}

§defer

The defer attribute is a shorthand for with() that defers child trait implementations to a given expression.

§Array

Blanket implementations of the Tree* traits are provided for homogeneous arrays [T; N].

§Option

Blanket implementations of the Tree* traits are provided for Option<T>.

These implementations do not alter the path hierarchy and do not consume any items from the keys iterators. The TreeSchema behavior of an Option is such that the None variant makes the corresponding part of the tree inaccessible at run-time. It will still be iterated over (e.g. by Schema::nodes()) but attempts to access it (e.g. TreeSerialize::serialize_by_key(), TreeDeserialize::deserialize_by_key(), TreeAny::ref_any_by_key(), or TreeAny::mut_any_by_key()) return the special ValueError::Absent.

This is the same behavior as for other enums that have the Tree* traits derived.

§Tuples

Blanket impementations for the Tree* traits are provided for heterogeneous tuples (T0, T1, ...) up to length eight.

§Examples

See the crate documentation for a longer example showing how the traits and the derive macros work.

Required Associated Constants§

Source

const SCHEMA: &Schema

Schema for this tree level

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl TreeSchema for SocketAddr

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for bool

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for char

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for f32

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for f64

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for i8

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for i16

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for i32

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for i64

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for i128

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for isize

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for str

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for u8

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for u16

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for u32

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for u64

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for u128

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for ()

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for usize

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for SocketAddrV4

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for SocketAddrV6

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for Duration

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl TreeSchema for [u8]

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl<T0: TreeSchema> TreeSchema for (T0,)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema> TreeSchema for (T0, T1)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema> TreeSchema for (T0, T1, T2)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema, T3: TreeSchema> TreeSchema for (T0, T1, T2, T3)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema, T3: TreeSchema, T4: TreeSchema> TreeSchema for (T0, T1, T2, T3, T4)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema, T3: TreeSchema, T4: TreeSchema, T5: TreeSchema> TreeSchema for (T0, T1, T2, T3, T4, T5)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema, T3: TreeSchema, T4: TreeSchema, T5: TreeSchema, T6: TreeSchema> TreeSchema for (T0, T1, T2, T3, T4, T5, T6)

Source§

const SCHEMA: &'static Schema

Source§

impl<T0: TreeSchema, T1: TreeSchema, T2: TreeSchema, T3: TreeSchema, T4: TreeSchema, T5: TreeSchema, T6: TreeSchema, T7: TreeSchema> TreeSchema for (T0, T1, T2, T3, T4, T5, T6, T7)

Source§

const SCHEMA: &'static Schema

Source§

impl<T, const N: usize> TreeSchema for Vec<T, N>

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Source§

impl<T: TreeSchema + ?Sized> TreeSchema for &T

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema + ?Sized> TreeSchema for &mut T

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for Bound<T>

Source§

const SCHEMA: &'static Schema

Source§

impl<T: TreeSchema> TreeSchema for Option<T>

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for Cell<T>

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for RefCell<T>

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for Wrapping<T>

Source§

const SCHEMA: &'static Schema = T::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for Range<T>

Source§

const SCHEMA: &'static Schema

Source§

impl<T: TreeSchema> TreeSchema for RangeFrom<T>

Source§

const SCHEMA: &'static Schema

Source§

impl<T: TreeSchema> TreeSchema for RangeInclusive<T>

Source§

const SCHEMA: &'static Schema = Range<T>::SCHEMA

Source§

impl<T: TreeSchema> TreeSchema for RangeTo<T>

Source§

const SCHEMA: &'static Schema

Source§

impl<T: TreeSchema, E: TreeSchema> TreeSchema for Result<T, E>

Source§

const SCHEMA: &'static Schema

Source§

impl<T: TreeSchema, const N: usize> TreeSchema for [T; N]

Source§

const SCHEMA: &'static Schema

Source§

impl<const N: usize> TreeSchema for String<N>

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA

Implementors§

Source§

impl<T: ?Sized> TreeSchema for Leaf<T>

Source§

const SCHEMA: &'static Schema = leaf::SCHEMA