1
use std::fmt::{Display, Formatter};
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
6
pub enum Constant {
7
    Int(i32),
8
    Bool(bool),
9
}
10

            
11
impl TryFrom<Constant> for i32 {
12
    type Error = &'static str;
13

            
14
    fn try_from(value: Constant) -> Result<Self, Self::Error> {
15
        match value {
16
            Constant::Int(i) => Ok(i),
17
            _ => Err("Cannot convert non-i32 Constant to i32"),
18
        }
19
    }
20
}
21
impl TryFrom<Constant> for bool {
22
    type Error = &'static str;
23

            
24
    fn try_from(value: Constant) -> Result<Self, Self::Error> {
25
        match value {
26
            Constant::Bool(b) => Ok(b),
27
            _ => Err("Cannot convert non-bool Constant to bool"),
28
        }
29
    }
30
}
31

            
32
impl From<i32> for Constant {
33
    fn from(i: i32) -> Self {
34
        Constant::Int(i)
35
    }
36
}
37

            
38
impl From<bool> for Constant {
39
    fn from(b: bool) -> Self {
40
        Constant::Bool(b)
41
    }
42
}
43

            
44
impl Display for Constant {
45
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46
        match &self {
47
            Constant::Int(i) => write!(f, "Int({})", i),
48
            Constant::Bool(b) => write!(f, "Bool({})", b),
49
        }
50
    }
51
}