1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//! `TreeSerialize`/`TreeDeserialize` with "JSON and `/`".
//!
//! Access items with `'/'` as path separator and JSON (from `serde-json-core`)
//! as serialization/deserialization payload format.
//!
//! Paths used here are reciprocal to `TreeKey::lookup::<Path<_, '/'>, _>(...)`/
//! `TreeKey::nodes::<Path<_, '/'>>()`.
//!
//! ```
//! use miniconf::{json, Leaf, Tree};
//! #[derive(Tree, Default)]
//! struct S {
//! foo: Leaf<u32>,
//! bar: [Leaf<u16>; 2],
//! };
//! let mut s = S::default();
//! json::set(&mut s, "/bar/1", b"9").unwrap();
//! assert_eq!(*s.bar[1], 9);
//! let mut buf = [0u8; 10];
//! let len = json::get(&mut s, "/bar/1", &mut buf[..]).unwrap();
//! assert_eq!(&buf[..len], b"9");
//! ```
use serde_json_core::{de, ser};
use crate::{Error, IntoKeys, Path, TreeDeserialize, TreeSerialize};
/// Update a node by path.
///
/// # Args
/// * `tree` - The `TreeDeserialize` to operate on.
/// * `path` - The path to the node. Everything before the first `'/'` is ignored.
/// * `data` - The serialized data making up the content.
///
/// # 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,
data: &'de [u8],
) -> Result<usize, Error<de::Error>> {
set_by_key(tree, Path::<_, '/'>::from(path), data)
}
/// Retrieve a serialized value by path.
///
/// # Args
/// * `tree` - The `TreeDeserialize` to operate on.
/// * `path` - The path to the node. Everything before the first `'/'` is ignored.
/// * `data` - The buffer to serialize the data into.
///
/// # Returns
/// The number of bytes used in the `data` buffer or an [Error].
#[inline]
pub fn get<T: TreeSerialize + ?Sized>(
tree: &T,
path: &str,
data: &mut [u8],
) -> Result<usize, Error<ser::Error>> {
get_by_key(tree, Path::<_, '/'>::from(path), data)
}
/// Update a node by key.
///
/// # 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,
data: &'de [u8],
) -> Result<usize, Error<de::Error>> {
let mut de = de::Deserializer::new(data, None);
tree.deserialize_by_key(keys.into_keys(), &mut de)?;
de.end().map_err(Error::Finalization)
}
/// Retrieve a serialized value by key.
///
/// # 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,
data: &mut [u8],
) -> Result<usize, Error<ser::Error>> {
let mut ser = ser::Serializer::new(data);
tree.serialize_by_key(keys.into_keys(), &mut ser)?;
Ok(ser.end())
}