pub enum Validated<T, E> {
Valid(T),
Invalid(E),
}
Expand description
Validated
is a type that represents either a Valid
value or an error(Invalid
).
See the module-level documentation for more details.
Variants§
Implementations§
source§impl<T, E> Validated<T, E>
impl<T, E> Validated<T, E>
sourcepub const fn is_invalid(&self) -> bool
pub const fn is_invalid(&self) -> bool
sourcepub fn valid(self) -> Option<T>
pub fn valid(self) -> Option<T>
Converts from Validated<T, E>
to Option<T>
.
Converts self
into an Option<T>
, consuming self
, and discarding the error, if any.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(1);
assert_eq!(x.valid(), Some(1));
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.valid(), None);
sourcepub fn invalid(self) -> Option<E>
pub fn invalid(self) -> Option<E>
Converts from Validated<T, E>
to Option<E>
.
Converts self
into an Option<E>
, consuming self
, and discarding the value, if any.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(1);
assert_eq!(x.invalid(), None);
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.invalid(), Some("error"));
sourcepub fn into_result(self) -> Result<T, E>
pub fn into_result(self) -> Result<T, E>
Converts from Validated<T, E>
to Result<T, E>
.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(1);
assert_eq!(x.into_result(), Ok(1));
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.into_result(), Err("error"));
sourcepub const fn as_ref(&self) -> Validated<&T, &E>
pub const fn as_ref(&self) -> Validated<&T, &E>
Converts from &Validated<T, E>
to Validated<&T, &E>
.
Produces a new Validated
, containing a reference
into the original, leaving the original in place.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(1);
assert_eq!(x.as_ref(), Valid(&1));
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.as_ref(), Invalid(&"error"));
sourcepub fn as_mut(&mut self) -> Validated<&mut T, &mut E>
pub fn as_mut(&mut self) -> Validated<&mut T, &mut E>
Converts from &mut Validated<T, E>
to Validated<&mut T, &mut E>
.
Examples
use rust2fun::prelude::*;
fn mutate(x: &mut Validated<i32, i32>) {
match x.as_mut() {
Valid(x) => *x = 1,
Invalid(x) => *x = 2,
}
}
let mut x = Valid(-1);
mutate(&mut x);
assert_eq!(x, Valid(1));
let mut x = Invalid(-2);
mutate(&mut x);
assert_eq!(x, Invalid(2));
sourcepub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Validated<U, E>
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Validated<U, E>
Maps a Validated<T, E>
to Validated<U, E>
by applying a function to a
contained Valid
value, leaving an Invalid
value untouched.
This function can be used to compose the results of two functions.
Examples
use rust2fun::prelude::*;
fn square(x: i32) -> i32 { x * x }
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.map(square), Valid(4));
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.map(square), Invalid("error"));
sourcepub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U
Returns the provided default (if Invalid
), or applies a function to the
contained value (if Valid
).
Arguments passed to map_or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use map_or_else
, which is
lazily evaluated.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.map_or(0, |v| v * 2), 4);
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.map_or(0, |v| v * 2), 0);
sourcepub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(
self,
default: D,
f: F
) -> U
pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>( self, default: D, f: F ) -> U
Maps a Validated<T, E>
to U
by applying fallback function default
to
a contained Invalid
value, or a function f
to a contained Valid
value.
This function can be used to unpack a successful result while handling an error.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.map_or_else(|e| e.len() as i32, |v| v * 2), 4);
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.map_or_else(|e| e.len() as i32, |v| v * 2), 5);
sourcepub fn map_err<U, F: FnOnce(E) -> U>(self, f: F) -> Validated<T, U>
pub fn map_err<U, F: FnOnce(E) -> U>(self, f: F) -> Validated<T, U>
Maps a Validated<T, E>
to Validated<T, U>
by applying a function to a
contained Invalid
value, leaving a Valid
value untouched.
This function can be used to pass through a successful result while handling an error.
Examples
use rust2fun::prelude::*;
fn stringify(x: i32) -> String { format!("error code: {x}") }
let x: Validated<i32, i32> = Valid(2);
assert_eq!(x.map_err(stringify), Valid(2));
let x: Validated<i32, i32> = Invalid(13);
assert_eq!(x.map_err(stringify), Invalid("error code: 13".to_string()));
sourcepub fn as_deref(&self) -> Validated<&T::Target, &E>where
T: Deref,
pub fn as_deref(&self) -> Validated<&T::Target, &E>where T: Deref,
Converts from Validated<T, E>
(or &Validated<T, E>
) to
Validated<&<T as Deref>::Target, &E>
.
Coerces the Valid
variant of a Validated
via Deref
and returns the new Validated
.
Examples
use rust2fun::prelude::*;
let x: Validated<String, u32> = Valid("hello".to_string());
let y: Validated<&str, &u32> = Valid("hello");
assert_eq!(x.as_deref(), y);
let x: Validated<String, u32> = Invalid(42);
let y: Validated<&str, &u32> = Invalid(&42);
assert_eq!(x.as_deref(), y);
sourcepub fn as_deref_mut(&mut self) -> Validated<&mut T::Target, &mut E>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> Validated<&mut T::Target, &mut E>where T: DerefMut,
Converts from Validated<T, E>
(or &mut Validated<T, E>
) to
Validated<&mut <T as Deref>::Target, &mut E>
.
Coerces the Valid
variant of a Validated
via DerefMut
and returns the new Validated
.
Examples
use rust2fun::prelude::*;
let mut s = "HELLO".to_string();
let mut x: Validated<String, u32> = Valid("hello".to_string());
let y: Validated<&mut str, &mut u32> = Valid(&mut s);
assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
let mut i = 42;
let mut x: Validated<String, u32> = Invalid(42);
let y: Validated<&mut str, &mut u32> = Invalid(&mut i);
assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
sourcepub fn expect(self, msg: &str) -> Twhere
E: Debug,
pub fn expect(self, msg: &str) -> Twhere E: Debug,
Returns the contained Valid
value, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the Invalid
case explicitly, or call unwrap_or
, unwrap_or_else
, or
unwrap_or_default
.
Panics
Panics if the value is an Invalid
with a panic message provided by
the passed message, and the content of the Invalid
.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Invalid("emergency failure");
x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
sourcepub fn unwrap(self) -> Twhere
E: Debug,
pub fn unwrap(self) -> Twhere E: Debug,
Returns the contained Valid
value, consuming the self
value.
Because this function may panic, its use is generally discouraged.
Instead, prefer to use pattern matching and handle the Invalid
case explicitly, or call unwrap_or
, unwrap_or_else
, or
unwrap_or_default
.
Panics
Panics if the value is an Invalid
with a panic message provided by
the content of the Invalid
.
Examples
Basic usage:
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.unwrap(), 2);
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Invalid("emergency failure");
x.unwrap(); // panics with `emergency failure`
sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere T: Default,
Returns the contained Valid
value, consuming the self
value.
Consumes the self
argument then, if Valid
, returns the contained
value, otherwise if Invalid
, returns default
value for T
.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.unwrap_or_default(), 2);
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.unwrap_or_default(), 0);
sourcepub fn expect_err(self, msg: &str) -> Ewhere
T: Debug,
pub fn expect_err(self, msg: &str) -> Ewhere T: Debug,
Returns the contained Invalid
value, consuming the self
value.
Panics
Panics if the value is an Valid
with a panic message provided by
the passed message, and the content of the Valid
.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 2`
sourcepub fn unwrap_err(self) -> Ewhere
T: Debug,
pub fn unwrap_err(self) -> Ewhere T: Debug,
Returns the contained Invalid
value, consuming the self
value.
Panics
Panics if the value is an Valid
with a panic message provided by
the content of the Valid
.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
x.unwrap_err(); // panics with `2`
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.unwrap_err(), "error");
sourcepub fn and<U>(self, other: Validated<U, E>) -> Validated<U, E>
pub fn and<U>(self, other: Validated<U, E>) -> Validated<U, E>
Returns other
if the result is Valid
, otherwise returns the Invalid
value of self
.
Arguments passed to and
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use and_then
, which is
lazily evaluated.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
let y: Validated<&str, &str> = Invalid("late error");
assert_eq!(x.and(y), Invalid("late error"));
let x: Validated<i32, &str> = Invalid("early error");
let y: Validated<&str, &str> = Valid("foo");
assert_eq!(x.and(y), Invalid("early error"));
let x: Validated<i32, &str> = Invalid("not a 2");
let y: Validated<&str, &str> = Invalid("late error");
assert_eq!(x.and(y), Invalid("not a 2"));
let x: Validated<i32, &str> = Valid(2);
let y: Validated<&str, &str> = Valid("different result type");
assert_eq!(x.and(y), Valid("different result type"));
sourcepub fn and_then<U, F: FnOnce(T) -> Validated<U, E>>(
self,
f: F
) -> Validated<U, E>
pub fn and_then<U, F: FnOnce(T) -> Validated<U, E>>( self, f: F ) -> Validated<U, E>
Calls f
if the result is Valid
, otherwise returns the Invalid
value of self
.
This allows “chained” validation: the output of one validation can be fed into another validation function.
This function has a “fail-fast” behaviour, meaning that it will stop
execution on the first Invalid
value. This makes it inconsistent with
Apply::ap
or other similar Apply
-based functions.
Examples
use rust2fun::prelude::*;
fn sq_then_to_string(x: u32) -> Validated<String, &'static str> {
x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed").into()
}
assert_eq!(Valid(2).and_then(sq_then_to_string), Valid(4.to_string()));
assert_eq!(Valid(1_000_000).and_then(sq_then_to_string), Invalid("overflowed"));
assert_eq!(Invalid("invalid").and_then(sq_then_to_string), Invalid("invalid"));
sourcepub fn or<U>(self, other: Validated<T, U>) -> Validated<T, U>
pub fn or<U>(self, other: Validated<T, U>) -> Validated<T, U>
Returns other
if the result is Invalid
, otherwise returns the Valid
value of self
.
Arguments passed to or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use or_else
, which is
lazily evaluated.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
let y: Validated<i32, &str> = Invalid("late error");
assert_eq!(x.or(y), Valid(2));
let x: Validated<i32, &str> = Invalid("early error");
let y: Validated<i32, &str> = Valid(2);
assert_eq!(x.or(y), Valid(2));
let x: Validated<i32, &str> = Invalid("not a 2");
let y: Validated<i32, &str> = Invalid("late error");
assert_eq!(x.or(y), Invalid("late error"));
let x: Validated<i32, &str> = Valid(2);
let y: Validated<i32, &str> = Valid(100);
assert_eq!(x.or(y), Valid(2));
sourcepub fn or_else<U, F: FnOnce(E) -> Validated<T, U>>(
self,
f: F
) -> Validated<T, U>
pub fn or_else<U, F: FnOnce(E) -> Validated<T, U>>( self, f: F ) -> Validated<T, U>
Calls f
if the result is Invalid
, otherwise returns the Valid
value of self
.
This function can be used for control flow based on Validated values.
Examples
use rust2fun::prelude::*;
fn sq(x: u32) -> Validated<u32, u32> { Valid(x * x) }
fn err(x: u32) -> Validated<u32, u32> { Invalid(x) }
assert_eq!(Valid(2).or_else(sq).or_else(sq), Valid(2));
assert_eq!(Valid(2).or_else(err).or_else(sq), Valid(2));
assert_eq!(Invalid(3).or_else(sq).or_else(err), Valid(9));
assert_eq!(Invalid(3).or_else(err).or_else(err), Invalid(3));
sourcepub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Returns the contained Valid
value or a provided default.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use unwrap_or_else
, which is
lazily evaluated.
Examples
use rust2fun::prelude::*;
let x: Validated<i32, &str> = Valid(2);
assert_eq!(x.unwrap_or(0), 2);
let x: Validated<i32, &str> = Invalid("error");
assert_eq!(x.unwrap_or(0), 0);
sourcepub fn unwrap_or_else<F: FnOnce(E) -> T>(self, f: F) -> T
pub fn unwrap_or_else<F: FnOnce(E) -> T>(self, f: F) -> T
Trait Implementations§
source§impl<T, E> From<Validated<T, E>> for ValidatedNev<T, E>
impl<T, E> From<Validated<T, E>> for ValidatedNev<T, E>
source§impl<A, B, E> Functor<B> for Validated<A, E>
impl<A, B, E> Functor<B> for Validated<A, E>
source§fn fmap<F>(self, f: F) -> Self::Target<B>where
F: FnMut(Self::Param) -> B,
Self: Sized,
fn fmap<F>(self, f: F) -> Self::Target<B>where F: FnMut(Self::Param) -> B, Self: Sized,
.map
method.source§impl<T: Ord, E: Ord> Ord for Validated<T, E>
impl<T: Ord, E: Ord> Ord for Validated<T, E>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq, E: PartialEq> PartialEq<Validated<T, E>> for Validated<T, E>
impl<T: PartialEq, E: PartialEq> PartialEq<Validated<T, E>> for Validated<T, E>
source§impl<T: PartialOrd, E: PartialOrd> PartialOrd<Validated<T, E>> for Validated<T, E>
impl<T: PartialOrd, E: PartialOrd> PartialOrd<Validated<T, E>> for Validated<T, E>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more