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

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

            
6
858
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate)]
7
#[uniplate()]
8

            
9
/// A literal value, equivalent to constants in Conjure.
10
pub enum Literal {
11
    Int(i32),
12
    Bool(bool),
13
}
14

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

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

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

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

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

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