| 1 |
use std::fmt::Display; |
| 2 |
|
| 3 |
use crate::Type; |
| 4 |
|
| 5 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 6 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 7 |
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
| 8 |
pub struct Result_ { |
| 9 |
ok: Option<Type>, |
| 10 |
err: Option<Type>, |
| 11 |
} |
| 12 |
|
| 13 |
impl Result_ { |
| 14 |
pub fn ok(type_: Type) -> Self { |
| 15 |
Self { |
| 16 |
ok: Some(type_), |
| 17 |
err: None, |
| 18 |
} |
| 19 |
} |
| 20 |
pub fn get_ok(&self) -> &Option<Type> { |
| 21 |
&self.ok |
| 22 |
} |
| 23 |
pub fn get_ok_mut(&mut self) -> &mut Option<Type> { |
| 24 |
&mut self.ok |
| 25 |
} |
| 26 |
pub fn err(type_: Type) -> Self { |
| 27 |
Self { |
| 28 |
ok: None, |
| 29 |
err: Some(type_), |
| 30 |
} |
| 31 |
} |
| 32 |
pub fn get_err(&self) -> &Option<Type> { |
| 33 |
&self.err |
| 34 |
} |
| 35 |
pub fn get_err_mut(&mut self) -> &mut Option<Type> { |
| 36 |
&mut self.err |
| 37 |
} |
| 38 |
pub fn both(ok: Type, err: Type) -> Self { |
| 39 |
Self { |
| 40 |
ok: Some(ok), |
| 41 |
err: Some(err), |
| 42 |
} |
| 43 |
} |
| 44 |
pub fn empty() -> Self { |
| 45 |
Self { |
| 46 |
ok: None, |
| 47 |
err: None, |
| 48 |
} |
| 49 |
} |
| 50 |
pub fn is_empty(&self) -> bool { |
| 51 |
self.ok.is_none() && self.err.is_none() |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl Display for Result_ { |
| 56 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 57 |
write!(f, "result")?; |
| 58 |
if !self.is_empty() { |
| 59 |
write!(f, "<")?; |
| 60 |
if let Some(type_) = &self.ok { |
| 61 |
type_.fmt(f)?; |
| 62 |
} else { |
| 63 |
write!(f, "_")?; |
| 64 |
} |
| 65 |
if let Some(type_) = &self.err { |
| 66 |
write!(f, ", ")?; |
| 67 |
type_.fmt(f)?; |
| 68 |
} |
| 69 |
write!(f, ">")?; |
| 70 |
} |
| 71 |
Ok(()) |
| 72 |
} |
| 73 |
} |