1
use crate::metadata::Metadata;
2

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

            
7
/// A `Factor` is an indivisible expression, such as a literal or a reference.
8
1775
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Uniplate)]
9
#[uniplate()]
10
#[biplate(to=Name)]
11
#[biplate(to=Literal)]
12
#[biplate(to=Metadata)]
13
#[biplate(to=Expression)]
14
pub enum Factor {
15
    Literal(Literal),
16
    Reference(Name),
17
}
18

            
19
impl std::fmt::Display for Factor {
20
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21
        match self {
22
            Factor::Literal(x) => x.fmt(f),
23
            Factor::Reference(Name::UserName(n)) => {
24
                write!(f, "{n}")?;
25
                Ok(())
26
            }
27

            
28
            Factor::Reference(Name::MachineName(x)) => {
29
                write!(f, "__{x}")?;
30
                Ok(())
31
            }
32
        }
33
    }
34
}
35

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

            
42
impl From<Name> for Factor {
43
    fn from(value: Name) -> Self {
44
        Factor::Reference(value)
45
    }
46
}