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 implementing serde::Deserialize.
  • TimeUnit deserializes Duration from a numeric value that has the specified unit of measurement
  • SizeUnit similarly deserializes ByteSize
  • WithUnit deserializes Durations / ByteSizes as an integer + unit of measurement (either in a string or object form).

§Decorators

  • Optional decorates a deserializer for T turning it into a deserializer for Option<T>
  • WithDefault adds a default value used if the input is missing
  • Delimited allows deserializing arrays from a delimited string (e.g., comma-delimited)
  • OrString allows to switch between structured and string deserialization

Required Associated Constants§

source

const EXPECTING: BasicTypes

Describes which parameter this deserializer is expecting.

Required Methods§

source

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.

source

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§

source

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 ()

source§

const EXPECTING: BasicTypes = _

source§

fn describe(&self, description: &mut TypeDescription)

source§

fn deserialize_param( &self, ctx: DeserializeContext<'_>, param: &'static ParamMetadata, ) -> Result<T, ErrorWithOrigin>

source§

fn serialize_param(&self, param: &T) -> Value

Implementors§

source§

impl DeserializeParam<Option<ByteSize>> for WithUnit

source§

const EXPECTING: BasicTypes = Self::EXPECTED_TYPES

source§

impl DeserializeParam<Option<Duration>> for WithUnit

source§

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.

§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));
source§

const EXPECTING: BasicTypes = BasicTypes::INTEGER

source§

impl DeserializeParam<ByteSize> for WithUnit

source§

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.

§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));
source§

const EXPECTING: BasicTypes = BasicTypes::INTEGER

source§

impl DeserializeParam<Duration> for WithUnit

source§

const EXPECTING: BasicTypes = Self::EXPECTED_TYPES

source§

impl<K, V, C, DeK, DeV> DeserializeParam<C> for Entries<K, V, DeK, DeV>
where K: 'static, V: 'static, DeK: DeserializeParam<K>, DeV: DeserializeParam<V>, C: FromIterator<(K, V)> + ToEntries<K, V>,

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>,

source§

impl<T, De> DeserializeParam<Option<T>> for Optional<De, true>
where De: DeserializeParam<Option<T>>,

source§

const EXPECTING: BasicTypes = De::EXPECTING

source§

impl<T, De> DeserializeParam<BTreeSet<T>> for Repeated<De>
where T: 'static + Eq + Ord, De: DeserializeParam<T>,

source§

const EXPECTING: BasicTypes = BasicTypes::ARRAY

source§

impl<T, De> DeserializeParam<T> for OrString<De>
where T: FromStr, T::Err: Display, De: DeserializeParam<T>,

source§

impl<T, De> DeserializeParam<T> for Qualified<De>
where De: DeserializeParam<T>,

source§

impl<T, De> DeserializeParam<T> for Secret<De>
where De: DeserializeParam<T>,

source§

impl<T, De: DeserializeParam<T>> DeserializeParam<Option<T>> for Optional<De>

source§

const EXPECTING: BasicTypes = De::EXPECTING

source§

impl<T, S, De> DeserializeParam<HashSet<T, S>> for Repeated<De>
where T: 'static + Eq + Hash, S: 'static + Default + BuildHasher, De: DeserializeParam<T>,

source§

const EXPECTING: BasicTypes = BasicTypes::ARRAY

source§

impl<T: 'static, De> DeserializeParam<Vec<T>> for Repeated<De>
where De: DeserializeParam<T>,

source§

const EXPECTING: BasicTypes = BasicTypes::ARRAY

source§

impl<T: 'static, De, const N: usize> DeserializeParam<[T; N]> for Repeated<De>
where De: DeserializeParam<T>,

source§

const EXPECTING: BasicTypes = BasicTypes::ARRAY

source§

impl<T: 'static, De: DeserializeParam<T>> DeserializeParam<T> for WithDefault<T, De>

source§

const EXPECTING: BasicTypes = De::EXPECTING

source§

impl<T: From<SecretString> + ExposeSecret<str>> DeserializeParam<T> for FromSecretString

source§

const EXPECTING: BasicTypes = BasicTypes::STRING

source§

impl<T: DeserializeOwned + WellKnown> DeserializeParam<T> for Delimited

source§

impl<T: Serialize + DeserializeOwned, const EXPECTING: u8> DeserializeParam<T> for Serde<EXPECTING>