use super::{super::problem::*, into::*};
use std::{any::*, error::Error, io};
pub trait ProblemResult<OkT> {
fn via<ErrorT>(self, error: ErrorT) -> Result<OkT, Problem>
where
ErrorT: 'static + Error + Send + Sync;
fn map_via<ErrorT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
ErrorT: 'static + Error + Send + Sync,
FromT: FnOnce() -> ErrorT;
fn with<AttachmentT>(self, attachment: AttachmentT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync;
fn map_with<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> AttachmentT;
fn with_once<AttachmentT>(self, attachment: AttachmentT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync;
fn map_with_once<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> AttachmentT;
fn maybe_with<AttachmentT>(self, attachment: Option<AttachmentT>) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync;
fn maybe_map_with<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> Option<AttachmentT>;
fn maybe_with_once<AttachmentT>(self, attachment: Option<AttachmentT>) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync;
fn maybe_map_with_once<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> Option<AttachmentT>;
fn with_location(self) -> Result<OkT, Problem>;
fn with_backtrace(self) -> Result<OkT, Problem>;
fn into_io_error(self) -> io::Result<OkT>;
}
impl<ResultT, OkT> ProblemResult<OkT> for ResultT
where
ResultT: IntoProblemResult<OkT>,
{
#[track_caller]
fn via<ViaErrorT>(self, error: ViaErrorT) -> Result<OkT, Problem>
where
ViaErrorT: 'static + Error + Send + Sync,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.via(error)),
}
}
#[track_caller]
fn map_via<ErrorT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
ErrorT: 'static + Error + Send + Sync,
FromT: FnOnce() -> ErrorT,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.via(from())),
}
}
#[track_caller]
fn with<AttachmentT>(self, attachment: AttachmentT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with(attachment)),
}
}
#[track_caller]
fn map_with<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> AttachmentT,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with(from())),
}
}
#[track_caller]
fn with_once<AttachmentT>(self, attachment: AttachmentT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_once(attachment)),
}
}
#[track_caller]
fn map_with_once<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> AttachmentT,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_once(from())),
}
}
#[track_caller]
fn maybe_with<AttachmentT>(self, attachment: Option<AttachmentT>) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.maybe_with(attachment)),
}
}
#[track_caller]
fn maybe_map_with<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> Option<AttachmentT>,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.maybe_with(from())),
}
}
#[track_caller]
fn maybe_with_once<AttachmentT>(self, attachment: Option<AttachmentT>) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.maybe_with_once(attachment)),
}
}
#[track_caller]
fn maybe_map_with_once<AttachmentT, FromT>(self, from: FromT) -> Result<OkT, Problem>
where
AttachmentT: Any + Send + Sync,
FromT: FnOnce() -> Option<AttachmentT>,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.maybe_with_once(from())),
}
}
#[track_caller]
fn with_location(self) -> Result<OkT, Problem> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_location()),
}
}
#[track_caller]
fn with_backtrace(self) -> Result<OkT, Problem> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_backtrace()),
}
}
#[track_caller]
fn into_io_error(self) -> io::Result<OkT> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.into()),
}
}
}