jiff/util/
c.rs

1/*!
2A module for constants and various base utilities.
3
4This module is a work-in-progress that may lead to helping us move off of
5ranged integers. I'm not quite sure where this will go.
6*/
7
8use crate::util::t;
9
10/// A representation of a numeric sign.
11///
12/// Its `Display` impl emits the ASCII minus sign, `-` when this
13/// is negative. It emits the empty string in all other cases.
14#[derive(
15    Clone, Copy, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord,
16)]
17#[repr(i8)]
18pub(crate) enum Sign {
19    #[default]
20    Zero = 0,
21    Positive = 1,
22    Negative = -1,
23}
24
25impl Sign {
26    /*
27    pub(crate) fn is_zero(&self) -> bool {
28        matches!(*self, Sign::Zero)
29    }
30
31    pub(crate) fn is_positive(&self) -> bool {
32        matches!(*self, Sign::Positive)
33    }
34    */
35
36    pub(crate) fn is_negative(&self) -> bool {
37        matches!(*self, Sign::Negative)
38    }
39
40    pub(crate) fn as_ranged_integer(&self) -> t::Sign {
41        match *self {
42            Sign::Zero => t::Sign::N::<0>(),
43            Sign::Positive => t::Sign::N::<1>(),
44            Sign::Negative => t::Sign::N::<-1>(),
45        }
46    }
47}
48
49impl core::fmt::Display for Sign {
50    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
51        if self.is_negative() {
52            write!(f, "-")
53        } else {
54            Ok(())
55        }
56    }
57}