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

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

            
6
#[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
2
    fn try_from(value: Literal) -> Result<Self, Self::Error> {
19
2
        match value {
20
2
            Literal::Int(i) => Ok(i),
21
            _ => Err("Cannot convert non-i32 literal to i32"),
22
        }
23
2
    }
24
}
25
impl TryFrom<Literal> for bool {
26
    type Error = &'static str;
27

            
28
    fn try_from(value: Literal) -> Result<Self, Self::Error> {
29
        match value {
30
            Literal::Bool(b) => Ok(b),
31
            _ => Err("Cannot convert non-bool literal to bool"),
32
        }
33
    }
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
9
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50
9
        match &self {
51
            Literal::Int(i) => write!(f, "{}", i),
52
9
            Literal::Bool(b) => write!(f, "{}", b),
53
        }
54
9
    }
55
}