Trait smart_config::de::DeserializeParam
source · pub trait DeserializeParam<T>:
Debug
+ Send
+ Sync
+ 'static {
const EXPECTING: BasicTypes;
// Required methods
fn deserialize_param(
&self,
ctx: DeserializeContext<'_>,
param: &'static ParamMetadata,
) -> Result<T, ErrorWithOrigin>;
fn serialize_param(&self, param: &T) -> Value;
// Provided method
fn describe(&self, description: &mut TypeDescription) { ... }
}
Expand description
Deserializes a parameter of the specified type.
§Implementations
§Basic implementations
Serde
allows deserializing any type implementingserde::Deserialize
.TimeUnit
deserializesDuration
from a numeric value that has the specified unit of measurementSizeUnit
similarly deserializesByteSize
WithUnit
deserializesDuration
s /ByteSize
s as an integer + unit of measurement (either in a string or object form).
§Decorators
Optional
decorates a deserializer forT
turning it into a deserializer forOption<T>
WithDefault
adds a default value used if the input is missingDelimited
allows deserializing arrays from a delimited string (e.g., comma-delimited)OrString
allows to switch between structured and string deserialization
Required Associated Constants§
sourceconst EXPECTING: BasicTypes
const EXPECTING: BasicTypes
Describes which parameter this deserializer is expecting.
Required Methods§
sourcefn 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.
§Errors
Returns an error if a param cannot be deserialized, e.g. if it has an incorrect type.
sourcefn serialize_param(&self, param: &T) -> Value
fn serialize_param(&self, param: &T) -> Value
Serializes the provided parameter to the JSON model.
Serialization is considered infallible (serde_json
serialization may fail on recursive or very deeply nested data types;
please don’t use such data types for config params).
Provided Methods§
sourcefn describe(&self, description: &mut TypeDescription)
fn describe(&self, description: &mut TypeDescription)
Additional info about the deserialized type, e.g., extended description.
Object Safety§
This trait is not object safe.
Implementations on Foreign Types§
source§impl<T: WellKnown> DeserializeParam<T> for ()
impl<T: WellKnown> DeserializeParam<T> for ()
const EXPECTING: BasicTypes = _
fn describe(&self, description: &mut TypeDescription)
fn deserialize_param( &self, ctx: DeserializeContext<'_>, param: &'static ParamMetadata, ) -> Result<T, ErrorWithOrigin>
fn serialize_param(&self, param: &T) -> Value
Implementors§
source§impl DeserializeParam<Option<ByteSize>> for WithUnit
impl DeserializeParam<Option<ByteSize>> for WithUnit
const EXPECTING: BasicTypes = Self::EXPECTED_TYPES
source§impl DeserializeParam<Option<Duration>> for WithUnit
impl DeserializeParam<Option<Duration>> for WithUnit
const EXPECTING: BasicTypes = Self::EXPECTED_TYPES
source§impl DeserializeParam<ByteSize> for SizeUnit
impl DeserializeParam<ByteSize> for SizeUnit
Supports deserializing a ByteSize
from a number, with self
being the unit of measurement.
§Examples
use smart_config::testing;
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(with = SizeUnit::MiB)]
size_mb: ByteSize,
}
let source = smart_config::config!("size_mb": 4);
let config = testing::test::<TestConfig>(source)?;
assert_eq!(config.size_mb, ByteSize(4 << 20));
const EXPECTING: BasicTypes = BasicTypes::INTEGER
source§impl DeserializeParam<ByteSize> for WithUnit
impl DeserializeParam<ByteSize> for WithUnit
const EXPECTING: BasicTypes = Self::EXPECTED_TYPES
source§impl DeserializeParam<Duration> for TimeUnit
impl DeserializeParam<Duration> for TimeUnit
Supports deserializing a Duration
from a number, with self
being the unit of measurement.
§Examples
use smart_config::testing;
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(with = TimeUnit::Millis)]
time_ms: Duration,
}
let source = smart_config::config!("time_ms": 100);
let config = testing::test::<TestConfig>(source)?;
assert_eq!(config.time_ms, Duration::from_millis(100));