conjure_core/ast/
atom.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::borrow::Borrow;

use crate::metadata::Metadata;

use super::{Expression, Literal, Name};
use serde::{Deserialize, Serialize};
use uniplate::derive::Uniplate;

/// An `Atom` is an indivisible expression, such as a literal or a reference.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate)]
#[uniplate()]
#[biplate(to=Name)]
#[biplate(to=Literal)]
#[biplate(to=Metadata)]
#[biplate(to=Expression)]
pub enum Atom {
    Literal(Literal),
    Reference(Name),
}

impl std::fmt::Display for Atom {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Atom::Literal(x) => x.fmt(f),
            Atom::Reference(x) => x.fmt(f),
        }
    }
}

impl From<Literal> for Atom {
    fn from(value: Literal) -> Self {
        Atom::Literal(value)
    }
}

impl From<Name> for Atom {
    fn from(value: Name) -> Self {
        Atom::Reference(value)
    }
}

impl From<i32> for Atom {
    fn from(value: i32) -> Self {
        Atom::Literal(value.into())
    }
}

impl From<bool> for Atom {
    fn from(value: bool) -> Self {
        Atom::Literal(value.into())
    }
}

impl TryFrom<Expression> for Atom {
    type Error = &'static str;

    fn try_from(value: Expression) -> Result<Self, Self::Error> {
        match value {
            Expression::Atomic(_, atom) => Ok(atom),
            _ => Err("Cannot convert non-atomic expression to Atom"),
        }
    }
}

impl<'a> TryFrom<&'a Expression> for &'a Atom {
    type Error = &'static str;

    fn try_from(value: &'a Expression) -> Result<Self, Self::Error> {
        match value {
            Expression::Atomic(_, atom) => Ok(atom),
            _ => Err("Cannot convert non-atomic expression to Atom"),
        }
    }
}

impl TryFrom<Box<Expression>> for Atom {
    type Error = &'static str;

    fn try_from(value: Box<Expression>) -> Result<Self, Self::Error> {
        (*value).try_into()
    }
}

impl<'a> TryFrom<&'a Box<Expression>> for &'a Atom {
    type Error = &'static str;

    fn try_from(value: &'a Box<Expression>) -> Result<Self, Self::Error> {
        let expr: &'a Expression = value.borrow();
        expr.try_into()
    }
}

impl TryFrom<Atom> for Literal {
    type Error = &'static str;

    fn try_from(value: Atom) -> Result<Self, Self::Error> {
        match value {
            Atom::Literal(l) => Ok(l),
            _ => Err("Cannot convert non-literal atom to Literal"),
        }
    }
}

impl<'a> TryFrom<&'a Atom> for &'a Literal {
    type Error = &'static str;

    fn try_from(value: &'a Atom) -> Result<Self, Self::Error> {
        match value {
            Atom::Literal(l) => Ok(l),
            _ => Err("Cannot convert non-literal atom to Literal"),
        }
    }
}

impl TryFrom<Atom> for Name {
    type Error = &'static str;

    fn try_from(value: Atom) -> Result<Self, Self::Error> {
        match value {
            Atom::Reference(n) => Ok(n),
            _ => Err("Cannot convert non-reference atom to Name"),
        }
    }
}

impl<'a> TryFrom<&'a Atom> for &'a Name {
    type Error = &'static str;

    fn try_from(value: &'a Atom) -> Result<Self, Self::Error> {
        match value {
            Atom::Reference(n) => Ok(n),
            _ => Err("Cannot convert non-reference atom to Name"),
        }
    }
}

impl TryFrom<Atom> for i32 {
    type Error = &'static str;

    fn try_from(value: Atom) -> Result<Self, Self::Error> {
        let lit: Literal = value.try_into()?;
        lit.try_into()
    }
}

impl TryFrom<&Atom> for i32 {
    type Error = &'static str;

    fn try_from(value: &Atom) -> Result<Self, Self::Error> {
        let lit: &Literal = value.try_into()?;
        lit.try_into()
    }
}

impl TryFrom<Atom> for bool {
    type Error = &'static str;

    fn try_from(value: Atom) -> Result<Self, Self::Error> {
        let lit: Literal = value.try_into()?;
        lit.try_into()
    }
}

impl TryFrom<&Atom> for bool {
    type Error = &'static str;

    fn try_from(value: &Atom) -> Result<Self, Self::Error> {
        let lit: &Literal = value.try_into()?;
        lit.try_into()
    }
}