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
//! ```
//! use num_sign::*;
//! assert_eq!(Positive as i32, 1);
//! assert_eq!(Negative as i32, -1);
//! assert!(Negative < Positive);
//! assert_eq!(Positive.cmp(&Negative), std::cmp::Ordering::Greater);
//! assert_eq!(1_i32, Positive.into());
//! assert_eq!(-1_i32, Negative.into());
//! assert_eq!(1.0, Positive.into());
//! assert_eq!(-1.0, Negative.into());
//! assert_eq!(-Positive, Negative);
//! assert_eq!(Positive, -Negative);
//! assert_eq!(Positive * Positive, Positive);
//! assert_eq!(Positive * Negative, Negative);
//! assert_eq!(Negative * Positive, Negative);
//! assert_eq!(Negative * Negative, Positive);
//! assert_eq!(Positive * 123, 123);
//! assert_eq!(Negative * 123, -123);
//! assert_eq!(Positive * 3.14, 3.14);
//! assert_eq!(Negative * 3.14, -3.14);
//! assert_eq!((123).sign(), Some(Positive));
//! assert_eq!((-123).sign(), Some(Negative));
//! assert_eq!((0).sign(), None);
//! assert_eq!((3.14).sign(), Some(Positive));
//! assert_eq!((-3.14).sign(), Some(Negative));
//! assert_eq!((0.0).sign(), Some(Positive));
//! assert_eq!((-0.0).sign(), Some(Negative));
//! assert_eq!(std::f64::NAN.sign(), None);
//! assert_eq!(std::f64::INFINITY.sign(), Some(Positive));
//! assert_eq!(std::f64::NEG_INFINITY.sign(), Some(Negative));
//! assert_eq!("+1".parse::<Sign>().unwrap(), Positive);
//! assert_eq!("-1".parse::<Sign>().unwrap(), Negative);
//! assert_eq!("Positive".parse::<Sign>().unwrap(), Positive);
//! assert_eq!("Negative".parse::<Sign>().unwrap(), Negative);
//! assert_eq!(Positive.to_string(), "+1");
//! assert_eq!(Negative.to_string(), "-1");
//! ```

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Sign {
    Positive = 1,
    Negative = -1,
}

pub use Sign::*;

impl Sign {
    pub fn to_isize(self) -> isize {
        self.into()
    }
    pub fn to_i64(self) -> i64 {
        self.into()
    }
    pub fn to_i32(self) -> i32 {
        self.into()
    }
    pub fn to_i16(self) -> i16 {
        self.into()
    }
    pub fn to_i8(self) -> i16 {
        self.into()
    }
}

impl std::ops::Neg for Sign {
    type Output = Self;
    fn neg(self) -> Self {
        match self {
            Positive => Negative,
            Negative => Positive,
        }
    }
}

impl<T: std::ops::Neg<Output = T>> std::ops::Mul<T> for Sign {
    type Output = T;
    fn mul(self, rhs: T) -> T {
        match self {
            Positive => rhs,
            Negative => -rhs,
        }
    }
}

impl std::str::FromStr for Sign {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "+1" => Ok(Positive),
            "-1" => Ok(Negative),
            "Positive" => Ok(Positive),
            "Negative" => Ok(Negative),
            _ => Err(()),
        }
    }
}

impl std::fmt::Display for Sign {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Positive => "+1",
            Negative => "-1",
        })
    }
}

pub trait Signed {
    fn sign(&self) -> Option<Sign>;
}

macro_rules! impl_traits_for_int {
    ($($int_type: ty),*) => {
        $(
            impl From<Sign> for $int_type {
                fn from(sign: Sign) -> $int_type {
                    match sign {
                        Positive => 1,
                        Negative => -1,
                    }
                }
            }
            impl Signed for $int_type {
                fn sign(&self) -> Option<Sign> {
                    match self.cmp(&0) {
                        std::cmp::Ordering::Equal => None,
                        std::cmp::Ordering::Greater => Some(Positive),
                        std::cmp::Ordering::Less => Some(Negative),
                    }
                }
            }
        )*
    };
}

impl_traits_for_int!(isize, i64, i32, i16, i8);

impl From<Sign> for f64 {
    fn from(sign: Sign) -> f64 {
        match sign {
            Positive => 1.0,
            Negative => -1.0,
        }
    }
}
impl From<Sign> for f32 {
    fn from(sign: Sign) -> f32 {
        match sign {
            Positive => 1.0,
            Negative => -1.0,
        }
    }
}

impl Signed for f64 {
    fn sign(&self) -> Option<Sign> {
        match self.signum().partial_cmp(&0.0) {
            Some(std::cmp::Ordering::Greater) => Some(Positive),
            Some(std::cmp::Ordering::Less) => Some(Negative),
            _ => None,
        }
    }
}
impl Signed for f32 {
    fn sign(&self) -> Option<Sign> {
        match self.signum().partial_cmp(&0.0) {
            Some(std::cmp::Ordering::Greater) => Some(Positive),
            Some(std::cmp::Ordering::Less) => Some(Negative),
            _ => None,
        }
    }
}

#[cfg(feature = "serde")]
#[test]
fn test_serde() {
    assert_eq!(serde_json::to_string(&Positive).unwrap(), "\"Positive\"");
    assert_eq!(serde_json::to_string(&Negative).unwrap(), "\"Negative\"");
}