kxio 3.0.0

Provides injectable Filesystem and Network resources to make code more testable
Documentation
//
use derive_more::derive::From;

use crate::net::Request;

use super::system::MockError;

/// The Errors that may occur within [kxio::net][crate::net].
#[derive(Debug, From, derive_more::Display)]
pub enum Error {
    /// The Errors that may occur when processing a `Request`.
    ///
    /// Note: Errors may include the full URL used to make the `Request`. If the URL
    /// contains sensitive information (e.g. an API key as a query parameter), be
    /// sure to remove it ([`without_url`](reqwest::Error::without_url))
    Reqwest(reqwest::Error),

    /// The Errors that may occur when processing a `Request`.
    ///
    /// The cause has been converted to a String.
    Request(String),

    Url(url::ParseError),

    /// There was network request that doesn't match any that were expected
    #[display("Unexpected request: {0}", 0.to_string())]
    UnexpectedMockRequest(Request),

    /// There was an error accessing the list of expected requests.
    RwLockLocked,

    /// There was an error making a network request.
    Http(http::Error),

    /// Attempted to extract a [MockNet][super::MockNet] from a [Net][super::Net] that does not contain one.
    NetIsNotAMock,

    InvalidMock(MockError),

    /// The returned response is has an error status code (i.e. 4xx or 5xx)
    #[display("response error: {}", response.status())]
    ResponseError {
        response: reqwest::Response,
    },
}
impl std::error::Error for Error {}
impl Clone for Error {
    fn clone(&self) -> Self {
        match self {
            Self::Reqwest(req) => Self::Request(req.to_string()),
            err => err.clone(),
        }
    }
}

/// Represents a success or a failure within [kxio::net][crate::net].
///
/// Any failure is related to `std::io`, a Path Traversal
/// (i.e. trying to escape the base of the `FileSystem`),
/// or attempting to use a file as a directory or /vise versa/.
pub type Result<T> = core::result::Result<T, Error>;