[][src]Struct status::Status

pub struct Status<K: Kind = Unkind, C: Context = NoContext> { /* fields omitted */ }

A container for use in Result<_, Status>..

Goals:

  • Easy crate inter-op while maintaining programmatic processing.
  • User-friendly without losing helpful debug information.

Note: this is optimized for the happy-path. When failing frequently inside of an inner loop, consider using your Kind to convey your status.

Example

use status::Kind;

#[derive(Copy, Clone, Debug, derive_more::Display)]
enum ErrorKind {
  #[display(fmt = "Failed to read file")]
  Read,
  #[display(fmt = "Failed to parse")]
  Parse,
}
type Status = status::Status<ErrorKind>;
type Result<T, E = Status> = std::result::Result<T, E>;

fn read_file() -> Result<()> {
    return ErrorKind::Read.into_err();
}

Methods

impl<K: Kind, C: Context> Status<K, C>[src]

pub fn new<U>(kind: U) -> Self where
    U: Into<K>, 
[src]

Create a container for the specified status Kind.

Example

fn read_file() -> Result<(), status::Status> {
    return Err(status::Status::new("Failed to read file"));
}

pub fn with_source<E>(self, error: E) -> Self where
    E: Error + 'static, 
[src]

Add a public error.

pub fn with_internal<E>(self, error: E) -> Self where
    E: Error + 'static, 
[src]

Add an internal error.

pub fn kind(&self) -> K[src]

Programmatic identifier for which error occurred.

Example

#[derive(Copy, Clone, Debug, derive_more::Display)]
enum ErrorKind {
  #[display(fmt = "Failed to read file")]
  Read,
  #[display(fmt = "Failed to parse")]
  Parse,
}

fn read_file() -> Result<()> {
}

fn process_file() -> Result<()> {
    match read_file() {
        Ok(_) => Ok(()),
        Err(e) => match e.kind() {
          ErrorKind::Read => Err(e),
          ErrorKind::Parse => Ok(()),
        }
    }
}

Important traits for Chain<'a>
pub fn sources(&self) -> Chain[src]

An iterator for the chain of sources.

When debugging, to display internal sources, run Status::into_internal.

Example

use status::Status;
use std::io;

pub fn underlying_io_error_kind(error: &Status) -> Option<io::ErrorKind> {
    for cause in error.sources() {
        if let Some(io_error) = cause.downcast_ref::<io::Error>() {
            return Some(io_error.kind());
        }
    }
    None
}

pub fn root_source(&self) -> Option<&(dyn Error + 'static)>[src]

The lowest level cause of this error — this error's cause's cause's cause etc.

The root cause is the last error in the iterator produced by sources().

Example

use status::Status;
use std::io;

pub fn underlying_io_error_kind(error: &Status) -> Option<io::ErrorKind> {
    let cause = error.root_source()?;
    if let Some(io_error) = cause.downcast_ref::<io::Error>() {
        return Some(io_error.kind());
    }
    None
}

pub fn into_internal(self) -> InternalStatus<K, C>[src]

View of Status, exposing implementation details.

Error::source and InternalStatus::sources are for debug / display purposes only and relying on them programmatically is likely to break across minor releases.

Example

fn display_status(status: status::Status) {
    if std::env::var("DEBUG").as_ref().map(|s| s.as_ref()).unwrap_or("0") == "1" {
        let status = status.into_internal();
        println!("{}", status);
    } else {
        println!("{}", status);
    }
}

pub fn into_err<T>(self) -> Result<T, Self>[src]

Convenience for returning an error.

Trait Implementations

impl<K: Kind, C: Context> Display for Status<K, C>[src]

impl<K: Debug + Kind, C: Debug + Context> Debug for Status<K, C>[src]

impl<K: Kind, C: Context> Deref for Status<K, C>[src]

type Target = C

The resulting type after dereferencing.

impl<K: Kind, C: Context> DerefMut for Status<K, C>[src]

impl<K: Kind, C: Context> Error for Status<K, C>[src]

Auto Trait Implementations

impl<K = Unkind, C = NoContext> !Send for Status<K, C>

impl<K = Unkind, C = NoContext> !Sync for Status<K, C>

impl<K, C> Unpin for Status<K, C>

impl<K = Unkind, C = NoContext> !UnwindSafe for Status<K, C>

impl<K = Unkind, C = NoContext> !RefUnwindSafe for Status<K, C>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]