Struct smart_config::de::NamedEntries
source · pub struct NamedEntries<K, V, DeK = <K as WellKnown>::Deserializer, DeV = <V as WellKnown>::Deserializer> { /* private fields */ }
Expand description
Deserializer that supports either a map or an array of { key: _, value: _ }
tuples (with customizable
key / value names). Created using Entries::named()
.
Unlike Entries
, NamedEntries
doesn’t require keys to be deserializable from strings (although
if they don’t, map inputs will not work).
§Examples
use std::{collections::HashMap, time::Duration};
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(with = Entries::WELL_KNOWN.named("num", "value"))]
entries: HashMap<u64, String>,
/// Can also be used with "linear" containers with tuple items.
#[config(with = Entries::WELL_KNOWN.named("method", "timeout"))]
tuples: Vec<(String, Duration)>,
}
// Parsing from maps:
let map_input = smart_config::config!(
"entries": serde_json::json!({ "2": "two", "3": "three" }),
"tuples": serde_json::json!({ "getLogs": "2s" }),
);
let config: TestConfig = testing::test(map_input)?;
assert_eq!(
config.entries,
HashMap::from([(2, "two".to_owned()), (3, "three".to_owned())])
);
assert_eq!(
config.tuples,
[("getLogs".to_owned(), Duration::from_secs(2))]
);
// The equivalent input as named tuples:
let tuples_input = smart_config::config!(
"entries": serde_json::json!([
{ "num": 2, "value": "two" },
{ "num": 3, "value": "three" },
]),
"tuples": serde_json::json!([
{ "method": "getLogs", "timeout": "2s" },
]),
);
let config: TestConfig = testing::test(tuples_input)?;
Trait Implementations§
source§impl<K, V, DeK, DeV, C> DeserializeParam<C> for NamedEntries<K, V, DeK, DeV>where
K: 'static,
V: 'static,
DeK: DeserializeParam<K>,
DeV: DeserializeParam<V>,
C: FromIterator<(K, V)> + ToEntries<K, V>,
impl<K, V, DeK, DeV, C> DeserializeParam<C> for NamedEntries<K, V, DeK, DeV>where
K: 'static,
V: 'static,
DeK: DeserializeParam<K>,
DeV: DeserializeParam<V>,
C: FromIterator<(K, V)> + ToEntries<K, V>,
source§const EXPECTING: BasicTypes = _
const EXPECTING: BasicTypes = _
Describes which parameter this deserializer is expecting.
source§fn describe(&self, description: &mut TypeDescription)
fn describe(&self, description: &mut TypeDescription)
Additional info about the deserialized type, e.g., extended description.
source§fn deserialize_param(
&self,
ctx: DeserializeContext<'_>,
param: &'static ParamMetadata,
) -> Result<C, ErrorWithOrigin>
fn deserialize_param( &self, ctx: DeserializeContext<'_>, param: &'static ParamMetadata, ) -> Result<C, ErrorWithOrigin>
Performs deserialization given the context and param metadata. Read more
Auto Trait Implementations§
impl<K, V, DeK, DeV> Freeze for NamedEntries<K, V, DeK, DeV>
impl<K, V, DeK, DeV> RefUnwindSafe for NamedEntries<K, V, DeK, DeV>where
DeK: RefUnwindSafe,
DeV: RefUnwindSafe,
impl<K, V, DeK, DeV> Send for NamedEntries<K, V, DeK, DeV>
impl<K, V, DeK, DeV> Sync for NamedEntries<K, V, DeK, DeV>
impl<K, V, DeK, DeV> Unpin for NamedEntries<K, V, DeK, DeV>
impl<K, V, DeK, DeV> UnwindSafe for NamedEntries<K, V, DeK, DeV>where
DeK: UnwindSafe,
DeV: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more