| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use super::{FromValueError, Node, Result}; |
| 6 |
use std::{collections::HashMap, fmt}; |
| 7 |
|
| 8 |
/// Represents the value of a node |
| 9 |
/// |
| 10 |
/// This is a loosely typed data structure similar to `serde_json::Value`. |
| 11 |
/// Nodes can be converted to/from `Value` which is what allows nodes to automatically be converted |
| 12 |
/// from one IR to another. |
| 13 |
pub enum Value { |
| 14 |
UInt8(u8), |
| 15 |
Int8(i8), |
| 16 |
UInt16(u16), |
| 17 |
Int16(i16), |
| 18 |
UInt32(u32), |
| 19 |
Int32(i32), |
| 20 |
UInt64(u64), |
| 21 |
Int64(i64), |
| 22 |
Float32(f32), |
| 23 |
Float64(f64), |
| 24 |
String(String), |
| 25 |
Bool(bool), |
| 26 |
Option(Option<Box<Value>>), |
| 27 |
Vec(Vec<Value>), |
| 28 |
Map(Vec<(Value, Value)>), |
| 29 |
Set(Vec<Value>), |
| 30 |
Struct { |
| 31 |
type_name: &'static str, |
| 32 |
fields: HashMap<&'static str, Value>, |
| 33 |
}, |
| 34 |
Variant { |
| 35 |
type_name: &'static str, |
| 36 |
variant_name: &'static str, |
| 37 |
fields: HashMap<&'static str, Value>, |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
impl Value { |
| 42 |
pub fn try_into_node<T: Node>(self) -> Result<T, FromValueError> { |
| 43 |
T::try_from_value(self) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl fmt::Debug for Value { |
| 48 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 49 |
match self { |
| 50 |
Self::UInt8(val) => write!(f, "{val}u8"), |
| 51 |
Self::Int8(val) => write!(f, "{val}i8"), |
| 52 |
Self::UInt16(val) => write!(f, "{val}u16"), |
| 53 |
Self::Int16(val) => write!(f, "{val}i16"), |
| 54 |
Self::UInt32(val) => write!(f, "{val}u32"), |
| 55 |
Self::Int32(val) => write!(f, "{val}i32"), |
| 56 |
Self::UInt64(val) => write!(f, "{val}u64"), |
| 57 |
Self::Int64(val) => write!(f, "{val}i64"), |
| 58 |
Self::Float32(val) => write!(f, "{val}f32"), |
| 59 |
Self::Float64(val) => write!(f, "{val}f64"), |
| 60 |
Self::String(val) => write!(f, "{val:?}"), |
| 61 |
Self::Bool(val) => write!(f, "{val:?}"), |
| 62 |
Self::Option(val) => match val { |
| 63 |
Some(v) => f.debug_tuple("Some").field(v).finish(), |
| 64 |
None => f.debug_tuple("None").finish(), |
| 65 |
}, |
| 66 |
Self::Vec(values) => f.debug_list().entries(values).finish(), |
| 67 |
Self::Map(values) => f |
| 68 |
.debug_map() |
| 69 |
.entries(values.iter().map(|(k, v)| (k, v))) |
| 70 |
.finish(), |
| 71 |
Self::Set(values) => f.debug_set().entries(values.iter()).finish(), |
| 72 |
Self::Struct { type_name, fields } => { |
| 73 |
let mut f = f.debug_struct(type_name); |
| 74 |
for (name, val) in fields { |
| 75 |
f.field(name, val); |
| 76 |
} |
| 77 |
f.finish() |
| 78 |
} |
| 79 |
Self::Variant { |
| 80 |
type_name, |
| 81 |
variant_name, |
| 82 |
fields, |
| 83 |
} => { |
| 84 |
let mut f = f.debug_struct(&format!("{type_name}::{variant_name}")); |
| 85 |
for (name, val) in fields { |
| 86 |
f.field(name, val); |
| 87 |
} |
| 88 |
f.finish() |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |