Skip to main content

code_gen/expression/
literal.rs

1use crate::CodeBuffer;
2use crate::Expression;
3
4/// A literal expression.
5#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
6pub struct Literal {
7    value: String,
8}
9
10impl<S: Into<String>> From<S> for Literal {
11    fn from(value: S) -> Self {
12        Self {
13            value: value.into(),
14        }
15    }
16}
17
18impl Expression for Literal {
19    fn write(&self, b: &mut CodeBuffer) {
20        b.write(self.value.as_str());
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn from_str() {
30        let lit = Literal::from("hello");
31        assert_eq!(lit.to_code(), "hello");
32    }
33
34    #[test]
35    fn from_string() {
36        let lit = Literal::from(String::from("world"));
37        assert_eq!(lit.to_code(), "world");
38    }
39}