Skip to main content

OptionArgument

Trait OptionArgument 

Source
pub trait OptionArgument<T>: Sealed + Sized {
    // Required methods
    fn require_some(self, path: &str) -> ArgumentResult<T>;
    fn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
       where F: FnOnce(&T) -> ArgumentResult<()>;
    fn validate_some<F>(self, validator: F) -> ArgumentResult<Self>
       where F: FnOnce(T) -> ArgumentResult<T>;
}
Expand description

Validates optional arguments without requiring their values to be cloned.

A required value can be extracted with Self::require_some. Conditional validation borrows a present value only for the validator call and returns the original option on success.

The trait is sealed and implemented only for Option<T>.

Required Methods§

Source

fn require_some(self, path: &str) -> ArgumentResult<T>

Requires this option to contain a value.

A present value is moved out and returned without cloning. An absent value returns ArgumentErrorKind::Missing at path.

Source

fn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
where F: FnOnce(&T) -> ArgumentResult<()>,

Validates a present value by temporary borrow.

validator receives a shared reference when this option is present. A successful validator returns the original option without cloning its value; it introduces no failure kind of its own and propagates any ArgumentErrorKind returned by validator unchanged. An absent option is returned without executing validator.

Source

fn validate_some<F>(self, validator: F) -> ArgumentResult<Self>
where F: FnOnce(T) -> ArgumentResult<T>,

Validates and returns a present owned value.

validator receives ownership of the contained value when this option is present and must return the validated value. A successful validator is wrapped in Some and returned; it may transform the value without requiring Clone or Copy. An absent option is returned without executing validator. Validator errors are propagated unchanged.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> OptionArgument<T> for Option<T>

Source§

fn require_some(self, path: &str) -> ArgumentResult<T>

Extracts a present value or reports that path is missing.

The contained value is moved without cloning. An absent option returns ArgumentErrorKind::Missing.

Source§

fn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
where F: FnOnce(&T) -> ArgumentResult<()>,

Borrows and validates a present value, then returns the original option.

A validator error is returned unchanged. When this option is absent, validator is not executed.

Source§

fn validate_some<F>(self, validator: F) -> ArgumentResult<Self>
where F: FnOnce(T) -> ArgumentResult<T>,

Moves a present value through validator and rewraps it on success.

A validator error is returned unchanged. When this option is absent, validator is not executed.

Implementors§