Skip to main content

dar_core/
value.rs

1// crates/dar-core/src/value.rs
2use std::{fmt, sync::Arc};
3#[derive(Debug, Clone, PartialEq)]
4pub enum Text {
5    Empty,
6    Value(String,),
7}
8impl Text {
9    pub fn to_rust_string(&self,) -> String {
10        match self {
11            Text::Empty => String::new(),
12            Text::Value(s,) => s.clone(),
13        }
14    }
15}
16impl fmt::Display for Text {
17    fn fmt(&self, f: &mut fmt::Formatter<'_,>,) -> fmt::Result {
18        match self {
19            Text::Empty => write!(f, "empty"),
20            Text::Value(s,) => write!(f, "{}", s),
21        }
22    }
23}
24#[derive(Clone)]
25pub enum Value {
26    Text(Text,),
27    NativeFunction(Arc<dyn Fn(Text,) -> Text,>,),
28}
29impl fmt::Debug for Value {
30    fn fmt(&self, f: &mut fmt::Formatter<'_,>,) -> fmt::Result {
31        match self {
32            Value::Text(t,) => write!(f, "Text({:?})", t),
33            Value::NativeFunction(_,) => write!(f, "NativeFunction(...)"),
34        }
35    }
36}
37impl Value {
38    pub fn as_text(&self,) -> Option<&Text,> {
39        if let Value::Text(t,) = self { Some(t,) } else { None }
40    }
41    pub fn expect_text(&self,) -> &Text {
42        self.as_text().expect("Expected Text value",)
43    }
44}