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>.
§Examples
use qubit_argument::OptionArgument;
let value = Some(42).validate_some(|value| Ok(value + 1));
assert_eq!(value.unwrap(), Some(43));Required Methods§
Sourcefn require_some(self, path: &str) -> ArgumentResult<T>
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.
§Parameters
path: The argument path reported when the option is absent.
§Returns
The contained value, or an error identifying the missing argument.
Sourcefn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
fn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
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.
§Parameters
validator: A callback that checks a borrowed present value.
§Returns
The original option on success, or the validator’s error unchanged.
Sourcefn validate_some<F>(self, validator: F) -> ArgumentResult<Self>where
F: FnOnce(T) -> ArgumentResult<T>,
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.
§Parameters
validator: A callback that consumes and returns the present value.
§Returns
The validated value wrapped in Some, None for an absent option,
or the validator’s error 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>
impl<T> OptionArgument<T> for Option<T>
Source§fn require_some(self, path: &str) -> ArgumentResult<T>
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>
fn validate_if_some<F>(self, validator: F) -> ArgumentResult<Self>
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>,
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.