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;
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum Constant {
Unit,
Bool(bool),
Char(char),
Byte(u8),
Integer(i64),
Float(ordered_float::NotNan<f64>),
String(Box<str>),
Bytes(Box<[u8]>),
}
impl Constant {
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(())
}
}