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
use crate::parse::ParseError;
use std::ops::Neg;
use thiserror::Error;
#[derive(Debug, Clone, Copy, Error)]
pub enum Error {
#[error("Invalid die")]
InvalidDie,
#[error("sum is too high for `i64`")]
OverflowPositive(#[from] crate::OverflowPositive),
#[error("sum is too low for `i64`")]
OverflowNegative(#[from] crate::OverflowNegative),
#[error("you've specified an invalid dice expression")]
InvalidExpression(#[from] ParseError),
}
macro_rules! impl_zst_neg {
($in:ty => $out:path) => {
impl Neg for $in {
type Output = $out;
fn neg(self) -> Self::Output {
$out
}
}
}
}
impl_zst_neg!(crate::OverflowPositive => crate::OverflowNegative);
impl_zst_neg!(crate::OverflowNegative => crate::OverflowPositive);
impl Neg for Error {
type Output = Self;
fn neg(self) -> Self::Output {
match self {
Error::OverflowPositive(x) => Error::OverflowNegative(-x),
Error::OverflowNegative(x) => Error::OverflowPositive(-x),
x => x,
}
}
}
pub(crate) enum MyResult<T, E> {
Ok(T),
Err(E),
}
impl<T: Neg<Output = T>, E: Neg<Output = E>> Neg for MyResult<T, E> {
type Output = Self;
fn neg(self) -> Self::Output {
match self {
MyResult::Ok(x) => MyResult::Ok(-x),
MyResult::Err(x) => MyResult::Err(-x),
}
}
}
impl<T, E> From<MyResult<T, E>> for Result<T, E> {
fn from(r: MyResult<T, E>) -> Result<T, E> {
match r {
MyResult::Ok(x) => Ok(x),
MyResult::Err(x) => Err(x),
}
}
}
impl<T, J, E, R> From<Result<T, E>> for MyResult<J, R>
where
T: Into<J>,
E: Into<R>,
{
fn from(r: Result<T, E>) -> MyResult<J, R> {
match r {
Ok(x) => MyResult::Ok(x.into()),
Err(x) => MyResult::Err(x.into()),
}
}
}