Enum shadow_rs::core::configuration::NullableOption

source ·
pub enum NullableOption<T> {
    Value(T),
    Null,
}
Expand description

This wrapper type allows cli options to specify “null” to overwrite a config file option with None, and is intended to be used for options where “null” is a valid option value.

Warning: This may result in unexpected behaviour when wrapping string types. For example, if this is used for a file path option, the value “null” will conflict with the valid filename “null”. So if the user specifies “null” for this option, Shadow will assume it means “no value” rather than the filename “null”.

§Motivation

For configuration options, there are generally three states:

  • set
  • not set
  • null

For serde, all three states are configurable:

  • set: runahead: 5ms
  • not set: (no runahead option used in yaml)
  • null: runahead: null

For clap, there are only two states:

  • set: --runahead 5ms
  • not set: (no --runahead option used in command)

There is no way to set a “null” state for cli options with clap.

§Configuration in Shadow

Shadow first parses the config file and cli options separately before merging them.

Parsing for serde:

  • set: runahead: 5ms => runahead is set to Some(5ms)
  • not set: (no runahead option used in yaml) => runahead is set to its default (either Some(..) or None)
  • null: runahead: null => runahead is set to None

Parsing for clap:

  • set: --runahead 5ms => runahead is set to Some(5ms)
  • not set: (no --runahead option used in command) => runahead is set to None

Then the options are merged such that any Some(..) options from the cli options will overwrite any Some or None options from the config file.

The issue is that no clap option can overwrite a config file option of Some with a value of None. For example if the config file specifies runahead: 5ms, then with clap you can only use --runahead 2ms to change the runahead to a Some(2ms) value, or you can not set --runahead at all to leave it as a Some(5ms) value. But there is no cli option to change the runahead to a None value.

This NullableOption type is a wrapper to allow you to specify “null” on the command line to overwrite the config file value with None. From the example above, you could now specify “–runahead null” to overwrite the config file value (for example Some(5ms)) with a None value.

Variants§

§

Value(T)

§

Null

Implementations§

source§

impl<T> NullableOption<T>

source

pub fn as_ref(&self) -> NullableOption<&T>

source

pub fn as_mut(&mut self) -> NullableOption<&mut T>

source

pub fn to_option(self) -> Option<T>

Easier to use than Into<Option<T>> since Option has a lot of blanket From implementations, requiring a lot of type annotations.

Trait Implementations§

source§

impl<T: Clone> Clone for NullableOption<T>

source§

fn clone(&self) -> NullableOption<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for NullableOption<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, T: Deserialize<'de>> Deserialize<'de> for NullableOption<T>

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> FromStr for NullableOption<T>
where T: FromStr<Err: Debug + Display>,

source§

type Err = <T as FromStr>::Err

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<T: JsonSchema> JsonSchema for NullableOption<T>

source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl<T: PartialEq> PartialEq for NullableOption<T>

source§

fn eq(&self, other: &NullableOption<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Serialize> Serialize for NullableOption<T>

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Copy> Copy for NullableOption<T>

source§

impl<T: Eq> Eq for NullableOption<T>

source§

impl<T> StructuralPartialEq for NullableOption<T>

Auto Trait Implementations§

§

impl<T> Freeze for NullableOption<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for NullableOption<T>
where T: RefUnwindSafe,

§

impl<T> Send for NullableOption<T>
where T: Send,

§

impl<T> Sync for NullableOption<T>
where T: Sync,

§

impl<T> Unpin for NullableOption<T>
where T: Unpin,

§

impl<T> UnwindSafe for NullableOption<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> NoTypeInference for T

source§

type This = T

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> Host for T
where T: Debug + Send,

source§

impl<T> Host for T
where T: Debug + Send + 'static,

source§

impl<T> Host for T
where T: Host + Host,