2016-10-19 07:30:02 +00:00
|
|
|
//! Success, failure, and forward handling.
|
|
|
|
//!
|
|
|
|
//! The `Outcome<S, E, F>` type is similar to the standard library's `Result<S,
|
|
|
|
//! E>` type. It is an enum with three variants, each containing a value:
|
|
|
|
//! `Success(S)`, which represents a successful outcome, `Failure(E)`, which
|
|
|
|
//! represents a failing outcome, and `Forward(F)`, which represents neither a
|
|
|
|
//! success or failure, but instead, indicates that processing could not be
|
|
|
|
//! handled and should instead be _forwarded_ to whatever can handle the
|
|
|
|
//! processing next.
|
|
|
|
//!
|
|
|
|
//! The `Outcome` type is the return type of many of the core Rocket traits,
|
2020-02-03 08:53:59 +00:00
|
|
|
//! including [`FromRequest`](crate::request::FromRequest), [`FromData`]
|
|
|
|
//! [`Responder`]. It is also the return type of request handlers via the
|
|
|
|
//! [`Response`](crate::response::Response) type.
|
|
|
|
//!
|
|
|
|
//! [`FromData`]: crate::data::FromData
|
|
|
|
//! [`Responder`]: crate::response::Responder
|
2016-10-19 07:30:02 +00:00
|
|
|
//!
|
|
|
|
//! # Success
|
|
|
|
//!
|
|
|
|
//! A successful `Outcome<S, E, F>`, `Success(S)`, is returned from functions
|
|
|
|
//! that complete successfully. The meaning of a `Success` outcome depends on
|
|
|
|
//! the context. For instance, the `Outcome` of the `from_data` method of the
|
2020-02-03 08:53:59 +00:00
|
|
|
//! [`FromData`] trait will be matched against the type expected by the user.
|
|
|
|
//! For example, consider the following handler:
|
2016-10-19 07:30:02 +00:00
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
|
|
|
//! #[post("/", data = "<my_val>")]
|
|
|
|
//! fn hello(my_val: S) -> ... { }
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-02-03 08:53:59 +00:00
|
|
|
//! The [`FromData`] implementation for the type `S` returns an `Outcome` with a
|
2016-10-19 07:30:02 +00:00
|
|
|
//! `Success(S)`. If `from_data` returns a `Success`, the `Success` value will
|
|
|
|
//! be unwrapped and the value will be used as the value of `my_val`.
|
|
|
|
//!
|
|
|
|
//! # Failure
|
|
|
|
//!
|
|
|
|
//! A failure `Outcome<S, E, F>`, `Failure(E)`, is returned when a function
|
|
|
|
//! fails with some error and no processing can or should continue as a result.
|
|
|
|
//! The meaning of a failure depends on the context.
|
|
|
|
//!
|
2017-04-16 07:59:52 +00:00
|
|
|
//! In Rocket, a `Failure` generally means that a request is taken out of normal
|
2016-10-19 07:30:02 +00:00
|
|
|
//! processing. The request is then given to the catcher corresponding to some
|
2017-09-14 12:02:57 +00:00
|
|
|
//! status code. Users can catch failures by requesting a type of `Result<S, E>`
|
2016-10-19 07:30:02 +00:00
|
|
|
//! or `Option<S>` in request handlers. For example, if a user's handler looks
|
|
|
|
//! like:
|
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
|
|
|
//! #[post("/", data = "<my_val>")]
|
|
|
|
//! fn hello(my_val: Result<S, E>) -> ... { }
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-02-03 08:53:59 +00:00
|
|
|
//! The [`FromData`] implementation for the type `S` returns an `Outcome` with a
|
2016-10-19 07:30:02 +00:00
|
|
|
//! `Success(S)` and `Failure(E)`. If `from_data` returns a `Failure`, the
|
|
|
|
//! `Failure` value will be unwrapped and the value will be used as the `Err`
|
|
|
|
//! value of `my_val` while a `Success` will be unwrapped and used the `Ok`
|
|
|
|
//! value.
|
|
|
|
//!
|
|
|
|
//! # Forward
|
|
|
|
//!
|
|
|
|
//! A forward `Outcome<S, E, F>`, `Forward(F)`, is returned when a function
|
|
|
|
//! wants to indicate that the requested processing should be _forwarded_ to the
|
|
|
|
//! next available processor. Again, the exact meaning depends on the context.
|
|
|
|
//!
|
|
|
|
//! In Rocket, a `Forward` generally means that a request is forwarded to the
|
|
|
|
//! next available request handler. For example, consider the following request
|
|
|
|
//! handler:
|
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
|
|
|
//! #[post("/", data = "<my_val>")]
|
|
|
|
//! fn hello(my_val: S) -> ... { }
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-02-03 08:53:59 +00:00
|
|
|
//! The [`FromData`] implementation for the type `S` returns an `Outcome` with a
|
2016-10-19 07:30:02 +00:00
|
|
|
//! `Success(S)`, `Failure(E)`, and `Forward(F)`. If the `Outcome` is a
|
|
|
|
//! `Forward`, the `hello` handler isn't called. Instead, the incoming request
|
|
|
|
//! is forwarded, or passed on to, the next matching route, if any. Ultimately,
|
|
|
|
//! if there are no non-forwarding routes, forwarded requests are handled by the
|
|
|
|
//! 404 catcher. Similar to `Failure`s, users can catch `Forward`s by requesting
|
|
|
|
//! a type of `Option<S>`. If an `Outcome` is a `Forward`, the `Option` will be
|
|
|
|
//! `None`.
|
|
|
|
|
2016-10-04 00:09:13 +00:00
|
|
|
use std::fmt;
|
2016-04-11 10:57:23 +00:00
|
|
|
|
2017-06-02 04:44:31 +00:00
|
|
|
use yansi::{Paint, Color};
|
2016-10-04 00:09:13 +00:00
|
|
|
|
2016-10-25 09:17:49 +00:00
|
|
|
use self::Outcome::*;
|
|
|
|
|
2016-10-19 07:30:02 +00:00
|
|
|
/// An enum representing success (`Success`), failure (`Failure`), or
|
|
|
|
/// forwarding (`Forward`).
|
2016-12-10 03:53:13 +00:00
|
|
|
///
|
2019-06-13 01:48:02 +00:00
|
|
|
/// See the [top level documentation](crate::outcome) for detailed information.
|
2016-10-08 06:20:49 +00:00
|
|
|
#[must_use]
|
2016-10-19 07:30:02 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub enum Outcome<S, E, F> {
|
|
|
|
/// Contains the success value.
|
|
|
|
Success(S),
|
|
|
|
/// Contains the failure error value.
|
|
|
|
Failure(E),
|
|
|
|
/// Contains the value to forward on.
|
|
|
|
Forward(F),
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 11:03:50 +00:00
|
|
|
/// Conversion trait from some type into an Outcome type.
|
2016-10-25 09:17:49 +00:00
|
|
|
pub trait IntoOutcome<S, E, F> {
|
2017-07-03 22:29:12 +00:00
|
|
|
/// The type to use when returning an `Outcome::Failure`.
|
2017-06-24 09:49:16 +00:00
|
|
|
type Failure: Sized;
|
2017-07-03 22:29:12 +00:00
|
|
|
|
|
|
|
/// The type to use when returning an `Outcome::Forward`.
|
2017-06-24 09:49:16 +00:00
|
|
|
type Forward: Sized;
|
2017-04-19 04:52:02 +00:00
|
|
|
|
2017-07-03 22:29:12 +00:00
|
|
|
/// Converts `self` into an `Outcome`. If `self` represents a success, an
|
|
|
|
/// `Outcome::Success` is returned. Otherwise, an `Outcome::Failure` is
|
|
|
|
/// returned with `failure` as the inner value.
|
2017-06-24 09:49:16 +00:00
|
|
|
fn into_outcome(self, failure: Self::Failure) -> Outcome<S, E, F>;
|
2017-07-03 22:29:12 +00:00
|
|
|
|
|
|
|
/// Converts `self` into an `Outcome`. If `self` represents a success, an
|
|
|
|
/// `Outcome::Success` is returned. Otherwise, an `Outcome::Forward` is
|
|
|
|
/// returned with `forward` as the inner value.
|
2017-06-24 09:49:16 +00:00
|
|
|
fn or_forward(self, forward: Self::Forward) -> Outcome<S, E, F>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, E, F> IntoOutcome<S, E, F> for Option<S> {
|
|
|
|
type Failure = E;
|
|
|
|
type Forward = F;
|
|
|
|
|
2017-07-04 21:05:16 +00:00
|
|
|
#[inline]
|
|
|
|
fn into_outcome(self, failure: E) -> Outcome<S, E, F> {
|
|
|
|
match self {
|
|
|
|
Some(val) => Success(val),
|
|
|
|
None => Failure(failure)
|
|
|
|
}
|
2017-06-24 09:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 21:05:16 +00:00
|
|
|
#[inline]
|
|
|
|
fn or_forward(self, forward: F) -> Outcome<S, E, F> {
|
|
|
|
match self {
|
|
|
|
Some(val) => Success(val),
|
|
|
|
None => Forward(forward)
|
|
|
|
}
|
2017-06-24 09:49:16 +00:00
|
|
|
}
|
2016-10-25 09:17:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> Outcome<S, E, F> {
|
|
|
|
/// Unwraps the Outcome, yielding the contents of a Success.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2016-10-19 07:30:02 +00:00
|
|
|
/// Panics if the value is not `Success`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.unwrap(), 10);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn unwrap(self) -> S {
|
|
|
|
match self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(val) => val,
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => panic!("Expected a successful outcome!")
|
2016-10-09 11:29:02 +00:00
|
|
|
}
|
2016-10-14 01:39:23 +00:00
|
|
|
}
|
2016-10-09 11:29:02 +00:00
|
|
|
|
2016-11-04 13:35:04 +00:00
|
|
|
/// Unwraps the Outcome, yielding the contents of a Success.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// If the value is not `Success`, panics with the given `message`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.expect("success value"), 10);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-11-04 13:35:04 +00:00
|
|
|
pub fn expect(self, message: &str) -> S {
|
|
|
|
match self {
|
|
|
|
Success(val) => val,
|
|
|
|
_ => panic!("Outcome::expect() failed: {}", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Success`.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.is_success(), true);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.is_success(), false);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.is_success(), false);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn is_success(&self) -> bool {
|
|
|
|
match *self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(_) => true,
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => false
|
|
|
|
}
|
2016-10-09 11:29:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Failure`.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.is_failure(), false);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.is_failure(), true);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.is_failure(), false);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn is_failure(&self) -> bool {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Failure(_) => true,
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => false
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Return true if this `Outcome` is a `Forward`.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.is_forward(), false);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.is_forward(), false);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.is_forward(), true);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn is_forward(&self) -> bool {
|
2016-04-23 02:48:03 +00:00
|
|
|
match *self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Forward(_) => true,
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => false
|
2016-10-08 06:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<S>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Success` if this is a `Success`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.succeeded(), Some(10));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.succeeded(), None);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.succeeded(), None);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn succeeded(self) -> Option<S> {
|
|
|
|
match self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(val) => Some(val),
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<E>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Failure` if this is a `Failure`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.failed(), None);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.failed(), Some("Hi! I'm an error."));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.failed(), None);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn failed(self) -> Option<E> {
|
|
|
|
match self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Failure(val) => Some(val),
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => None
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Option<F>`.
|
|
|
|
///
|
|
|
|
/// Returns the `Some` of the `Forward` if this is a `Forward`, otherwise
|
|
|
|
/// returns `None`. `self` is consumed, and all other values are discarded.
|
2016-10-19 07:30:02 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.forwarded(), None);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.forwarded(), None);
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.forwarded(), Some(25));
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
pub fn forwarded(self) -> Option<F> {
|
|
|
|
match self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Forward(val) => Some(val),
|
2016-10-14 01:39:23 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:23:03 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Result<S, T>` for a given `T`.
|
|
|
|
///
|
|
|
|
/// Returns `Ok` with the `Success` value if this is a `Success`, otherwise
|
|
|
|
/// returns an `Err` with the provided value. `self` is consumed, and all
|
|
|
|
/// other values are discarded.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.success_or(false), Ok(10));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.success_or(false), Err(false));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.success_or("whoops"), Err("whoops"));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn success_or<T>(self, value: T) -> Result<S, T> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Ok(val),
|
|
|
|
_ => Err(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts from `Outcome<S, E, F>` to `Result<S, T>` for a given `T`
|
|
|
|
/// produced from a supplied function or closure.
|
|
|
|
///
|
|
|
|
/// Returns `Ok` with the `Success` value if this is a `Success`, otherwise
|
|
|
|
/// returns an `Err` with the result of calling `f`. `self` is consumed, and
|
|
|
|
/// all other values are discarded.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.success_or_else(|| false), Ok(10));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.success_or_else(|| false), Err(false));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(25);
|
|
|
|
/// assert_eq!(x.success_or_else(|| "whoops"), Err("whoops"));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn success_or_else<T, V: FnOnce() -> T>(self, f: V) -> Result<S, T> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Ok(val),
|
|
|
|
_ => Err(f())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 07:30:02 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Outcome<&S, &E, &F>`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// assert_eq!(x.as_ref(), Success(&10));
|
|
|
|
///
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("Hi! I'm an error.");
|
|
|
|
/// assert_eq!(x.as_ref(), Failure(&"Hi! I'm an error."));
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-19 07:30:02 +00:00
|
|
|
pub fn as_ref(&self) -> Outcome<&S, &E, &F> {
|
|
|
|
match *self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(ref val) => Success(val),
|
|
|
|
Failure(ref val) => Failure(val),
|
|
|
|
Forward(ref val) => Forward(val),
|
2016-10-19 07:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 04:52:02 +00:00
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<T, E, F>` by applying the
|
|
|
|
/// function `f` to the value of type `S` in `self` if `self` is an
|
|
|
|
/// `Outcome::Success`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
///
|
|
|
|
/// let mapped = x.map(|v| if v == 10 { "10" } else { "not 10" });
|
|
|
|
/// assert_eq!(mapped, Success("10"));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn map<T, M: FnOnce(S) -> T>(self, f: M) -> Outcome<T, E, F> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Success(f(val)),
|
|
|
|
Failure(val) => Failure(val),
|
|
|
|
Forward(val) => Forward(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<S, T, F>` by applying the
|
|
|
|
/// function `f` to the value of type `E` in `self` if `self` is an
|
|
|
|
/// `Outcome::Failure`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Failure("hi");
|
|
|
|
///
|
|
|
|
/// let mapped = x.map_failure(|v| if v == "hi" { 10 } else { 0 });
|
|
|
|
/// assert_eq!(mapped, Failure(10));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn map_failure<T, M: FnOnce(E) -> T>(self, f: M) -> Outcome<S, T, F> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Success(val),
|
|
|
|
Failure(val) => Failure(f(val)),
|
|
|
|
Forward(val) => Forward(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<S, E, T>` by applying the
|
|
|
|
/// function `f` to the value of type `F` in `self` if `self` is an
|
|
|
|
/// `Outcome::Forward`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, usize> = Forward(5);
|
|
|
|
///
|
|
|
|
/// let mapped = x.map_forward(|v| if v == 5 { "a" } else { "b" });
|
|
|
|
/// assert_eq!(mapped, Forward("a"));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn map_forward<T, M: FnOnce(F) -> T>(self, f: M) -> Outcome<S, E, T> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Success(val),
|
|
|
|
Failure(val) => Failure(val),
|
|
|
|
Forward(val) => Forward(f(val)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 10:34:35 +00:00
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<T, E, F>` by applying the
|
|
|
|
/// function `f` to the value of type `S` in `self` if `self` is an
|
|
|
|
/// `Outcome::Success`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, bool> = Success(10);
|
|
|
|
///
|
|
|
|
/// let mapped = x.and_then(|v| match v {
|
|
|
|
/// 10 => Success("10"),
|
|
|
|
/// 1 => Forward(false),
|
|
|
|
/// _ => Failure("30")
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// assert_eq!(mapped, Success("10"));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn and_then<T, M: FnOnce(S) -> Outcome<T, E, F>>(self, f: M) -> Outcome<T, E, F> {
|
|
|
|
match self {
|
|
|
|
Success(val) => f(val),
|
|
|
|
Failure(val) => Failure(val),
|
|
|
|
Forward(val) => Forward(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<S, T, F>` by applying the
|
|
|
|
/// function `f` to the value of type `E` in `self` if `self` is an
|
|
|
|
/// `Outcome::Failure`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, bool> = Failure("hi");
|
|
|
|
///
|
|
|
|
/// let mapped = x.failure_then(|v| match v {
|
|
|
|
/// "hi" => Failure(10),
|
|
|
|
/// "test" => Forward(false),
|
|
|
|
/// _ => Success(10)
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// assert_eq!(mapped, Failure(10));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn failure_then<T, M: FnOnce(E) -> Outcome<S, T, F>>(self, f: M) -> Outcome<S, T, F> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Success(val),
|
|
|
|
Failure(val) => f(val),
|
|
|
|
Forward(val) => Forward(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps an `Outcome<S, E, F>` to an `Outcome<S, E, T>` by applying the
|
|
|
|
/// function `f` to the value of type `F` in `self` if `self` is an
|
|
|
|
/// `Outcome::Forward`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let x: Outcome<i32, &str, Option<bool>> = Forward(Some(false));
|
|
|
|
///
|
|
|
|
/// let mapped = x.forward_then(|v| match v {
|
|
|
|
/// Some(true) => Success(10),
|
|
|
|
/// Some(false) => Forward(20),
|
|
|
|
/// None => Failure("10")
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// assert_eq!(mapped, Forward(20));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn forward_then<T, M: FnOnce(F) -> Outcome<S, E, T>>(self, f: M) -> Outcome<S, E, T> {
|
|
|
|
match self {
|
|
|
|
Success(val) => Success(val),
|
|
|
|
Failure(val) => Failure(val),
|
|
|
|
Forward(val) => f(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 07:30:02 +00:00
|
|
|
/// Converts from `Outcome<S, E, F>` to `Outcome<&mut S, &mut E, &mut F>`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::outcome::Outcome;
|
|
|
|
/// # use rocket::outcome::Outcome::*;
|
|
|
|
/// #
|
|
|
|
/// let mut x: Outcome<i32, &str, usize> = Success(10);
|
|
|
|
/// if let Success(val) = x.as_mut() {
|
|
|
|
/// *val = 20;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(x.unwrap(), 20);
|
|
|
|
/// ```
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-19 07:30:02 +00:00
|
|
|
pub fn as_mut(&mut self) -> Outcome<&mut S, &mut E, &mut F> {
|
|
|
|
match *self {
|
2016-10-25 09:17:49 +00:00
|
|
|
Success(ref mut val) => Success(val),
|
|
|
|
Failure(ref mut val) => Failure(val),
|
|
|
|
Forward(ref mut val) => Forward(val),
|
2016-10-19 07:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 10:16:46 +00:00
|
|
|
#[inline]
|
2016-10-14 01:39:23 +00:00
|
|
|
fn formatting(&self) -> (Color, &'static str) {
|
|
|
|
match *self {
|
2017-06-02 04:44:31 +00:00
|
|
|
Success(..) => (Color::Green, "Success"),
|
|
|
|
Failure(..) => (Color::Red, "Failure"),
|
|
|
|
Forward(..) => (Color::Yellow, "Forward"),
|
2016-10-14 01:39:23 +00:00
|
|
|
}
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 01:04:34 +00:00
|
|
|
/// Unwraps an [`Outcome`] to its success value, otherwise propagating the
|
|
|
|
/// forward or failure.
|
|
|
|
///
|
|
|
|
/// In the case of a `Forward` or `Failure` variant, the inner type is passed to
|
|
|
|
/// [`From`](std::convert::From), allowing for the conversion between specific
|
|
|
|
/// and more general types. The resulting forward/error is immediately returned.
|
|
|
|
///
|
|
|
|
/// Because of the early return, `try_outcome!` can only be used in methods that
|
|
|
|
/// return [`Outcome`].
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
|
|
|
/// # #![feature(proc_macro_hygiene)]
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// # use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
/// use rocket::request::{self, Request, FromRequest, State};
|
|
|
|
/// use rocket::outcome::Outcome::*;
|
|
|
|
///
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct Atomics {
|
|
|
|
/// uncached: AtomicUsize,
|
|
|
|
/// cached: AtomicUsize,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct Guard1;
|
|
|
|
/// struct Guard2;
|
|
|
|
///
|
2020-01-31 09:34:15 +00:00
|
|
|
/// #[rocket::async_trait]
|
2019-09-11 01:04:34 +00:00
|
|
|
/// impl<'a, 'r> FromRequest<'a, 'r> for Guard1 {
|
|
|
|
/// type Error = ();
|
|
|
|
///
|
2020-01-31 09:34:15 +00:00
|
|
|
/// async fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
2019-09-11 01:04:34 +00:00
|
|
|
/// // Attempt to fetch the guard, passing through any error or forward.
|
2020-01-31 09:34:15 +00:00
|
|
|
/// let atomics = try_outcome!(req.guard::<State<'_, Atomics>>().await);
|
2019-09-11 01:04:34 +00:00
|
|
|
/// atomics.uncached.fetch_add(1, Ordering::Relaxed);
|
|
|
|
/// req.local_cache(|| atomics.cached.fetch_add(1, Ordering::Relaxed));
|
|
|
|
///
|
|
|
|
/// Success(Guard1)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2020-01-31 09:34:15 +00:00
|
|
|
/// #[rocket::async_trait]
|
2019-09-11 01:04:34 +00:00
|
|
|
/// impl<'a, 'r> FromRequest<'a, 'r> for Guard2 {
|
|
|
|
/// type Error = ();
|
|
|
|
///
|
2020-01-31 09:34:15 +00:00
|
|
|
/// async fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
2019-09-11 01:04:34 +00:00
|
|
|
/// // Attempt to fetch the guard, passing through any error or forward.
|
2020-01-31 09:34:15 +00:00
|
|
|
/// let guard1: Guard1 = try_outcome!(req.guard::<Guard1>().await);
|
2019-09-11 01:04:34 +00:00
|
|
|
/// Success(Guard2)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! try_outcome {
|
|
|
|
($expr:expr $(,)?) => (match $expr {
|
|
|
|
$crate::outcome::Outcome::Success(val) => val,
|
|
|
|
$crate::outcome::Outcome::Failure(e) => {
|
|
|
|
return $crate::outcome::Outcome::Failure(::std::convert::From::from(e))
|
|
|
|
},
|
|
|
|
$crate::outcome::Outcome::Forward(f) => {
|
|
|
|
return $crate::outcome::Outcome::Forward(::std::convert::From::from(f))
|
|
|
|
},
|
|
|
|
});
|
2017-06-24 09:49:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> fmt::Debug for Outcome<S, E, F> {
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-10-14 01:39:23 +00:00
|
|
|
write!(f, "Outcome::{}", self.formatting().1)
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:39:23 +00:00
|
|
|
impl<S, E, F> fmt::Display for Outcome<S, E, F> {
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-10-14 01:39:23 +00:00
|
|
|
let (color, string) = self.formatting();
|
2018-11-19 10:11:38 +00:00
|
|
|
write!(f, "{}", Paint::default(string).fg(color))
|
2016-04-11 10:57:23 +00:00
|
|
|
}
|
|
|
|
}
|