Trait 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.

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

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