use super::super::{attachment::*, error::*, problem::*, result::*};
use std::process::*;
impl From<&Problem> for ExitCode {
fn from(problem: &Problem) -> Self {
problem
.attachment_of_type::<Self>()
.map(|exit_code| exit_code.clone())
.unwrap_or(Self::FAILURE)
}
}
impl From<Problem> for ExitCode {
fn from(problem: Problem) -> Self {
(&problem).into()
}
}
gloss_error!(ExitError, "");
impl ExitError {
#[track_caller]
pub fn code<ExitCodeT>(exit_code: ExitCodeT) -> Problem
where
ExitCodeT: Into<ExitCode>,
{
Self::default_as_problem().with_exit_code(exit_code)
}
#[track_caller]
pub fn code_message<ExitCodeT, ToStringT>(exit_code: ExitCodeT, message: ToStringT) -> Problem
where
ExitCodeT: Into<ExitCode>,
ToStringT: ToString,
{
Self::as_problem(message).with_exit_code(exit_code)
}
#[track_caller]
pub fn failure() -> Problem {
Self::default_as_problem().with_failure_exit_code()
}
#[track_caller]
pub fn failure_message<ToStringT>(message: ToStringT) -> Problem
where
ToStringT: ToString,
{
Self::as_problem(message).with_failure_exit_code()
}
#[track_caller]
pub fn success() -> Problem {
Self::default_as_problem().with_success_exit_code()
}
}
pub trait WithExitCode {
fn with_exit_code<ExitCodeT>(self, exit_code: ExitCodeT) -> Self
where
ExitCodeT: Into<ExitCode>;
fn with_failure_exit_code(self) -> Self;
fn with_success_exit_code(self) -> Self;
}
impl WithExitCode for Problem {
fn with_exit_code<ExitCodeT>(self, exit_code: ExitCodeT) -> Self
where
ExitCodeT: Into<ExitCode>,
{
let exit_code: ExitCode = exit_code.into();
self.with(exit_code)
}
fn with_failure_exit_code(self) -> Self {
self.with(ExitCode::FAILURE)
}
fn with_success_exit_code(self) -> Self {
self.with(ExitCode::SUCCESS)
}
}
pub trait WithExitCodeResult<OkT> {
fn with_exit_code<ExitCodeT>(self, exit_code: ExitCodeT) -> Result<OkT, Problem>
where
ExitCodeT: Into<ExitCode>;
fn with_failure_exit_code(self) -> Result<OkT, Problem>;
fn with_success_exit_code(self) -> Result<OkT, Problem>;
}
impl<ResultT, OkT> WithExitCodeResult<OkT> for ResultT
where
ResultT: IntoProblemResult<OkT>,
{
#[track_caller]
fn with_exit_code<ExitCodeT>(self, exit_code: ExitCodeT) -> Result<OkT, Problem>
where
ExitCodeT: Into<ExitCode>,
{
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_exit_code(exit_code)),
}
}
#[track_caller]
fn with_failure_exit_code(self) -> Result<OkT, Problem> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_failure_exit_code()),
}
}
#[track_caller]
fn with_success_exit_code(self) -> Result<OkT, Problem> {
match self.into_problem() {
Ok(ok) => Ok(ok),
Err(problem) => Err(problem.with_success_exit_code()),
}
}
}