libdeadmock 0.1.6

API Mocking and Virtualization
// Copyright (c) 2018 libdeadmock developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Errors generated by the library.
use failure::{Backtrace, Context, Fail};
use std::fmt;

/// Errors generated by the library.
#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>,
}

impl Error {
    /// Get the kind of this error.
    pub fn kind(&self) -> ErrorKind {
        *self.inner.get_context()
    }
}

impl Fail for Error {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Self {
            inner: Context::new(kind),
        }
    }
}

impl From<Context<ErrorKind>> for Error {
    fn from(inner: Context<ErrorKind>) -> Self {
        Self { inner }
    }
}

/// Kind of error generated by the library.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
#[allow(clippy::stutter)]
pub enum ErrorKind {
    /// If `use-proxy` is true, a `proxy-url` must also be given.
    #[fail(display = "invalid proxy configuration! proxy url is required")]
    InvalidProxyConfig,
    /// Generated if the runtime configuration cannot be created.
    #[fail(display = "invalid runtime configuration!")]
    InvalidRuntimeConfig,
    /// Generated if a mapping is inserted into the map with the same `Uuid` key.
    #[fail(display = "mapping key collision")]
    MappingKeyCollision,
    /// Generated if no mapping can be found that matches the request.
    #[fail(display = "mapping not found")]
    MappingNotFound,
}