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, Hash)]
7
#[uniplate()]
8
/// A literal value, equivalent to constants in Conjure.
9
pub enum Literal {
10
    Int(i32),
11
    Bool(bool),
12
}
13

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

            
17
91683
    fn try_from(value: Literal) -> Result<Self, Self::Error> {
18
91683
        match value {
19
91632
            Literal::Int(i) => Ok(i),
20
51
            _ => Err("Cannot convert non-i32 literal to i32"),
21
        }
22
91683
    }
23
}
24

            
25
impl TryFrom<&Literal> for i32 {
26
    type Error = &'static str;
27

            
28
4012
    fn try_from(value: &Literal) -> Result<Self, Self::Error> {
29
4012
        match value {
30
4012
            Literal::Int(i) => Ok(*i),
31
            _ => Err("Cannot convert non-i32 literal to i32"),
32
        }
33
4012
    }
34
}
35

            
36
impl TryFrom<Literal> for bool {
37
    type Error = &'static str;
38

            
39
816
    fn try_from(value: Literal) -> Result<Self, Self::Error> {
40
816
        match value {
41
527
            Literal::Bool(b) => Ok(b),
42
289
            _ => Err("Cannot convert non-bool literal to bool"),
43
        }
44
816
    }
45
}
46

            
47
impl TryFrom<&Literal> for bool {
48
    type Error = &'static str;
49

            
50
    fn try_from(value: &Literal) -> Result<Self, Self::Error> {
51
        match value {
52
            Literal::Bool(b) => Ok(*b),
53
            _ => Err("Cannot convert non-bool literal to bool"),
54
        }
55
    }
56
}
57

            
58
impl From<i32> for Literal {
59
102
    fn from(i: i32) -> Self {
60
102
        Literal::Int(i)
61
102
    }
62
}
63

            
64
impl From<bool> for Literal {
65
    fn from(b: bool) -> Self {
66
        Literal::Bool(b)
67
    }
68
}
69

            
70
impl Display for Literal {
71
43843
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72
43843
        match &self {
73
40205
            Literal::Int(i) => write!(f, "{}", i),
74
3638
            Literal::Bool(b) => write!(f, "{}", b),
75
        }
76
43843
    }
77
}