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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use super::{clean_input, ArithmeticError, BasicError, Calcable};

pub type Error = BasicError<i64, std::num::ParseIntError>;
pub type Result = std::result::Result<i64, Error>;

fn as_u32(n: i64) -> std::result::Result<u32, Error> {
    if n > (u32::MAX as i64) {
        Err(Error::Arithmetic(ArithmeticError::Overflow))
    } else if n < 0 {
        Err(Error::Arithmetic(ArithmeticError::Underflow))
    } else {
        Ok(n as u32)
    }
}

impl Calcable for i64 {
    type Err = Error;

    const E: Option<Self> = None;
    const PI: Option<Self> = None;

    fn parse_binary(s: &str) -> Result {
        i64::from_str_radix(&clean_input(s, "0b"), 2).map_err(Error::Parse)
    }

    fn parse_octal(s: &str) -> Result {
        i64::from_str_radix(&clean_input(s, "0o"), 8).map_err(Error::Parse)
    }

    fn parse_decimal(s: &str) -> Result {
        i64::from_str_radix(&clean_input(s, "0d"), 10).map_err(Error::Parse)
    }

    fn parse_hex(s: &str) -> Result {
        i64::from_str_radix(&clean_input(s, "0x"), 16).map_err(Error::Parse)
    }

    fn from_f32(f: f32) -> Option<Self> {
        Some(f as Self)
    }

    fn neg(self) -> Option<Self> {
        Some(-self)
    }

    fn not(self) -> Option<Self> {
        Some(!self)
    }

    fn add(self, other: Self) -> Result {
        self.checked_add(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::Overflow))
    }

    fn sub(self, other: Self) -> Result {
        self.checked_sub(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::Underflow))
    }

    fn mul(self, other: Self) -> Result {
        self.checked_mul(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::Overflow))
    }

    fn div(self, other: Self) -> Result {
        self.checked_div(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::DivideBy0))
    }

    fn trunc_div(self, other: Self) -> Result {
        self.div(other)
    }

    fn pow(self, other: Self) -> Result {
        let other = as_u32(other)?;
        self.checked_pow(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::Overflow))
    }

    fn rem(self, other: Self) -> Result {
        self.checked_rem(other)
            .ok_or(BasicError::Arithmetic(ArithmeticError::DivideBy0))
    }

    fn shl(self, other: Self) -> Result {
        Ok(self << other)
    }

    fn shr(self, other: Self) -> Result {
        Ok(self >> other)
    }

    fn rotate_left(self, other: Self) -> Result {
        Ok(self.rotate_left(as_u32(other)?))
    }

    fn rotate_right(self, other: Self) -> Result {
        Ok(self.rotate_right(as_u32(other)?))
    }

    fn bit_and(self, other: Self) -> Option<Self> {
        Some(self & other)
    }

    fn bit_or(self, other: Self) -> Option<Self> {
        Some(self | other)
    }

    fn bit_xor(self, other: Self) -> Option<Self> {
        Some(self ^ other)
    }

    fn abs(self) -> Option<Self> {
        Some(self.abs())
    }

    fn ceil(self) -> Option<Self> {
        Some(self)
    }

    fn floor(self) -> Option<Self> {
        Some(self)
    }

    fn round(self) -> Option<Self> {
        Some(self)
    }

    fn sin(self) -> Option<Self> {
        None
    }

    fn cos(self) -> Option<Self> {
        None
    }

    fn tan(self) -> Option<Self> {
        None
    }

    fn sinh(self) -> Option<Self> {
        None
    }

    fn cosh(self) -> Option<Self> {
        None
    }

    fn tanh(self) -> Option<Self> {
        None
    }

    fn asin(self) -> Option<Self> {
        None
    }

    fn acos(self) -> Option<Self> {
        None
    }

    fn atan(self) -> Option<Self> {
        None
    }

    fn asinh(self) -> Option<Self> {
        None
    }

    fn acosh(self) -> Option<Self> {
        None
    }

    fn atanh(self) -> Option<Self> {
        None
    }

    fn sqrt(self) -> Option<Self> {
        None
    }

    fn cbrt(self) -> Option<Self> {
        None
    }

    fn ln(self) -> Option<Self> {
        None
    }
}