teo_runtime/value/
option_variant.rs

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
60
61
62
63
64
65
66
67
68
69
use std::ops::{BitAnd, BitOr, BitXor, Not};
use bigdecimal::Zero;
use serde::Serialize;
use teo_result::Result;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OptionVariant {
    pub value: i32,
    pub display: String,
}

impl OptionVariant {

    pub fn into_i32(self) -> i32 {
        self.value
    }

    pub fn normal_not(&self) -> bool {
        self.value.is_zero()
    }
}

impl BitAnd for &OptionVariant {

    type Output = Result<OptionVariant>;

    fn bitand(self, rhs: Self) -> Self::Output {
        Ok(OptionVariant {
            value: self.value & rhs.value,
            display: format!("({} & {})", self.display, rhs.display),
        })
    }
}

impl BitOr for &OptionVariant {

    type Output = Result<OptionVariant>;

    fn bitor(self, rhs: Self) -> Self::Output {
        Ok(OptionVariant {
            value: self.value | rhs.value,
            display: format!("({} | {})", self.display, rhs.display),
        })
    }
}

impl BitXor for &OptionVariant {

    type Output = Result<OptionVariant>;

    fn bitxor(self, rhs: Self) -> Self::Output {
        Ok(OptionVariant {
            value: self.value ^ rhs.value,
            display: format!("({} ^ {})", self.display, rhs.display),
        })
    }
}

impl Not for &OptionVariant {

    type Output = OptionVariant;

    fn not(self) -> Self::Output {
        OptionVariant {
            value: self.value.not(),
            display: format!("~{}", self.display),
        }
    }
}