Struct smart_config::de::Delimited
source · pub struct Delimited(pub &'static str);
Expand description
Deserializer that supports either an array of values, or a string in which values are delimited by the specified separator.
§Examples
use std::{collections::HashSet, path::PathBuf};
use smart_config::{de, testing, DescribeConfig, DeserializeConfig};
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(default, with = de::Delimited(","))]
strings: Vec<String>,
// More complex types are supported as well
#[config(with = de::Delimited(":"))]
paths: Vec<PathBuf>,
// ...and more complex collections (here together with string -> number coercion)
#[config(with = de::Delimited(";"))]
ints: HashSet<u64>,
}
let sample = smart_config::config!(
"strings": ["test", "string"], // standard array value is still supported
"paths": "/usr/bin:/usr/local/bin",
"ints": "12;34;12",
);
let config: TestConfig = testing::test(sample)?;
assert_eq!(config.strings.len(), 2);
assert_eq!(config.strings[0], "test");
assert_eq!(config.paths.len(), 2);
assert_eq!(config.paths[1].as_os_str(), "/usr/local/bin");
assert_eq!(config.ints, HashSet::from([12, 34]));
The wrapping logic is smart enough to catch in compile time an attempt to apply Delimited
to a type
that cannot be deserialized from an array:
ⓘ
use smart_config::{de, DescribeConfig, DeserializeConfig};
#[derive(DescribeConfig, DeserializeConfig)]
struct Fail {
// will fail with "evaluation of `<Delimited as DeserializeParam<u64>>::EXPECTING` failed"
#[config(default, with = de::Delimited(","))]
test: u64,
}
Tuple Fields§
§0: &'static str
Trait Implementations§
source§impl<T: DeserializeOwned + WellKnown> DeserializeParam<T> for Delimited
impl<T: DeserializeOwned + WellKnown> DeserializeParam<T> for Delimited
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<T, ErrorWithOrigin>
fn deserialize_param( &self, ctx: DeserializeContext<'_>, param: &'static ParamMetadata, ) -> Result<T, ErrorWithOrigin>
Performs deserialization given the context and param metadata. Read more
Auto Trait Implementations§
impl Freeze for Delimited
impl RefUnwindSafe for Delimited
impl Send for Delimited
impl Sync for Delimited
impl Unpin for Delimited
impl UnwindSafe for Delimited
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