2023-08-28 14:12:41 +00:00
|
|
|
//! Types that represent runtime values.
|
2023-08-22 15:40:50 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, Result},
|
2023-10-01 09:03:59 +00:00
|
|
|
EvaluatorTree, Function, Identifier, Statement, Table, Time, ValueType, VariableMap,
|
2023-08-22 15:40:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use json::JsonValue;
|
|
|
|
use serde::{
|
|
|
|
de::{MapAccess, SeqAccess, Visitor},
|
|
|
|
ser::SerializeTuple,
|
|
|
|
Deserialize, Serialize, Serializer,
|
|
|
|
};
|
2023-10-02 19:19:48 +00:00
|
|
|
use tree_sitter::{Node, TreeCursor};
|
2023-08-22 15:40:50 +00:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
|
|
|
convert::TryFrom,
|
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
marker::PhantomData,
|
2023-10-02 19:19:48 +00:00
|
|
|
ops::{Add, Range, Sub},
|
2023-08-22 15:40:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub mod function;
|
|
|
|
pub mod iter;
|
|
|
|
pub mod table;
|
|
|
|
pub mod time;
|
|
|
|
pub mod value_type;
|
|
|
|
pub mod variable_map;
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub enum Primitive {
|
|
|
|
String(String),
|
|
|
|
Float(f64),
|
|
|
|
Integer(i64),
|
|
|
|
Boolean(bool),
|
|
|
|
#[default]
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
impl Primitive {
|
|
|
|
fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
|
|
|
match node.kind() {
|
|
|
|
"integer" => Primitive::integer_from_source(source, node.byte_range()),
|
|
|
|
"float" => Primitive::float_from_source(source, node.byte_range()),
|
|
|
|
"boolean" => Primitive::boolean_from_source(source, node.byte_range()),
|
|
|
|
"string" => Primitive::string_from_source(source, node.byte_range()),
|
|
|
|
"empty" => Ok(Primitive::Empty),
|
|
|
|
_ => Err(Error::UnexpectedSyntax {
|
|
|
|
expected: "integer, float, boolean, string or empty",
|
|
|
|
actual: node.kind(),
|
|
|
|
location: node.start_position(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn integer_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
let value_snippet = &source[byte_range];
|
|
|
|
let raw = value_snippet.parse::<i64>().unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Primitive::Integer(raw))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn float_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
let value_snippet = &source[byte_range];
|
|
|
|
let raw = value_snippet.parse::<f64>().unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Primitive::Float(raw))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn boolean_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
let value_snippet = &source[byte_range];
|
|
|
|
let raw = value_snippet.parse::<bool>().unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(Primitive::Boolean(raw))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
let value_snippet = &source[byte_range];
|
|
|
|
let without_quotes = &value_snippet[1..value_snippet.len() - 1];
|
|
|
|
|
|
|
|
Ok(Primitive::String(without_quotes.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
impl Eq for Primitive {}
|
|
|
|
|
|
|
|
impl Ord for Primitive {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Primitive::String(left), Primitive::String(right)) => left.cmp(right),
|
|
|
|
(Primitive::Float(left), Primitive::Float(right)) => {
|
|
|
|
left.to_le_bytes().cmp(&right.to_le_bytes())
|
|
|
|
}
|
|
|
|
(Primitive::Integer(left), Primitive::Integer(right)) => left.cmp(right),
|
|
|
|
(Primitive::Boolean(left), Primitive::Boolean(right)) => left.cmp(right),
|
|
|
|
(Primitive::Empty, Primitive::Empty) => Ordering::Equal,
|
|
|
|
(Primitive::String(_), _) => Ordering::Greater,
|
|
|
|
(Primitive::Float(_), _) => Ordering::Greater,
|
|
|
|
(Primitive::Integer(_), _) => Ordering::Greater,
|
|
|
|
(Primitive::Boolean(_), _) => Ordering::Greater,
|
|
|
|
(Primitive::Empty, _) => Ordering::Less,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:40:50 +00:00
|
|
|
/// Whale value representation.
|
|
|
|
///
|
|
|
|
/// Every whale variable has a key and a Value. Variables are represented by
|
|
|
|
/// storing them in a VariableMap. This means the map of variables is itself a
|
|
|
|
/// value that can be treated as any other.
|
2023-10-02 21:15:05 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-08-22 15:40:50 +00:00
|
|
|
pub enum Value {
|
2023-10-02 21:15:05 +00:00
|
|
|
Primitive(Primitive),
|
2023-08-22 15:40:50 +00:00
|
|
|
List(Vec<Value>),
|
|
|
|
Map(VariableMap),
|
|
|
|
Table(Table),
|
|
|
|
Time(Time),
|
|
|
|
Function(Function),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
2023-10-03 03:19:01 +00:00
|
|
|
pub fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
|
|
|
debug_assert_eq!(node.kind(), "value");
|
2023-09-29 03:29:50 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
let child = node.child(0).unwrap();
|
2023-09-29 03:29:50 +00:00
|
|
|
|
2023-10-01 16:17:27 +00:00
|
|
|
match child.kind() {
|
2023-10-03 03:19:01 +00:00
|
|
|
"integer" | "float" | "boolean" | "string" | "empty" => Ok(Value::Primitive(
|
|
|
|
Primitive::from_syntax_node(child, source)?,
|
|
|
|
)),
|
2023-09-29 12:59:09 +00:00
|
|
|
"list" => {
|
2023-10-03 03:19:01 +00:00
|
|
|
let item_count = child.named_child_count();
|
|
|
|
let mut values = Vec::with_capacity(item_count);
|
|
|
|
let mut current_node = child.child(1).unwrap();
|
2023-09-29 12:59:09 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
while values.len() < item_count {
|
|
|
|
if current_node.is_named() {
|
|
|
|
let value = Value::from_syntax_node(current_node, source)?;
|
2023-09-29 12:59:09 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
values.push(value);
|
|
|
|
}
|
2023-09-29 12:59:09 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
current_node = current_node.next_sibling().unwrap();
|
2023-09-29 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::List(values))
|
|
|
|
}
|
2023-09-29 20:33:46 +00:00
|
|
|
"table" => {
|
2023-10-03 03:19:01 +00:00
|
|
|
let mut current_node = child.child(0).unwrap();
|
|
|
|
let header_and_row_count = child.named_child_count();
|
|
|
|
|
|
|
|
let mut headers = Vec::new();
|
|
|
|
let mut rows = Vec::new();
|
|
|
|
|
|
|
|
while headers.len() + rows.len() < header_and_row_count {
|
|
|
|
println!("{current_node:?}");
|
2023-09-29 20:33:46 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
if current_node.kind() == "identifier" {
|
|
|
|
let identifier = Identifier::from_syntax_node(current_node, source)?;
|
|
|
|
let identifier_text = identifier.take_inner();
|
|
|
|
|
|
|
|
headers.push(identifier_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if current_node.kind() == "list" {
|
|
|
|
let value = Value::list_from_syntax_node(current_node, source)?;
|
|
|
|
let row = value.into_inner_list()?;
|
|
|
|
|
|
|
|
rows.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(node) = current_node.next_sibling() {
|
|
|
|
current_node = node;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let table = Table::from_raw_parts(headers, rows);
|
2023-09-29 20:33:46 +00:00
|
|
|
|
|
|
|
Ok(Value::Table(table))
|
|
|
|
}
|
2023-09-30 22:07:12 +00:00
|
|
|
"map" => {
|
|
|
|
let mut map = VariableMap::new();
|
2023-10-03 03:19:01 +00:00
|
|
|
let pair_count = child.named_child_count();
|
2023-10-02 21:15:05 +00:00
|
|
|
let mut current_key = String::new();
|
2023-10-03 03:19:01 +00:00
|
|
|
let mut current_node = child.child(0).unwrap();
|
2023-09-30 22:07:12 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
while map.len() < pair_count {
|
|
|
|
if current_node.kind() == "identifier" {
|
|
|
|
let identifier_text = &source[current_node.byte_range()];
|
2023-10-02 21:15:05 +00:00
|
|
|
current_key = identifier_text.to_string();
|
|
|
|
}
|
2023-09-30 22:07:12 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
if current_node.kind() == "value" {
|
|
|
|
let value = Value::from_syntax_node(current_node, source)?;
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
map.set_value(current_key.to_string(), value)?;
|
2023-09-30 22:07:12 +00:00
|
|
|
}
|
2023-10-03 03:19:01 +00:00
|
|
|
|
|
|
|
if let Some(node) = current_node.next_sibling() {
|
|
|
|
current_node = node;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2023-09-30 22:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Map(map))
|
|
|
|
}
|
2023-09-30 21:52:37 +00:00
|
|
|
"function" => {
|
2023-10-02 19:19:48 +00:00
|
|
|
let child_count = child.child_count();
|
2023-09-30 21:52:37 +00:00
|
|
|
let mut identifiers = Vec::new();
|
|
|
|
let mut statements = Vec::new();
|
|
|
|
|
|
|
|
for index in 0..child_count {
|
2023-10-02 19:19:48 +00:00
|
|
|
let child = child.child(index).unwrap();
|
2023-09-30 21:52:37 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// if child.kind() == "identifier" {
|
|
|
|
// let identifier = Identifier::new(source, cursor)?;
|
2023-09-30 21:52:37 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// identifiers.push(identifier)
|
|
|
|
// }
|
2023-09-30 21:52:37 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// if child.kind() == "statement" {
|
|
|
|
// let statement = Statement::new(source, cursor)?;
|
2023-09-30 21:52:37 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// statements.push(statement)
|
|
|
|
// }
|
2023-09-30 21:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Function(Function::new(identifiers, statements)))
|
|
|
|
}
|
2023-10-01 08:20:29 +00:00
|
|
|
_ => Err(Error::UnexpectedSyntax {
|
2023-10-02 19:19:48 +00:00
|
|
|
expected: "integer, float, boolean, string list, table, map, function or empty",
|
2023-10-01 17:13:13 +00:00
|
|
|
actual: child.kind(),
|
|
|
|
location: child.start_position(),
|
2023-09-29 03:29:50 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
pub fn list_from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
|
|
|
debug_assert_eq!(node.kind(), "list");
|
2023-10-02 21:15:05 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
let item_count = node.named_child_count();
|
|
|
|
let mut values = Vec::with_capacity(item_count);
|
|
|
|
let mut current_node = node.child(1).unwrap();
|
|
|
|
|
|
|
|
while values.len() < item_count {
|
|
|
|
if current_node.is_named() {
|
|
|
|
let value = Value::from_syntax_node(current_node, source)?;
|
|
|
|
|
|
|
|
values.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
current_node = current_node.next_sibling().unwrap();
|
2023-10-02 19:19:48 +00:00
|
|
|
}
|
2023-10-03 03:19:01 +00:00
|
|
|
|
|
|
|
Ok(Value::List(values))
|
2023-10-02 19:19:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// pub fn integer_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
// let value_snippet = &source[byte_range];
|
|
|
|
// let raw = value_snippet.parse::<i64>().unwrap_or_default();
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// Ok(Primitive::Integer(raw))
|
|
|
|
// }
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// pub fn float_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
// let value_snippet = &source[byte_range];
|
|
|
|
// let raw = value_snippet.parse::<f64>().unwrap_or_default();
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// Ok(Primitive::Float(raw))
|
|
|
|
// }
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// pub fn boolean_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
// let value_snippet = &source[byte_range];
|
|
|
|
// let raw = value_snippet.parse::<bool>().unwrap_or_default();
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// Ok(Primitive::Boolean(raw))
|
|
|
|
// }
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// pub fn string_from_source(source: &str, byte_range: Range<usize>) -> Result<Self> {
|
|
|
|
// let value_snippet = &source[byte_range];
|
|
|
|
// let without_quotes = &value_snippet[1..value_snippet.len() - 1];
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-10-03 03:19:01 +00:00
|
|
|
// Ok(Primitive::String(without_quotes.to_string()))
|
|
|
|
// }
|
2023-10-02 19:19:48 +00:00
|
|
|
|
2023-08-22 15:40:50 +00:00
|
|
|
pub fn value_type(&self) -> ValueType {
|
|
|
|
ValueType::from(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_table(&self) -> bool {
|
|
|
|
matches!(self, Value::Table(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_string(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(self, Value::Primitive(Primitive::String(_)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_integer(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(self, Value::Primitive(Primitive::Integer(_)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_float(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(self, Value::Primitive(Primitive::Float(_)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_number(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(
|
|
|
|
self,
|
|
|
|
Value::Primitive(Primitive::Integer(_)) | Value::Primitive(Primitive::Float(_))
|
|
|
|
)
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_boolean(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(self, Value::Primitive(Primitive::Boolean(_)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_list(&self) -> bool {
|
|
|
|
matches!(self, Value::List(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2023-10-02 21:15:05 +00:00
|
|
|
matches!(self, Value::Primitive(Primitive::Empty))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_map(&self) -> bool {
|
|
|
|
matches!(self, Value::Map(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_function(&self) -> bool {
|
|
|
|
matches!(self, Value::Map(_))
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
/// Borrows the value stored in `self` as `String`, or returns `Err` if `self` is not a `Value::Primitive(Primitive::String`.
|
2023-08-22 15:40:50 +00:00
|
|
|
pub fn as_string(&self) -> Result<&String> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::String(string)) => Ok(string),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_string(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copies the value stored in `self` as `i64`, or returns `Err` if `self` is not a `Value::Int`.
|
|
|
|
pub fn as_int(&self) -> Result<i64> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Integer(i)) => Ok(*i),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_int(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
/// Copies the value stored in `self` as `f64`, or returns `Err` if `self` is not a `Primitive::Float`.
|
2023-08-22 15:40:50 +00:00
|
|
|
pub fn as_float(&self) -> Result<f64> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Float(f)) => Ok(*f),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_float(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
/// Copies the value stored in `self` as `f64`, or returns `Err` if `self` is not a `Primitive::Float` or `Value::Int`.
|
2023-08-22 15:40:50 +00:00
|
|
|
/// Note that this method silently converts `i64` to `f64`, if `self` is a `Value::Int`.
|
|
|
|
pub fn as_number(&self) -> Result<f64> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Float(f)) => Ok(*f),
|
|
|
|
Value::Primitive(Primitive::Integer(i)) => Ok(*i as f64),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_number(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
/// Copies the value stored in `self` as `bool`, or returns `Err` if `self` is not a `Primitive::Boolean`.
|
2023-08-22 15:40:50 +00:00
|
|
|
pub fn as_boolean(&self) -> Result<bool> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Boolean(boolean)) => Ok(*boolean),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_boolean(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Vec<Value>`, or returns `Err` if `self` is not a `Value::List`.
|
|
|
|
pub fn as_list(&self) -> Result<&Vec<Value>> {
|
|
|
|
match self {
|
|
|
|
Value::List(list) => Ok(list),
|
|
|
|
value => Err(Error::expected_list(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Vec<Value>`, or returns `Err` if `self` is not a `Value::List`.
|
|
|
|
pub fn into_inner_list(self) -> Result<Vec<Value>> {
|
|
|
|
match self {
|
|
|
|
Value::List(list) => Ok(list),
|
|
|
|
value => Err(Error::expected_list(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Vec<Value>` or returns `Err` if `self` is not a `Value::Map` of the required length.
|
|
|
|
pub fn as_fixed_len_list(&self, len: usize) -> Result<&Vec<Value>> {
|
|
|
|
match self {
|
|
|
|
Value::List(tuple) => {
|
|
|
|
if tuple.len() == len {
|
|
|
|
Ok(tuple)
|
|
|
|
} else {
|
|
|
|
Err(Error::expected_fixed_len_list(len, self.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value => Err(Error::expected_list(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Vec<Value>`, or returns `Err` if `self` is not a `Value::Map`.
|
|
|
|
pub fn as_map(&self) -> Result<&VariableMap> {
|
|
|
|
match self {
|
|
|
|
Value::Map(map) => Ok(map),
|
|
|
|
value => Err(Error::expected_map(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Vec<Value>`, or returns `Err` if `self` is not a `Value::Table`.
|
|
|
|
pub fn as_table(&self) -> Result<&Table> {
|
|
|
|
match self {
|
|
|
|
Value::Table(table) => Ok(table),
|
|
|
|
value => Err(Error::expected_table(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Function`, or returns `Err` if
|
|
|
|
/// `self` is not a `Value::Function`.
|
|
|
|
pub fn as_function(&self) -> Result<&Function> {
|
|
|
|
match self {
|
|
|
|
Value::Function(function) => Ok(function),
|
|
|
|
value => Err(Error::expected_function(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the value stored in `self` as `Time`, or returns `Err` if
|
|
|
|
/// `self` is not a `Value::Time`.
|
|
|
|
pub fn as_time(&self) -> Result<&Time> {
|
|
|
|
match self {
|
|
|
|
Value::Time(time) => Ok(time),
|
|
|
|
value => Err(Error::expected_function(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `()`, or returns`Err` if `self` is not a `Value::Tuple`.
|
|
|
|
pub fn as_empty(&self) -> Result<()> {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Empty) => Ok(()),
|
2023-08-22 15:40:50 +00:00
|
|
|
value => Err(Error::expected_empty(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an owned table, either by cloning or converting the inner value..
|
|
|
|
pub fn to_table(&self) -> Result<Table> {
|
|
|
|
match self {
|
|
|
|
Value::Table(table) => Ok(table.clone()),
|
|
|
|
Value::List(list) => Ok(Table::from(list)),
|
|
|
|
Value::Map(map) => Ok(Table::from(map)),
|
|
|
|
value => Err(Error::expected_table(value.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
impl Default for Value {
|
|
|
|
fn default() -> Self {
|
|
|
|
Value::Primitive(Primitive::Empty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 03:53:37 +00:00
|
|
|
impl Add for Value {
|
|
|
|
type Output = Result<Value>;
|
|
|
|
|
2023-09-29 10:02:46 +00:00
|
|
|
fn add(self, other: Self) -> Self::Output {
|
|
|
|
match (self, other) {
|
2023-10-02 21:15:05 +00:00
|
|
|
(
|
|
|
|
Value::Primitive(Primitive::String(left)),
|
|
|
|
Value::Primitive(Primitive::String(right)),
|
|
|
|
) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
let concatenated = left + &right;
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::String(concatenated)))
|
2023-09-29 03:53:37 +00:00
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(Primitive::String(_)), other)
|
|
|
|
| (other, Value::Primitive(Primitive::String(_))) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
Err(Error::ExpectedString { actual: other })
|
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(
|
|
|
|
Value::Primitive(Primitive::Float(left)),
|
|
|
|
Value::Primitive(Primitive::Float(right)),
|
|
|
|
) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
let addition = left + right;
|
|
|
|
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Float(addition)))
|
2023-09-29 03:53:37 +00:00
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(Primitive::Float(_)), other)
|
|
|
|
| (other, Value::Primitive(Primitive::Float(_))) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
Err(Error::ExpectedFloat { actual: other })
|
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(
|
|
|
|
Value::Primitive(Primitive::Integer(left)),
|
|
|
|
Value::Primitive(Primitive::Integer(right)),
|
|
|
|
) => Ok(Value::Primitive(Primitive::Integer(left + right))),
|
|
|
|
(Value::Primitive(Primitive::Integer(_)), other)
|
|
|
|
| (other, Value::Primitive(Primitive::Integer(_))) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
Err(Error::ExpectedInt { actual: other })
|
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(Primitive::Boolean(_)), Value::Primitive(Primitive::Boolean(_))) => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
(Value::Primitive(Primitive::Boolean(_)), other)
|
|
|
|
| (other, Value::Primitive(Primitive::Boolean(_))) => {
|
2023-09-29 03:53:37 +00:00
|
|
|
Err(Error::ExpectedBoolean { actual: other })
|
|
|
|
}
|
|
|
|
(Value::List(_), Value::List(_)) => todo!(),
|
|
|
|
(Value::List(_), other) | (other, Value::List(_)) => {
|
|
|
|
Err(Error::ExpectedList { actual: other })
|
|
|
|
}
|
|
|
|
(Value::Map(_), Value::Map(_)) => todo!(),
|
|
|
|
(Value::Map(_), other) | (other, Value::Map(_)) => {
|
|
|
|
Err(Error::ExpectedMap { actual: other })
|
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(Primitive::Empty), Value::Primitive(Primitive::Empty)) => {
|
|
|
|
Ok(Value::Primitive(Primitive::Empty))
|
|
|
|
}
|
2023-09-29 03:53:37 +00:00
|
|
|
_ => Err(Error::CustomMessage(
|
|
|
|
"Cannot add the given types.".to_string(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 10:02:46 +00:00
|
|
|
impl Sub for Value {
|
|
|
|
type Output = Result<Self>;
|
|
|
|
|
|
|
|
fn sub(self, other: Self) -> Self::Output {
|
2023-10-02 21:15:05 +00:00
|
|
|
todo!()
|
2023-09-29 10:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:40:50 +00:00
|
|
|
impl Eq for Value {}
|
|
|
|
|
2023-10-01 16:17:27 +00:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::List(left), Value::List(right)) => left == right,
|
|
|
|
(Value::Map(left), Value::Map(right)) => left == right,
|
|
|
|
(Value::Table(left), Value::Table(right)) => left == right,
|
|
|
|
(Value::Time(left), Value::Time(right)) => left == right,
|
|
|
|
(Value::Function(left), Value::Function(right)) => left == right,
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(left), Value::Primitive(right)) => left == right,
|
2023-10-01 16:17:27 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:40:50 +00:00
|
|
|
impl PartialOrd for Value {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Value {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::List(left), Value::List(right)) => left.cmp(right),
|
|
|
|
(Value::List(_), _) => Ordering::Greater,
|
|
|
|
(Value::Map(left), Value::Map(right)) => left.cmp(right),
|
|
|
|
(Value::Map(_), _) => Ordering::Greater,
|
|
|
|
(Value::Table(left), Value::Table(right)) => left.cmp(right),
|
|
|
|
(Value::Table(_), _) => Ordering::Greater,
|
|
|
|
(Value::Function(left), Value::Function(right)) => left.cmp(right),
|
|
|
|
(Value::Function(_), _) => Ordering::Greater,
|
|
|
|
(Value::Time(left), Value::Time(right)) => left.cmp(right),
|
|
|
|
(Value::Time(_), _) => Ordering::Greater,
|
2023-10-02 21:15:05 +00:00
|
|
|
(Value::Primitive(left), Value::Primitive(right)) => left.cmp(right),
|
|
|
|
(Value::Primitive(_), _) => Ordering::Less,
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Value {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::String(inner)) => serializer.serialize_str(inner),
|
|
|
|
Value::Primitive(Primitive::Float(inner)) => serializer.serialize_f64(*inner),
|
|
|
|
Value::Primitive(Primitive::Integer(inner)) => serializer.serialize_i64(*inner),
|
|
|
|
Value::Primitive(Primitive::Boolean(inner)) => serializer.serialize_bool(*inner),
|
2023-08-22 15:40:50 +00:00
|
|
|
Value::List(inner) => {
|
|
|
|
let mut tuple = serializer.serialize_tuple(inner.len())?;
|
|
|
|
|
|
|
|
for value in inner {
|
|
|
|
tuple.serialize_element(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple.end()
|
|
|
|
}
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Empty) => todo!(),
|
2023-08-22 15:40:50 +00:00
|
|
|
Value::Map(inner) => inner.serialize(serializer),
|
|
|
|
Value::Table(inner) => inner.serialize(serializer),
|
|
|
|
Value::Function(inner) => inner.serialize(serializer),
|
|
|
|
Value::Time(inner) => inner.serialize(serializer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::String(string)) => write!(f, "{string}"),
|
|
|
|
Value::Primitive(Primitive::Float(float)) => write!(f, "{}", float),
|
|
|
|
Value::Primitive(Primitive::Integer(int)) => write!(f, "{}", int),
|
|
|
|
Value::Primitive(Primitive::Boolean(boolean)) => write!(f, "{}", boolean),
|
|
|
|
Value::Primitive(Primitive::Empty) => write!(f, "()"),
|
2023-10-03 03:19:01 +00:00
|
|
|
Value::List(list) => {
|
|
|
|
write!(f, "(")?;
|
|
|
|
for value in list {
|
|
|
|
write!(f, " {value} ")?;
|
|
|
|
}
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
Value::Map(map) => write!(f, "{map}"),
|
|
|
|
Value::Table(table) => write!(f, "{table}"),
|
|
|
|
Value::Function(function) => write!(f, "{function}"),
|
|
|
|
Value::Time(time) => write!(f, "{time}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Value {
|
|
|
|
fn from(string: String) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::String(string))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Value {
|
|
|
|
fn from(string: &str) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::String(string.to_string()))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<f64> for Value {
|
|
|
|
fn from(float: f64) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Float(float))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i64> for Value {
|
|
|
|
fn from(int: i64) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Integer(int))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bool> for Value {
|
|
|
|
fn from(boolean: bool) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Boolean(boolean))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<Value>> for Value {
|
|
|
|
fn from(tuple: Vec<Value>) -> Self {
|
|
|
|
Value::List(tuple)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Value> for Result<Value> {
|
|
|
|
fn from(value: Value) -> Self {
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<()> for Value {
|
|
|
|
fn from(_: ()) -> Self {
|
2023-10-02 21:15:05 +00:00
|
|
|
Value::Primitive(Primitive::Empty)
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<JsonValue> for Value {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(json_value: JsonValue) -> Result<Self> {
|
|
|
|
use JsonValue::*;
|
|
|
|
|
|
|
|
match json_value {
|
2023-10-02 21:15:05 +00:00
|
|
|
Null => Ok(Value::Primitive(Primitive::Empty)),
|
|
|
|
Short(short) => Ok(Value::Primitive(Primitive::String(short.to_string()))),
|
|
|
|
String(string) => Ok(Value::Primitive(Primitive::String(string))),
|
|
|
|
Number(number) => Ok(Value::Primitive(Primitive::Float(f64::from(number)))),
|
|
|
|
Boolean(boolean) => Ok(Value::Primitive(Primitive::Boolean(boolean))),
|
2023-08-22 15:40:50 +00:00
|
|
|
Object(object) => {
|
|
|
|
let mut map = VariableMap::new();
|
|
|
|
|
|
|
|
for (key, node_value) in object.iter() {
|
|
|
|
let value = Value::try_from(node_value)?;
|
|
|
|
|
2023-10-02 19:19:48 +00:00
|
|
|
map.set_value(key.to_string(), value)?;
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Map(map))
|
|
|
|
}
|
|
|
|
Array(array) => {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for json_value in array {
|
|
|
|
let value = Value::try_from(json_value)?;
|
|
|
|
|
|
|
|
list.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::List(list))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&JsonValue> for Value {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(json_value: &JsonValue) -> Result<Self> {
|
|
|
|
use JsonValue::*;
|
|
|
|
|
|
|
|
match json_value {
|
2023-10-02 21:15:05 +00:00
|
|
|
Null => Ok(Value::Primitive(Primitive::Empty)),
|
|
|
|
Short(short) => Ok(Value::Primitive(Primitive::String(short.to_string()))),
|
|
|
|
String(string) => Ok(Value::Primitive(Primitive::String(string.clone()))),
|
|
|
|
Number(number) => Ok(Value::Primitive(Primitive::Float(f64::from(*number)))),
|
|
|
|
Boolean(boolean) => Ok(Value::Primitive(Primitive::Boolean(*boolean))),
|
2023-08-22 15:40:50 +00:00
|
|
|
Object(object) => {
|
|
|
|
let mut map = VariableMap::new();
|
|
|
|
|
|
|
|
for (key, node_value) in object.iter() {
|
|
|
|
let value = Value::try_from(node_value)?;
|
|
|
|
|
2023-10-02 19:19:48 +00:00
|
|
|
map.set_value(key.to_string(), value)?;
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Map(map))
|
|
|
|
}
|
|
|
|
Array(array) => {
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for json_value in array {
|
|
|
|
let value = Value::try_from(json_value)?;
|
|
|
|
|
|
|
|
list.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::List(list))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for String {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
2023-10-02 21:15:05 +00:00
|
|
|
if let Value::Primitive(Primitive::String(value)) = value {
|
2023-08-22 15:40:50 +00:00
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Err(Error::ExpectedString { actual: value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for f64 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
2023-10-02 21:15:05 +00:00
|
|
|
if let Value::Primitive(Primitive::Float(value)) = value {
|
2023-08-22 15:40:50 +00:00
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Err(Error::ExpectedFloat { actual: value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for i64 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
2023-10-02 21:15:05 +00:00
|
|
|
if let Value::Primitive(Primitive::Integer(value)) = value {
|
2023-08-22 15:40:50 +00:00
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Err(Error::ExpectedInt { actual: value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for bool {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
2023-10-02 21:15:05 +00:00
|
|
|
if let Value::Primitive(Primitive::Boolean(value)) = value {
|
2023-08-22 15:40:50 +00:00
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Err(Error::ExpectedBoolean { actual: value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueVisitor {
|
|
|
|
marker: PhantomData<fn() -> Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValueVisitor {
|
|
|
|
fn new() -> Self {
|
|
|
|
ValueVisitor {
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for ValueVisitor {
|
|
|
|
type Value = Value;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("Any valid whale data.")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, v: bool) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Boolean(v)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i8<E>(self, v: i8) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i16<E>(self, v: i16) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i32<E>(self, v: i32) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i64<E>(self, v: i64) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Integer(v)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i128<E>(self, v: i128) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
if v > i64::MAX as i128 {
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Integer(i64::MAX)))
|
2023-08-22 15:40:50 +00:00
|
|
|
} else {
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Integer(v as i64)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u8<E>(self, v: u8) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u16<E>(self, v: u16) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u32<E>(self, v: u32) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u64<E>(self, v: u64) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u128<E>(self, v: u128) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i128(v as i128)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_f32<E>(self, v: f32) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_f64(v as f64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_f64<E>(self, v: f64) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::Float(v)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_char<E>(self, v: char) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(&v.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::String(v.to_string())))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2023-10-02 21:15:05 +00:00
|
|
|
Ok(Value::Primitive(Primitive::String(v)))
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
let _ = v;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Bytes(v),
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_bytes(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_bytes(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_none<E>(self) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Option,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_some<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let _ = deserializer;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Option,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_unit<E>(self) -> std::result::Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Unit,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_newtype_struct<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let _ = deserializer;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::NewtypeStruct,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A>(self, mut access: A) -> std::result::Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
while let Some(value) = access.next_element()? {
|
|
|
|
list.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::List(list))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<M>(self, mut access: M) -> std::result::Result<Value, M::Error>
|
|
|
|
where
|
|
|
|
M: MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut map = VariableMap::new();
|
|
|
|
|
|
|
|
while let Some((key, value)) = access.next_entry()? {
|
|
|
|
map.set_value(key, value)
|
|
|
|
.expect("Failed to deserialize Value. This is a no-op.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::Map(map))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_enum<A>(self, data: A) -> std::result::Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: serde::de::EnumAccess<'de>,
|
|
|
|
{
|
|
|
|
let _ = data;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Enum,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __private_visit_untagged_option<D>(self, _: D) -> std::result::Result<Self::Value, ()>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Value {
|
|
|
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_any(ValueVisitor::new())
|
|
|
|
}
|
|
|
|
}
|