use super::super::problem::*;

use std::error::Error;

//
// IntoProblemResult
//

/// Maps [Err] into a [Problem].
pub trait IntoProblemResult<OkT> {
    /// Maps [Err] into a [Problem].
    fn into_problem(self) -> Result<OkT, Problem>;
}

impl<OkT> IntoProblemResult<OkT> for Result<OkT, Problem> {
    #[track_caller]
    fn into_problem(self) -> Result<OkT, Problem> {
        self
    }
}

impl<OkT, ErrorT> IntoProblemResult<OkT> for Result<OkT, ErrorT>
where
    ErrorT: 'static + Error + Send + Sync,
{
    #[track_caller]
    fn into_problem(self) -> Result<OkT, Problem> {
        // Note that we are *not* using map_err() here because a closure cannot be annotated with #[track_caller]
        match self {
            Ok(ok) => Ok(ok),
            Err(error) => Err(error.into()),
        }
    }
}
