reqlang_expr/
value.rs

1//! The core value type used in the virtual machine.
2
3use std::{fmt::Display, rc::Rc};
4
5use crate::{builtins::BuiltinFn, types::Type};
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum Value {
9    String(String),
10    Fn(Rc<BuiltinFn>),
11    Bool(bool),
12}
13
14impl Value {
15    pub fn get_type(&self) -> Type {
16        self.clone().into()
17    }
18
19    pub fn get_string(&self) -> &str {
20        match self {
21            Value::String(s) => s.as_str(),
22            _ => panic!("Value is not a string"),
23        }
24    }
25
26    pub fn get_func(&self) -> Rc<BuiltinFn> {
27        match self {
28            Value::Fn(f) => f.clone(),
29            _ => panic!("Value is not a function"),
30        }
31    }
32
33    pub fn get_bool(&self) -> bool {
34        match self {
35            Value::Bool(s) => *s,
36            _ => panic!("Value is not a string"),
37        }
38    }
39}
40
41impl Display for Value {
42    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43        match self {
44            Value::String(string) => write!(f, "`{}`", string),
45            Value::Fn(builtin) => write!(f, "{builtin:?}"),
46            Value::Bool(value) => write!(f, "{}", value),
47        }
48    }
49}