1use bumpalo::Bump;
8
9use crate::value::{Map, Ref, Res, Value};
10
11#[derive(Debug, Clone, Copy, PartialEq)]
13pub enum ValueRef<'a> {
14 Null,
15 Bool(bool),
16 Int(i64),
17 Float(f64),
18 Str(&'a str),
19 List(&'a [ValueRef<'a>]),
20 Map(MapRef<'a>),
21 Ref { kind: &'a str, id: &'a str },
23 Tag { kind: &'a str, id: &'a str },
25 Res { set: &'a str, key: &'a str },
27 Lang(&'a str),
29 Text(&'a str),
31 Image(&'a str),
33 Color(u32),
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Default)]
39pub struct MapRef<'a>(pub &'a [(&'a str, ValueRef<'a>)]);
40
41impl<'a> MapRef<'a> {
42 pub fn get(&self, key: &str) -> Option<&'a ValueRef<'a>> {
43 self.0.iter().find(|(k, _)| *k == key).map(|(_, v)| v)
44 }
45
46 pub fn iter(&self) -> impl ExactSizeIterator<Item = (&'a str, &'a ValueRef<'a>)> + 'a {
47 self.0.iter().map(|(k, v)| (*k, v))
48 }
49
50 pub fn keys(&self) -> impl ExactSizeIterator<Item = &'a str> + 'a {
51 self.0.iter().map(|(k, _)| *k)
52 }
53
54 pub fn len(&self) -> usize {
55 self.0.len()
56 }
57
58 pub fn is_empty(&self) -> bool {
59 self.0.is_empty()
60 }
61}
62
63impl<'a> ValueRef<'a> {
64 pub fn kind_name(&self) -> &'static str {
66 match self {
67 ValueRef::Null => "null",
68 ValueRef::Bool(_) => "true/false",
69 ValueRef::Int(_) => "an integer",
70 ValueRef::Float(_) => "a number",
71 ValueRef::Str(_) => "a string",
72 ValueRef::List(_) => "a list",
73 ValueRef::Map(_) => "an object",
74 ValueRef::Ref { .. } => "a reference",
75 ValueRef::Tag { .. } => "a tag",
76 ValueRef::Res { .. } => "a resource",
77 ValueRef::Lang(_) => "a translation key",
78 ValueRef::Text(_) => "a text",
79 ValueRef::Image(_) => "an image",
80 ValueRef::Color(_) => "a color",
81 }
82 }
83
84 pub fn is_null(&self) -> bool {
85 matches!(self, ValueRef::Null)
86 }
87
88 pub fn as_bool(&self) -> Option<bool> {
89 match self {
90 ValueRef::Bool(b) => Some(*b),
91 _ => None,
92 }
93 }
94
95 pub fn as_i64(&self) -> Option<i64> {
96 match self {
97 ValueRef::Int(n) => Some(*n),
98 ValueRef::Float(f) if f.fract() == 0.0 && f.abs() < 9.0e15 => Some(*f as i64),
99 _ => None,
100 }
101 }
102
103 pub fn as_f64(&self) -> Option<f64> {
104 match self {
105 ValueRef::Int(n) => Some(*n as f64),
106 ValueRef::Float(f) => Some(*f),
107 _ => None,
108 }
109 }
110
111 pub fn as_str(&self) -> Option<&'a str> {
113 match self {
114 ValueRef::Str(s) | ValueRef::Text(s) | ValueRef::Image(s) => Some(s),
115 _ => None,
116 }
117 }
118
119 pub fn as_list(&self) -> Option<&'a [ValueRef<'a>]> {
120 match self {
121 ValueRef::List(l) => Some(l),
122 _ => None,
123 }
124 }
125
126 pub fn as_map(&self) -> Option<MapRef<'a>> {
127 match self {
128 ValueRef::Map(m) => Some(*m),
129 _ => None,
130 }
131 }
132
133 pub fn get(&self, key: &str) -> Option<&'a ValueRef<'a>> {
135 self.as_map()?.get(key)
136 }
137
138 pub fn to_value(&self) -> Value {
140 match *self {
141 ValueRef::Null => Value::Null,
142 ValueRef::Bool(b) => Value::Bool(b),
143 ValueRef::Int(n) => Value::Int(n),
144 ValueRef::Float(f) => Value::Float(f),
145 ValueRef::Str(s) => Value::Str(s.to_string()),
146 ValueRef::List(l) => Value::List(l.iter().map(ValueRef::to_value).collect()),
147 ValueRef::Map(m) => {
148 let mut out = Map::with_capacity(m.len());
149 for (k, v) in m.iter() {
150 out.insert(k, v.to_value());
151 }
152 Value::Map(out)
153 }
154 ValueRef::Ref { kind, id } => Value::Ref(Ref::new(kind, id)),
155 ValueRef::Tag { kind, id } => Value::Tag(Ref::new(kind, id)),
156 ValueRef::Res { set, key } => Value::Res(Res { set: set.to_string(), key: key.to_string() }),
157 ValueRef::Lang(k) => Value::Lang(k.to_string()),
158 ValueRef::Text(s) => Value::Text(s.to_string()),
159 ValueRef::Image(s) => Value::Image(s.to_string()),
160 ValueRef::Color(c) => Value::Color(c),
161 }
162 }
163
164 pub fn to_ref(&self) -> Option<Ref> {
166 match *self {
167 ValueRef::Ref { kind, id } => Some(Ref::new(kind, id)),
168 _ => None,
169 }
170 }
171}
172
173impl Value {
174 pub fn borrow<'a>(&'a self, arena: &'a Bump) -> ValueRef<'a> {
176 match self {
177 Value::Null => ValueRef::Null,
178 Value::Bool(b) => ValueRef::Bool(*b),
179 Value::Int(n) => ValueRef::Int(*n),
180 Value::Float(f) => ValueRef::Float(*f),
181 Value::Str(s) => ValueRef::Str(s),
182 Value::List(l) => ValueRef::List(arena.alloc_slice_fill_iter(l.iter().map(|v| v.borrow(arena)))),
183 Value::Map(m) => ValueRef::Map(MapRef(arena.alloc_slice_fill_iter(m.iter().map(|(k, v)| (k, v.borrow(arena)))))),
184 Value::Ref(r) => ValueRef::Ref { kind: &r.kind, id: &r.id },
185 Value::Tag(r) => ValueRef::Tag { kind: &r.kind, id: &r.id },
186 Value::Res(r) => ValueRef::Res { set: &r.set, key: &r.key },
187 Value::Lang(k) => ValueRef::Lang(k),
188 Value::Text(s) => ValueRef::Text(s),
189 Value::Image(s) => ValueRef::Image(s),
190 Value::Color(c) => ValueRef::Color(*c),
191 }
192 }
193}