pub struct Delimited<De = (), S = &'static str>(pub De, pub S);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, pat::lazy_regex, testing, DescribeConfig, DeserializeConfig};
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(default, with = de::Delimited::new(","))]
strings: Vec<String>,
// More complex types are supported as well (along with custom base deserializers).
// Importantly, the base deserializer still refers to the entire collection, not its items,
// so you should use something like `Repeated`, or use the `repeat()` constructor.
#[config(with = de::Delimited::repeat(de::Serde![str], ":"))]
paths: Vec<PathBuf>,
// ...and more complex collections (here together with string -> number coercion
// and a regex-based splitter)
#[config(with = de::Delimited::new(lazy_regex!(ref r"\s*;\s*")))]
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::new(","))]
test: u64,
}Tuple Fields§
§0: De§1: SImplementations§
Trait Implementations§
Source§impl<T, De, S> DeserializeParam<T> for Delimited<De, S>where
De: DeserializeParam<T>,
S: Split,
impl<T, De, S> DeserializeParam<T> for Delimited<De, S>where
De: DeserializeParam<T>,
S: Split,
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<De, S> Freeze for Delimited<De, S>
impl<De, S> RefUnwindSafe for Delimited<De, S>where
De: RefUnwindSafe,
S: RefUnwindSafe,
impl<De, S> Send for Delimited<De, S>
impl<De, S> Sync for Delimited<De, S>
impl<De, S> Unpin for Delimited<De, S>
impl<De, S> UnwindSafe for Delimited<De, S>where
De: UnwindSafe,
S: 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