ruby_marshal/value_arena/
value.rs

1use crate::TypedValueHandle;
2use crate::ValueHandle;
3
4/// A Ruby Value
5#[derive(Debug)]
6pub enum Value {
7    /// Nil
8    Nil(NilValue),
9
10    /// A Bool
11    Bool(BoolValue),
12
13    /// A Fixnum
14    Fixnum(FixnumValue),
15
16    /// A Symbol
17    Symbol(SymbolValue),
18
19    /// An Array
20    Array(ArrayValue),
21
22    /// A hash value
23    Hash(HashValue),
24
25    /// An Object
26    Object(ObjectValue),
27
28    /// A String
29    String(StringValue),
30
31    /// A User Defined Value
32    UserDefined(UserDefinedValue),
33}
34
35impl Value {
36    /// Get a ref to the [`SymbolValue`], if it is a symbol.
37    pub fn as_symbol(&self) -> Option<&SymbolValue> {
38        match self {
39            Self::Symbol(value) => Some(value),
40            _ => None,
41        }
42    }
43
44    /// Get a ref to the [`ObjectValue`], if it is an object.
45    pub fn as_object(&self) -> Option<&ObjectValue> {
46        match self {
47            Self::Object(value) => Some(value),
48            _ => None,
49        }
50    }
51
52    /// Get a ref to the [`StringValue`], if it is a string.
53    pub fn as_string(&self) -> Option<&StringValue> {
54        match self {
55            Self::String(value) => Some(value),
56            _ => None,
57        }
58    }
59
60    /// Get the kind of value.
61    pub fn kind(&self) -> ValueKind {
62        match self {
63            Self::Nil(_) => ValueKind::Nil,
64            Self::Bool(_) => ValueKind::Bool,
65            Self::Fixnum(_) => ValueKind::Fixnum,
66            Self::Symbol(_) => ValueKind::Symbol,
67            Self::Array(_) => ValueKind::Array,
68            Self::Hash(_) => ValueKind::Hash,
69            Self::Object(_) => ValueKind::Object,
70            Self::String(_) => ValueKind::String,
71            Self::UserDefined(_) => ValueKind::UserDefined,
72        }
73    }
74}
75
76impl From<NilValue> for Value {
77    fn from(value: NilValue) -> Self {
78        Self::Nil(value)
79    }
80}
81
82impl From<BoolValue> for Value {
83    fn from(value: BoolValue) -> Self {
84        Self::Bool(value)
85    }
86}
87
88impl From<FixnumValue> for Value {
89    fn from(value: FixnumValue) -> Self {
90        Self::Fixnum(value)
91    }
92}
93
94impl From<SymbolValue> for Value {
95    fn from(value: SymbolValue) -> Self {
96        Self::Symbol(value)
97    }
98}
99
100impl From<ArrayValue> for Value {
101    fn from(value: ArrayValue) -> Self {
102        Self::Array(value)
103    }
104}
105
106impl From<HashValue> for Value {
107    fn from(value: HashValue) -> Self {
108        Self::Hash(value)
109    }
110}
111
112impl From<ObjectValue> for Value {
113    fn from(value: ObjectValue) -> Self {
114        Self::Object(value)
115    }
116}
117
118impl From<StringValue> for Value {
119    fn from(value: StringValue) -> Self {
120        Self::String(value)
121    }
122}
123
124impl From<UserDefinedValue> for Value {
125    fn from(value: UserDefinedValue) -> Self {
126        Self::UserDefined(value)
127    }
128}
129
130/// A Nil value.
131#[derive(Debug)]
132pub struct NilValue;
133
134/// A bool value.
135#[derive(Debug, Copy, Clone)]
136pub struct BoolValue {
137    value: bool,
138}
139
140impl BoolValue {
141    /// Create a new [`BoolValue`].
142    pub(super) fn new(value: bool) -> Self {
143        Self { value }
144    }
145
146    /// Get the inner value
147    pub fn value(self) -> bool {
148        self.value
149    }
150}
151
152/// A Fixnum Value
153#[derive(Debug, Copy, Clone)]
154pub struct FixnumValue {
155    value: i32,
156}
157
158impl FixnumValue {
159    /// Create a new [`FixnumValue`].
160    pub(super) fn new(value: i32) -> Self {
161        Self { value }
162    }
163
164    /// Get the inner value
165    pub fn value(self) -> i32 {
166        self.value
167    }
168}
169
170/// A Symbol
171#[derive(Debug)]
172pub struct SymbolValue {
173    value: Vec<u8>,
174}
175
176impl SymbolValue {
177    /// Create a new [`SymbolValue`].
178    pub(super) fn new(value: Vec<u8>) -> Self {
179        Self { value }
180    }
181
182    /// Get the inner value.
183    pub fn value(&self) -> &[u8] {
184        &self.value
185    }
186}
187
188/// An Array
189#[derive(Debug)]
190pub struct ArrayValue {
191    value: Vec<ValueHandle>,
192}
193
194impl ArrayValue {
195    /// Create a new [`Array`].
196    pub(crate) fn new(value: Vec<ValueHandle>) -> Self {
197        Self { value }
198    }
199
200    /// Get the inner value.
201    pub fn value(&self) -> &[ValueHandle] {
202        &self.value
203    }
204
205    /// Get the number of elements in the array
206    pub fn len(&self) -> usize {
207        self.value.len()
208    }
209
210    /// Check if this is empty
211    pub fn is_empty(&self) -> bool {
212        self.value.is_empty()
213    }
214}
215
216/// A Hash
217#[derive(Debug)]
218pub struct HashValue {
219    value: Vec<(ValueHandle, ValueHandle)>,
220    default_value: Option<ValueHandle>,
221}
222
223impl HashValue {
224    /// Create a new [`HashValue`].
225    pub(crate) fn new(
226        value: Vec<(ValueHandle, ValueHandle)>,
227        default_value: Option<ValueHandle>,
228    ) -> Self {
229        Self {
230            value,
231            default_value,
232        }
233    }
234
235    /// Get the inner value.
236    pub fn value(&self) -> &[(ValueHandle, ValueHandle)] {
237        &self.value
238    }
239
240    /// Get the default value.
241    pub fn default_value(&self) -> Option<ValueHandle> {
242        self.default_value
243    }
244}
245
246/// An object
247#[derive(Debug)]
248pub struct ObjectValue {
249    name: TypedValueHandle<SymbolValue>,
250    instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
251}
252
253impl ObjectValue {
254    /// Create a new [`ObjectValue`].
255    pub(crate) fn new(
256        name: TypedValueHandle<SymbolValue>,
257        instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
258    ) -> Self {
259        Self {
260            name,
261            instance_variables,
262        }
263    }
264
265    /// Get the name.
266    pub fn name(&self) -> TypedValueHandle<SymbolValue> {
267        self.name
268    }
269
270    /// Get the instance variables
271    pub fn instance_variables(&self) -> &[(TypedValueHandle<SymbolValue>, ValueHandle)] {
272        &self.instance_variables
273    }
274}
275
276/// A String
277#[derive(Debug)]
278pub struct StringValue {
279    value: Vec<u8>,
280    instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
281}
282
283impl StringValue {
284    /// Create a new [`String`].
285    pub(crate) fn new(value: Vec<u8>) -> Self {
286        Self {
287            value,
288            instance_variables: None,
289        }
290    }
291
292    /// Get the inner value.
293    pub fn value(&self) -> &[u8] {
294        &self.value
295    }
296
297    /// Get the instance variables
298    pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
299        self.instance_variables.as_deref()
300    }
301
302    /// Set the instance variables.
303    ///
304    /// # Returns
305    /// Returns the old instance variables
306    pub(crate) fn set_instance_variables(
307        &mut self,
308        mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
309    ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
310        std::mem::swap(&mut self.instance_variables, &mut instance_variables);
311        instance_variables
312    }
313}
314
315/// A User Defined value
316#[derive(Debug)]
317pub struct UserDefinedValue {
318    name: TypedValueHandle<SymbolValue>,
319    value: Vec<u8>,
320    instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
321}
322
323impl UserDefinedValue {
324    /// Create a new [`UserDefinedValue`].
325    pub(crate) fn new(name: TypedValueHandle<SymbolValue>, value: Vec<u8>) -> Self {
326        Self {
327            name,
328            value,
329            instance_variables: None,
330        }
331    }
332
333    /// Get the name.
334    pub fn name(&self) -> TypedValueHandle<SymbolValue> {
335        self.name
336    }
337
338    /// Get the inner value.
339    pub fn value(&self) -> &[u8] {
340        &self.value
341    }
342
343    /// Get the instance variables
344    pub fn instance_variables(&self) -> Option<&[(TypedValueHandle<SymbolValue>, ValueHandle)]> {
345        self.instance_variables.as_deref()
346    }
347
348    /// Set the instance variables.
349    ///
350    /// # Returns
351    /// Returns the old instance variables
352    pub(crate) fn set_instance_variables(
353        &mut self,
354        mut instance_variables: Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>>,
355    ) -> Option<Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>> {
356        std::mem::swap(&mut self.instance_variables, &mut instance_variables);
357        instance_variables
358    }
359}
360
361/// The kind of value
362#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
363pub enum ValueKind {
364    Nil,
365    Bool,
366    Fixnum,
367    Symbol,
368    Array,
369    Hash,
370    Object,
371    String,
372    UserDefined,
373}