1use core::{
2 convert::Infallible,
3 fmt::{self, Display, Formatter},
4 num::NonZeroU8,
5};
6
7pub const USAGE_STATUS: NonZeroU8 = NonZeroU8::new(2).unwrap();
10
11#[derive(Clone, Debug, PartialEq, Eq)]
16pub enum CliOutcome<Value, Error> {
17 Success(Value),
19 Failure(Error),
21}
22
23pub trait CliFailureAlg {
28 fn cli_status(&self) -> NonZeroU8;
30
31 fn cli_reported(&self) -> bool {
35 false
36 }
37}
38
39impl CliFailureAlg for Infallible {
41 fn cli_status(&self) -> NonZeroU8 {
42 match *self {}
43 }
44}
45
46impl<Value, Error> CliOutcome<Value, Error>
47where
48 Error: CliFailureAlg,
49{
50 pub fn cli_status(&self) -> u8 {
52 match self {
53 Self::Success(_) => 0,
54 Self::Failure(error) => error.cli_status().get(),
55 }
56 }
57}
58
59#[derive(Debug, Default)]
61pub struct Total;
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub struct Fallible {
68 status: NonZeroU8,
69}
70
71impl Fallible {
72 #[must_use]
78 #[track_caller]
79 pub const fn new(status: u8) -> Self {
80 match NonZeroU8::new(status) {
81 Some(status) => Self { status },
82 None => panic!("a failure exits with a nonzero status"),
83 }
84 }
85}
86
87impl Default for Fallible {
89 fn default() -> Self {
90 Self { status: NonZeroU8::MIN }
91 }
92}
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
97pub struct FallibleByError;
98
99#[derive(Clone, Debug, PartialEq, Eq)]
104pub struct CliFailure<Error> {
105 pub error: Error,
107 status: NonZeroU8,
108}
109
110impl<Error> CliFailureAlg for CliFailure<Error> {
111 fn cli_status(&self) -> NonZeroU8 {
112 self.status
113 }
114}
115
116impl<Error> Display for CliFailure<Error>
117where
118 Error: Display,
119{
120 fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
121 self.error.fmt(formatter)
122 }
123}
124
125#[diagnostic::on_unimplemented(
127 message = "`{Self}` cannot read the output `{Output}` as a command outcome",
128 note = "a `.fallible()`, `.fallible_as(..)`, or `.fallible_by_error()` command applies an operation whose output is a `Result`"
129)]
130pub trait CliOutcomeOf<Output> {
131 type Value;
133 type Error;
135
136 fn cli_outcome(&self, output: Output) -> CliOutcome<Self::Value, Self::Error>;
138}
139
140impl<Output> CliOutcomeOf<Output> for Total {
141 type Value = Output;
142 type Error = Infallible;
143
144 fn cli_outcome(&self, output: Output) -> CliOutcome<Output, Infallible> {
145 CliOutcome::Success(output)
146 }
147}
148
149impl<Value, Error> CliOutcomeOf<Result<Value, Error>> for Fallible {
150 type Value = Value;
151 type Error = CliFailure<Error>;
152
153 fn cli_outcome(&self, output: Result<Value, Error>) -> CliOutcome<Value, CliFailure<Error>> {
154 match output {
155 Ok(value) => CliOutcome::Success(value),
156 Err(error) => CliOutcome::Failure(CliFailure { error, status: self.status }),
157 }
158 }
159}
160
161impl<Value, Error> CliOutcomeOf<Result<Value, Error>> for FallibleByError {
162 type Value = Value;
163 type Error = Error;
164
165 fn cli_outcome(&self, output: Result<Value, Error>) -> CliOutcome<Value, Error> {
166 match output {
167 Ok(value) => CliOutcome::Success(value),
168 Err(error) => CliOutcome::Failure(error),
169 }
170 }
171}