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

            
3
use serde::{Deserialize, Serialize};
4
use uniplate::derive::Uniplate;
5
use uniplate::{Biplate, Uniplate};
6

            
7
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate)]
8
#[uniplate()]
9
pub enum Constant {
10
    Int(i32),
11
    Bool(bool),
12
}
13

            
14
impl TryFrom<Constant> for i32 {
15
    type Error = &'static str;
16

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

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

            
35
impl From<i32> for Constant {
36
    fn from(i: i32) -> Self {
37
        Constant::Int(i)
38
    }
39
}
40

            
41
impl From<bool> for Constant {
42
    fn from(b: bool) -> Self {
43
        Constant::Bool(b)
44
    }
45
}
46

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