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
Serdeallows deserializing any type implementingserde::Deserialize.TimeUnitdeserializesDurationfrom a numeric value that has the specified unit of measurementSizeUnitsimilarly deserializesByteSizeWithUnitdeserializesDurations /ByteSizes as an integer + unit of measurement (either in a string or object form).
§Decorators
Optionaldecorates a deserializer forTturning it into a deserializer forOption<T>WithDefaultadds a default value used if the input is missingDelimitedallows deserializing arrays from a delimited string (e.g., comma-delimited)OrStringallows 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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so 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<EtherAmount>> for WithUnit
impl DeserializeParam<Option<EtherAmount>> 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
Supports deserializing a ByteSize from a number, with self being the unit of measurement.
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<EtherAmount> for WithUnit
impl DeserializeParam<EtherAmount> for WithUnit
const EXPECTING: BasicTypes = Self::EXPECTED_TYPES
Source§impl DeserializeParam<Duration> for TimeUnit
Supports deserializing a Duration from a number, with self being the unit of measurement.
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));