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
use crate::Error;
use std::fmt;

/// A constant value.
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum Constant {
    /// The unit constant (always has constant id = 0).
    Unit,
    /// A boolean constant.
    Bool(bool),
    /// A character constant.
    Char(char),
    /// A byte constant.
    Byte(u8),
    /// An integer constant.
    Integer(i64),
    /// A float constant.
    Float(ordered_float::NotNan<f64>),
    /// A string constant.
    String(Box<str>),
    /// A byte constant.
    Bytes(Box<[u8]>),
}

impl Constant {
    /// Construct a float constant and error if it can't be constructed.
    pub fn float(f: f64) -> Result<Self, Error> {
        let f = ordered_float::NotNan::new(f).map_err(|_| Error::FloatIsNan)?;
        Ok(Self::Float(f))
    }
}

impl fmt::Debug for Constant {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Constant::Unit => {
                write!(f, "()")?;
            }
            Constant::Bool(b) => {
                write!(f, "{}", b)?;
            }
            Constant::Char(c) => {
                write!(f, "{:?}", c)?;
            }
            Constant::Byte(b) => {
                write!(f, "0x{:02x}", b)?;
            }
            Constant::Integer(n) => {
                write!(f, "{}", n)?;
            }
            Constant::Float(n) => {
                write!(f, "{}", n.into_inner())?;
            }
            Constant::String(s) => {
                write!(f, "{:?}", s)?;
            }
            Constant::Bytes(b) => {
                write!(f, "{:?}", b)?;
            }
        }

        Ok(())
    }
}