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
use std::{
    cmp::Ordering,
    fmt,
    ops::{Mul, Neg},
};

use crate::display;

/// Sign representation
///
/// Fractions keep the sign represented by this enum,
/// so that we can use unsigned ints as base data types.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "with-serde-support", derive(Serialize, Deserialize))]
pub enum Sign {
    Plus,
    Minus,
}

impl Sign {
    pub fn is_positive(self) -> bool {
        matches!(self, Sign::Plus)
    }

    pub fn is_negative(self) -> bool {
        matches!(self, Sign::Minus)
    }
}

impl Mul for Sign {
    type Output = Self;

    fn mul(self, oth: Sign) -> Self::Output {
        if self == oth {
            Sign::Plus
        } else {
            Sign::Minus
        }
    }
}

impl Neg for Sign {
    type Output = Self;

    fn neg(self) -> Sign {
        match self {
            Sign::Plus => Sign::Minus,
            Sign::Minus => Sign::Plus,
        }
    }
}

impl PartialOrd for Sign {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Sign {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (Sign::Minus, Sign::Minus) => Ordering::Equal,
            (Sign::Plus, Sign::Minus) => Ordering::Greater,
            (Sign::Minus, Sign::Plus) => Ordering::Less,
            (Sign::Plus, Sign::Plus) => Ordering::Equal,
        }
    }
}

impl fmt::Display for Sign {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let format = display::Format::new(f);
        display::format_sign(*self, f, &format)
    }
}

impl From<Sign> for char {
    fn from(sign: Sign) -> char {
        match sign {
            Sign::Plus => '+',
            Sign::Minus => '-',
        }
    }
}