1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::Display;

use crate::Type;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Result_ {
    ok: Option<Type>,
    err: Option<Type>,
}

impl Result_ {
    pub fn ok(type_: Type) -> Self {
        Self {
            ok: Some(type_),
            err: None,
        }
    }
    pub fn err(type_: Type) -> Self {
        Self {
            ok: None,
            err: Some(type_),
        }
    }
    pub fn both(ok: Type, err: Type) -> Self {
        Self {
            ok: Some(ok),
            err: Some(err),
        }
    }
    pub fn empty() -> Self {
        Self {
            ok: None,
            err: None,
        }
    }
    pub fn is_empty(&self) -> bool {
        self.ok.is_none() && self.err.is_none()
    }
}

impl Display for Result_ {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "result")?;
        if !self.is_empty() {
            write!(f, "<")?;
            if let Some(type_) = &self.ok {
                type_.fmt(f)?;
            } else {
                write!(f, "_")?;
            }
            if let Some(type_) = &self.err {
                write!(f, ", ")?;
                type_.fmt(f)?;
            }
            write!(f, ">")?;
        }
        Ok(())
    }
}