Update internal API
This commit is contained in:
parent
5551f40ffd
commit
7fa5dd0f54
@ -3,7 +3,7 @@
|
|||||||
//! To deal with errors from dependencies, either create a new error variant
|
//! To deal with errors from dependencies, either create a new error variant
|
||||||
//! or use the MacroFailure variant if the error can only occur inside a macro.
|
//! or use the MacroFailure variant if the error can only occur inside a macro.
|
||||||
|
|
||||||
use crate::{value::value_type::ValueType, value::Value, Primitive};
|
use crate::{value::value_type::ValueType, value::Value};
|
||||||
|
|
||||||
use std::{fmt, io, time::SystemTimeError};
|
use std::{fmt, io, time::SystemTimeError};
|
||||||
|
|
||||||
@ -382,9 +382,7 @@ impl Error {
|
|||||||
/// Returns `Ok(())` if the given value is a string or a numeric.
|
/// Returns `Ok(())` if the given value is a string or a numeric.
|
||||||
pub fn expect_number_or_string(actual: &Value) -> Result<()> {
|
pub fn expect_number_or_string(actual: &Value) -> Result<()> {
|
||||||
match actual {
|
match actual {
|
||||||
Value::Primitive(Primitive::String(_))
|
Value::String(_) | Value::Float(_) | Value::Integer(_) => Ok(()),
|
||||||
| Value::Primitive(Primitive::Float(_))
|
|
||||||
| Value::Primitive(Primitive::Integer(_)) => Ok(()),
|
|
||||||
_ => Err(Error::expected_number_or_string(actual.clone())),
|
_ => Err(Error::expected_number_or_string(actual.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
103
src/interface.rs
103
src/interface.rs
@ -7,7 +7,7 @@ use std::fmt::{self, Debug, Formatter};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::{Node, Parser, Tree as TSTree, TreeCursor};
|
use tree_sitter::{Node, Parser, Tree as TSTree, TreeCursor};
|
||||||
|
|
||||||
use crate::{language, Error, Primitive, Result, Value, VariableMap};
|
use crate::{language, Error, Result, Value, VariableMap};
|
||||||
|
|
||||||
/// Evaluate the given source code.
|
/// Evaluate the given source code.
|
||||||
///
|
///
|
||||||
@ -161,7 +161,7 @@ impl EvaluatorTree for Item {
|
|||||||
|
|
||||||
fn run(&self, context: &mut VariableMap) -> Result<Value> {
|
fn run(&self, context: &mut VariableMap) -> Result<Value> {
|
||||||
match self {
|
match self {
|
||||||
Item::Comment(text) => Ok(Value::Primitive(Primitive::String(text.clone()))),
|
Item::Comment(text) => Ok(Value::String(text.clone())),
|
||||||
Item::Statement(statement) => statement.run(context),
|
Item::Statement(statement) => statement.run(context),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -322,7 +322,7 @@ impl EvaluatorTree for ControlFlow {
|
|||||||
} else if let Some(statement) = &self.else_statement {
|
} else if let Some(statement) = &self.else_statement {
|
||||||
statement.run(context)
|
statement.run(context)
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Primitive(Primitive::Empty))
|
Ok(Value::Empty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -355,7 +355,7 @@ impl EvaluatorTree for Assignment {
|
|||||||
|
|
||||||
context.set_value(key, value)?;
|
context.set_value(key, value)?;
|
||||||
|
|
||||||
Ok(Value::Primitive(Primitive::Empty))
|
Ok(Value::Empty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ impl EvaluatorTree for Math {
|
|||||||
MathOperator::Modulo => left_value % right_value,
|
MathOperator::Modulo => left_value % right_value,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Value::Primitive(Primitive::Float(outcome)))
|
Ok(Value::Float(outcome))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -467,67 +467,40 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn evaluate_empty() {
|
fn evaluate_empty() {
|
||||||
assert_eq!(eval("x = 9"), vec![Ok(Value::Primitive(Primitive::Empty))]);
|
assert_eq!(eval("x = 9"), vec![Ok(Value::Empty)]);
|
||||||
assert_eq!(
|
assert_eq!(eval("x = 'foo' + 'bar'"), vec![Ok(Value::Empty)]);
|
||||||
eval("x = 'foo' + 'bar'"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::Empty))]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn evaluate_integer() {
|
fn evaluate_integer() {
|
||||||
assert_eq!(eval("1"), vec![Ok(Value::Primitive(Primitive::Integer(1)))]);
|
assert_eq!(eval("1"), vec![Ok(Value::Integer(1))]);
|
||||||
assert_eq!(
|
assert_eq!(eval("123"), vec![Ok(Value::Integer(123))]);
|
||||||
eval("123"),
|
assert_eq!(eval("-666"), vec![Ok(Value::Integer(-666))]);
|
||||||
vec![Ok(Value::Primitive(Primitive::Integer(123)))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
eval("-666"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::Integer(-666)))]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn evaluate_float() {
|
fn evaluate_float() {
|
||||||
assert_eq!(
|
assert_eq!(eval("0.1"), vec![Ok(Value::Float(0.1))]);
|
||||||
eval("0.1"),
|
assert_eq!(eval("12.3"), vec![Ok(Value::Float(12.3))]);
|
||||||
vec![Ok(Value::Primitive(Primitive::Float(0.1)))]
|
assert_eq!(eval("-6.66"), vec![Ok(Value::Float(-6.66))]);
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
eval("12.3"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::Float(12.3)))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
eval("-6.66"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::Float(-6.66)))]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn evaluate_string() {
|
fn evaluate_string() {
|
||||||
assert_eq!(
|
assert_eq!(eval("\"one\""), vec![Ok(Value::String("one".to_string()))]);
|
||||||
eval("\"one\""),
|
assert_eq!(eval("'one'"), vec![Ok(Value::String("one".to_string()))]);
|
||||||
vec![Ok(Value::Primitive(Primitive::String("one".to_string())))]
|
assert_eq!(eval("`one`"), vec![Ok(Value::String("one".to_string()))]);
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
eval("'one'"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::String("one".to_string())))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
eval("`one`"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::String("one".to_string())))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("`'one'`"),
|
eval("`'one'`"),
|
||||||
vec![Ok(Value::Primitive(Primitive::String("'one'".to_string())))]
|
vec![Ok(Value::String("'one'".to_string()))]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("'`one`'"),
|
eval("'`one`'"),
|
||||||
vec![Ok(Value::Primitive(Primitive::String("`one`".to_string())))]
|
vec![Ok(Value::String("`one`".to_string()))]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("\"'one'\""),
|
eval("\"'one'\""),
|
||||||
vec![Ok(Value::Primitive(Primitive::String("'one'".to_string())))]
|
vec![Ok(Value::String("'one'".to_string()))]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -536,9 +509,9 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("[1, 2, 'foobar']"),
|
eval("[1, 2, 'foobar']"),
|
||||||
vec![Ok(Value::List(vec![
|
vec![Ok(Value::List(vec![
|
||||||
Value::Primitive(Primitive::Integer(1)),
|
Value::Integer(1),
|
||||||
Value::Primitive(Primitive::Integer(2)),
|
Value::Integer(2),
|
||||||
Value::Primitive(Primitive::String("foobar".to_string())),
|
Value::String("foobar".to_string()),
|
||||||
]))]
|
]))]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -547,12 +520,8 @@ mod tests {
|
|||||||
fn evaluate_map() {
|
fn evaluate_map() {
|
||||||
let mut map = VariableMap::new();
|
let mut map = VariableMap::new();
|
||||||
|
|
||||||
map.set_value("x".to_string(), Value::Primitive(Primitive::Integer(1)))
|
map.set_value("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
.unwrap();
|
map.set_value("foo".to_string(), Value::String("bar".to_string()))
|
||||||
map.set_value(
|
|
||||||
"foo".to_string(),
|
|
||||||
Value::Primitive(Primitive::String("bar".to_string())),
|
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(eval("map { x = 1 foo = 'bar' }"), vec![Ok(Value::Map(map))]);
|
assert_eq!(eval("map { x = 1 foo = 'bar' }"), vec![Ok(Value::Map(map))]);
|
||||||
@ -563,22 +532,13 @@ mod tests {
|
|||||||
let mut table = Table::new(vec!["messages".to_string(), "numbers".to_string()]);
|
let mut table = Table::new(vec!["messages".to_string(), "numbers".to_string()]);
|
||||||
|
|
||||||
table
|
table
|
||||||
.insert(vec![
|
.insert(vec![Value::String("hiya".to_string()), Value::Integer(42)])
|
||||||
Value::Primitive(Primitive::String("hiya".to_string())),
|
|
||||||
Value::Primitive(Primitive::Integer(42)),
|
|
||||||
])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
table
|
table
|
||||||
.insert(vec![
|
.insert(vec![Value::String("foo".to_string()), Value::Integer(57)])
|
||||||
Value::Primitive(Primitive::String("foo".to_string())),
|
|
||||||
Value::Primitive(Primitive::Integer(57)),
|
|
||||||
])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
table
|
table
|
||||||
.insert(vec![
|
.insert(vec![Value::String("bar".to_string()), Value::Float(99.99)])
|
||||||
Value::Primitive(Primitive::String("bar".to_string())),
|
|
||||||
Value::Primitive(Primitive::Float(99.99)),
|
|
||||||
])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -599,19 +559,16 @@ mod tests {
|
|||||||
fn if_then() {
|
fn if_then() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("if true then 'true'"),
|
eval("if true then 'true'"),
|
||||||
vec![Ok(Value::Primitive(Primitive::String("true".to_string())))]
|
vec![Ok(Value::String("true".to_string()))]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn if_then_else() {
|
fn if_then_else() {
|
||||||
assert_eq!(
|
assert_eq!(eval("if false then 1 else 2"), vec![Ok(Value::Integer(2))]);
|
||||||
eval("if false then 1 else 2"),
|
|
||||||
vec![Ok(Value::Primitive(Primitive::Integer(2)))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
eval("if true then 1.0 else 42.0"),
|
eval("if true then 1.0 else 42.0"),
|
||||||
vec![Ok(Value::Primitive(Primitive::Float(1.0)))]
|
vec![Ok(Value::Float(1.0))]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ pub use crate::{
|
|||||||
interface::*,
|
interface::*,
|
||||||
value::{
|
value::{
|
||||||
function::Function, table::Table, time::Time, value_type::ValueType,
|
function::Function, table::Table, time::Time, value_type::ValueType,
|
||||||
variable_map::VariableMap, Primitive, Value,
|
variable_map::VariableMap, Value,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ use rustyline::{
|
|||||||
|
|
||||||
use std::{borrow::Cow, fs::read_to_string};
|
use std::{borrow::Cow, fs::read_to_string};
|
||||||
|
|
||||||
use dust_lib::{eval, eval_with_context, Primitive, Value, VariableMap};
|
use dust_lib::{eval, eval_with_context, Value, VariableMap};
|
||||||
|
|
||||||
/// Command-line arguments to be parsed.
|
/// Command-line arguments to be parsed.
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@ -39,7 +39,7 @@ fn main() {
|
|||||||
} else if let Some(command) = args.command {
|
} else if let Some(command) = args.command {
|
||||||
eval(&command)
|
eval(&command)
|
||||||
} else {
|
} else {
|
||||||
vec![Ok(Value::Primitive(Primitive::Empty))]
|
vec![Ok(Value::Empty)]
|
||||||
};
|
};
|
||||||
|
|
||||||
for result in eval_results {
|
for result in eval_results {
|
||||||
|
260
src/value/mod.rs
260
src/value/mod.rs
@ -27,8 +27,18 @@ pub mod time;
|
|||||||
pub mod value_type;
|
pub mod value_type;
|
||||||
pub mod variable_map;
|
pub mod variable_map;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Serialize, Deserialize)]
|
/// Whale value representation.
|
||||||
pub enum Primitive {
|
///
|
||||||
|
/// 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.
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub enum Value {
|
||||||
|
List(Vec<Value>),
|
||||||
|
Map(VariableMap),
|
||||||
|
Table(Table),
|
||||||
|
Time(Time),
|
||||||
|
Function(Function),
|
||||||
String(String),
|
String(String),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Integer(i64),
|
Integer(i64),
|
||||||
@ -37,86 +47,6 @@ pub enum Primitive {
|
|||||||
Empty,
|
Empty,
|
||||||
}
|
}
|
||||||
|
|
||||||
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()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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.
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum Value {
|
|
||||||
Primitive(Primitive),
|
|
||||||
List(Vec<Value>),
|
|
||||||
Map(VariableMap),
|
|
||||||
Table(Table),
|
|
||||||
Time(Time),
|
|
||||||
Function(Function),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
pub fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
pub fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
|
||||||
debug_assert_eq!(node.kind(), "value");
|
debug_assert_eq!(node.kind(), "value");
|
||||||
@ -124,9 +54,9 @@ impl Value {
|
|||||||
let child = node.child(0).unwrap();
|
let child = node.child(0).unwrap();
|
||||||
|
|
||||||
match child.kind() {
|
match child.kind() {
|
||||||
"integer" | "float" | "boolean" | "string" | "empty" => Ok(Value::Primitive(
|
"integer" | "float" | "boolean" | "string" | "empty" => {
|
||||||
Primitive::from_syntax_node(child, source)?,
|
Ok(Value::from_syntax_node(child, source)?)
|
||||||
)),
|
}
|
||||||
"list" => {
|
"list" => {
|
||||||
let item_count = child.named_child_count();
|
let item_count = child.named_child_count();
|
||||||
let mut values = Vec::with_capacity(item_count);
|
let mut values = Vec::with_capacity(item_count);
|
||||||
@ -294,26 +224,23 @@ impl Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_string(&self) -> bool {
|
pub fn is_string(&self) -> bool {
|
||||||
matches!(self, Value::Primitive(Primitive::String(_)))
|
matches!(self, Value::String(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_integer(&self) -> bool {
|
pub fn is_integer(&self) -> bool {
|
||||||
matches!(self, Value::Primitive(Primitive::Integer(_)))
|
matches!(self, Value::Integer(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_float(&self) -> bool {
|
pub fn is_float(&self) -> bool {
|
||||||
matches!(self, Value::Primitive(Primitive::Float(_)))
|
matches!(self, Value::Float(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_number(&self) -> bool {
|
pub fn is_number(&self) -> bool {
|
||||||
matches!(
|
matches!(self, Value::Integer(_) | Value::Float(_))
|
||||||
self,
|
|
||||||
Value::Primitive(Primitive::Integer(_)) | Value::Primitive(Primitive::Float(_))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_boolean(&self) -> bool {
|
pub fn is_boolean(&self) -> bool {
|
||||||
matches!(self, Value::Primitive(Primitive::Boolean(_)))
|
matches!(self, Value::Boolean(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_list(&self) -> bool {
|
pub fn is_list(&self) -> bool {
|
||||||
@ -321,7 +248,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
matches!(self, Value::Primitive(Primitive::Empty))
|
matches!(self, Value::Empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_map(&self) -> bool {
|
pub fn is_map(&self) -> bool {
|
||||||
@ -332,10 +259,10 @@ impl Value {
|
|||||||
matches!(self, Value::Map(_))
|
matches!(self, Value::Map(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Borrows the value stored in `self` as `String`, or returns `Err` if `self` is not a `Value::Primitive(Primitive::String`.
|
/// Borrows the value stored in `self` as `String`, or returns `Err` if `self` is not a `Value::String`.
|
||||||
pub fn as_string(&self) -> Result<&String> {
|
pub fn as_string(&self) -> Result<&String> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::String(string)) => Ok(string),
|
Value::String(string) => Ok(string),
|
||||||
value => Err(Error::expected_string(value.clone())),
|
value => Err(Error::expected_string(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -343,7 +270,7 @@ impl Value {
|
|||||||
/// Copies the value stored in `self` as `i64`, or returns `Err` if `self` is not a `Value::Int`.
|
/// 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> {
|
pub fn as_int(&self) -> Result<i64> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::Integer(i)) => Ok(*i),
|
Value::Integer(i) => Ok(*i),
|
||||||
value => Err(Error::expected_int(value.clone())),
|
value => Err(Error::expected_int(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -351,7 +278,7 @@ impl Value {
|
|||||||
/// Copies the value stored in `self` as `f64`, or returns `Err` if `self` is not a `Primitive::Float`.
|
/// Copies the value stored in `self` as `f64`, or returns `Err` if `self` is not a `Primitive::Float`.
|
||||||
pub fn as_float(&self) -> Result<f64> {
|
pub fn as_float(&self) -> Result<f64> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::Float(f)) => Ok(*f),
|
Value::Float(f) => Ok(*f),
|
||||||
value => Err(Error::expected_float(value.clone())),
|
value => Err(Error::expected_float(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -360,8 +287,8 @@ impl Value {
|
|||||||
/// Note that this method silently converts `i64` to `f64`, if `self` is a `Value::Int`.
|
/// Note that this method silently converts `i64` to `f64`, if `self` is a `Value::Int`.
|
||||||
pub fn as_number(&self) -> Result<f64> {
|
pub fn as_number(&self) -> Result<f64> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::Float(f)) => Ok(*f),
|
Value::Float(f) => Ok(*f),
|
||||||
Value::Primitive(Primitive::Integer(i)) => Ok(*i as f64),
|
Value::Integer(i) => Ok(*i as f64),
|
||||||
value => Err(Error::expected_number(value.clone())),
|
value => Err(Error::expected_number(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -369,7 +296,7 @@ impl Value {
|
|||||||
/// Copies the value stored in `self` as `bool`, or returns `Err` if `self` is not a `Primitive::Boolean`.
|
/// Copies the value stored in `self` as `bool`, or returns `Err` if `self` is not a `Primitive::Boolean`.
|
||||||
pub fn as_boolean(&self) -> Result<bool> {
|
pub fn as_boolean(&self) -> Result<bool> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::Boolean(boolean)) => Ok(*boolean),
|
Value::Boolean(boolean) => Ok(*boolean),
|
||||||
value => Err(Error::expected_boolean(value.clone())),
|
value => Err(Error::expected_boolean(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -441,7 +368,7 @@ impl Value {
|
|||||||
/// Returns `()`, or returns`Err` if `self` is not a `Value::Tuple`.
|
/// Returns `()`, or returns`Err` if `self` is not a `Value::Tuple`.
|
||||||
pub fn as_empty(&self) -> Result<()> {
|
pub fn as_empty(&self) -> Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::Empty) => Ok(()),
|
Value::Empty => Ok(()),
|
||||||
value => Err(Error::expected_empty(value.clone())),
|
value => Err(Error::expected_empty(value.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -457,54 +384,35 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Value {
|
|
||||||
fn default() -> Self {
|
|
||||||
Value::Primitive(Primitive::Empty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for Value {
|
impl Add for Value {
|
||||||
type Output = Result<Value>;
|
type Output = Result<Value>;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(
|
(Value::String(left), Value::String(right)) => {
|
||||||
Value::Primitive(Primitive::String(left)),
|
|
||||||
Value::Primitive(Primitive::String(right)),
|
|
||||||
) => {
|
|
||||||
let concatenated = left + &right;
|
let concatenated = left + &right;
|
||||||
|
|
||||||
Ok(Value::Primitive(Primitive::String(concatenated)))
|
Ok(Value::String(concatenated))
|
||||||
}
|
}
|
||||||
(Value::Primitive(Primitive::String(_)), other)
|
(Value::String(_), other) | (other, Value::String(_)) => {
|
||||||
| (other, Value::Primitive(Primitive::String(_))) => {
|
|
||||||
Err(Error::ExpectedString { actual: other })
|
Err(Error::ExpectedString { actual: other })
|
||||||
}
|
}
|
||||||
(
|
(Value::Float(left), Value::Float(right)) => {
|
||||||
Value::Primitive(Primitive::Float(left)),
|
|
||||||
Value::Primitive(Primitive::Float(right)),
|
|
||||||
) => {
|
|
||||||
let addition = left + right;
|
let addition = left + right;
|
||||||
|
|
||||||
Ok(Value::Primitive(Primitive::Float(addition)))
|
Ok(Value::Float(addition))
|
||||||
}
|
}
|
||||||
(Value::Primitive(Primitive::Float(_)), other)
|
(Value::Float(_), other) | (other, Value::Float(_)) => {
|
||||||
| (other, Value::Primitive(Primitive::Float(_))) => {
|
|
||||||
Err(Error::ExpectedFloat { actual: other })
|
Err(Error::ExpectedFloat { actual: other })
|
||||||
}
|
}
|
||||||
(
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Integer(left + right)),
|
||||||
Value::Primitive(Primitive::Integer(left)),
|
(Value::Integer(_), other) | (other, Value::Integer(_)) => {
|
||||||
Value::Primitive(Primitive::Integer(right)),
|
|
||||||
) => Ok(Value::Primitive(Primitive::Integer(left + right))),
|
|
||||||
(Value::Primitive(Primitive::Integer(_)), other)
|
|
||||||
| (other, Value::Primitive(Primitive::Integer(_))) => {
|
|
||||||
Err(Error::ExpectedInt { actual: other })
|
Err(Error::ExpectedInt { actual: other })
|
||||||
}
|
}
|
||||||
(Value::Primitive(Primitive::Boolean(_)), Value::Primitive(Primitive::Boolean(_))) => {
|
(Value::Boolean(_), Value::Boolean(_)) => {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
(Value::Primitive(Primitive::Boolean(_)), other)
|
(Value::Boolean(_), other) | (other, Value::Boolean(_)) => {
|
||||||
| (other, Value::Primitive(Primitive::Boolean(_))) => {
|
|
||||||
Err(Error::ExpectedBoolean { actual: other })
|
Err(Error::ExpectedBoolean { actual: other })
|
||||||
}
|
}
|
||||||
(Value::List(_), Value::List(_)) => todo!(),
|
(Value::List(_), Value::List(_)) => todo!(),
|
||||||
@ -515,9 +423,7 @@ impl Add for Value {
|
|||||||
(Value::Map(_), other) | (other, Value::Map(_)) => {
|
(Value::Map(_), other) | (other, Value::Map(_)) => {
|
||||||
Err(Error::ExpectedMap { actual: other })
|
Err(Error::ExpectedMap { actual: other })
|
||||||
}
|
}
|
||||||
(Value::Primitive(Primitive::Empty), Value::Primitive(Primitive::Empty)) => {
|
(Value::Empty, Value::Empty) => Ok(Value::Empty),
|
||||||
Ok(Value::Primitive(Primitive::Empty))
|
|
||||||
}
|
|
||||||
_ => Err(Error::CustomMessage(
|
_ => Err(Error::CustomMessage(
|
||||||
"Cannot add the given types.".to_string(),
|
"Cannot add the given types.".to_string(),
|
||||||
)),
|
)),
|
||||||
@ -543,7 +449,6 @@ impl PartialEq for Value {
|
|||||||
(Value::Table(left), Value::Table(right)) => left == right,
|
(Value::Table(left), Value::Table(right)) => left == right,
|
||||||
(Value::Time(left), Value::Time(right)) => left == right,
|
(Value::Time(left), Value::Time(right)) => left == right,
|
||||||
(Value::Function(left), Value::Function(right)) => left == right,
|
(Value::Function(left), Value::Function(right)) => left == right,
|
||||||
(Value::Primitive(left), Value::Primitive(right)) => left == right,
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -558,6 +463,14 @@ impl PartialOrd for Value {
|
|||||||
impl Ord for Value {
|
impl Ord for Value {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
|
(Value::String(left), Value::String(right)) => left.cmp(right),
|
||||||
|
(Value::String(_), _) => Ordering::Greater,
|
||||||
|
(Value::Float(left), Value::Float(right)) => left.total_cmp(right),
|
||||||
|
(Value::Float(_), _) => Ordering::Greater,
|
||||||
|
(Value::Integer(left), Value::Integer(right)) => left.cmp(right),
|
||||||
|
(Value::Integer(_), _) => Ordering::Greater,
|
||||||
|
(Value::Boolean(left), Value::Boolean(right)) => left.cmp(right),
|
||||||
|
(Value::Boolean(_), _) => Ordering::Greater,
|
||||||
(Value::List(left), Value::List(right)) => left.cmp(right),
|
(Value::List(left), Value::List(right)) => left.cmp(right),
|
||||||
(Value::List(_), _) => Ordering::Greater,
|
(Value::List(_), _) => Ordering::Greater,
|
||||||
(Value::Map(left), Value::Map(right)) => left.cmp(right),
|
(Value::Map(left), Value::Map(right)) => left.cmp(right),
|
||||||
@ -568,8 +481,7 @@ impl Ord for Value {
|
|||||||
(Value::Function(_), _) => Ordering::Greater,
|
(Value::Function(_), _) => Ordering::Greater,
|
||||||
(Value::Time(left), Value::Time(right)) => left.cmp(right),
|
(Value::Time(left), Value::Time(right)) => left.cmp(right),
|
||||||
(Value::Time(_), _) => Ordering::Greater,
|
(Value::Time(_), _) => Ordering::Greater,
|
||||||
(Value::Primitive(left), Value::Primitive(right)) => left.cmp(right),
|
(Value::Empty, _) => Ordering::Less,
|
||||||
(Value::Primitive(_), _) => Ordering::Less,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -580,10 +492,10 @@ impl Serialize for Value {
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::String(inner)) => serializer.serialize_str(inner),
|
Value::String(inner) => serializer.serialize_str(inner),
|
||||||
Value::Primitive(Primitive::Float(inner)) => serializer.serialize_f64(*inner),
|
Value::Float(inner) => serializer.serialize_f64(*inner),
|
||||||
Value::Primitive(Primitive::Integer(inner)) => serializer.serialize_i64(*inner),
|
Value::Integer(inner) => serializer.serialize_i64(*inner),
|
||||||
Value::Primitive(Primitive::Boolean(inner)) => serializer.serialize_bool(*inner),
|
Value::Boolean(inner) => serializer.serialize_bool(*inner),
|
||||||
Value::List(inner) => {
|
Value::List(inner) => {
|
||||||
let mut tuple = serializer.serialize_tuple(inner.len())?;
|
let mut tuple = serializer.serialize_tuple(inner.len())?;
|
||||||
|
|
||||||
@ -593,7 +505,7 @@ impl Serialize for Value {
|
|||||||
|
|
||||||
tuple.end()
|
tuple.end()
|
||||||
}
|
}
|
||||||
Value::Primitive(Primitive::Empty) => todo!(),
|
Value::Empty => todo!(),
|
||||||
Value::Map(inner) => inner.serialize(serializer),
|
Value::Map(inner) => inner.serialize(serializer),
|
||||||
Value::Table(inner) => inner.serialize(serializer),
|
Value::Table(inner) => inner.serialize(serializer),
|
||||||
Value::Function(inner) => inner.serialize(serializer),
|
Value::Function(inner) => inner.serialize(serializer),
|
||||||
@ -605,11 +517,11 @@ impl Serialize for Value {
|
|||||||
impl Display for Value {
|
impl Display for Value {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Value::Primitive(Primitive::String(string)) => write!(f, "{string}"),
|
Value::String(string) => write!(f, "{string}"),
|
||||||
Value::Primitive(Primitive::Float(float)) => write!(f, "{}", float),
|
Value::Float(float) => write!(f, "{}", float),
|
||||||
Value::Primitive(Primitive::Integer(int)) => write!(f, "{}", int),
|
Value::Integer(int) => write!(f, "{}", int),
|
||||||
Value::Primitive(Primitive::Boolean(boolean)) => write!(f, "{}", boolean),
|
Value::Boolean(boolean) => write!(f, "{}", boolean),
|
||||||
Value::Primitive(Primitive::Empty) => write!(f, "()"),
|
Value::Empty => write!(f, "()"),
|
||||||
Value::List(list) => {
|
Value::List(list) => {
|
||||||
write!(f, "(")?;
|
write!(f, "(")?;
|
||||||
for value in list {
|
for value in list {
|
||||||
@ -627,31 +539,31 @@ impl Display for Value {
|
|||||||
|
|
||||||
impl From<String> for Value {
|
impl From<String> for Value {
|
||||||
fn from(string: String) -> Self {
|
fn from(string: String) -> Self {
|
||||||
Value::Primitive(Primitive::String(string))
|
Value::String(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Value {
|
impl From<&str> for Value {
|
||||||
fn from(string: &str) -> Self {
|
fn from(string: &str) -> Self {
|
||||||
Value::Primitive(Primitive::String(string.to_string()))
|
Value::String(string.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<f64> for Value {
|
impl From<f64> for Value {
|
||||||
fn from(float: f64) -> Self {
|
fn from(float: f64) -> Self {
|
||||||
Value::Primitive(Primitive::Float(float))
|
Value::Float(float)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<i64> for Value {
|
impl From<i64> for Value {
|
||||||
fn from(int: i64) -> Self {
|
fn from(int: i64) -> Self {
|
||||||
Value::Primitive(Primitive::Integer(int))
|
Value::Integer(int)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<bool> for Value {
|
impl From<bool> for Value {
|
||||||
fn from(boolean: bool) -> Self {
|
fn from(boolean: bool) -> Self {
|
||||||
Value::Primitive(Primitive::Boolean(boolean))
|
Value::Boolean(boolean)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -669,7 +581,7 @@ impl From<Value> for Result<Value> {
|
|||||||
|
|
||||||
impl From<()> for Value {
|
impl From<()> for Value {
|
||||||
fn from(_: ()) -> Self {
|
fn from(_: ()) -> Self {
|
||||||
Value::Primitive(Primitive::Empty)
|
Value::Empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -680,11 +592,11 @@ impl TryFrom<JsonValue> for Value {
|
|||||||
use JsonValue::*;
|
use JsonValue::*;
|
||||||
|
|
||||||
match json_value {
|
match json_value {
|
||||||
Null => Ok(Value::Primitive(Primitive::Empty)),
|
Null => Ok(Value::Empty),
|
||||||
Short(short) => Ok(Value::Primitive(Primitive::String(short.to_string()))),
|
Short(short) => Ok(Value::String(short.to_string())),
|
||||||
String(string) => Ok(Value::Primitive(Primitive::String(string))),
|
String(string) => Ok(Value::String(string)),
|
||||||
Number(number) => Ok(Value::Primitive(Primitive::Float(f64::from(number)))),
|
Number(number) => Ok(Value::Float(f64::from(number))),
|
||||||
Boolean(boolean) => Ok(Value::Primitive(Primitive::Boolean(boolean))),
|
Boolean(boolean) => Ok(Value::Boolean(boolean)),
|
||||||
Object(object) => {
|
Object(object) => {
|
||||||
let mut map = VariableMap::new();
|
let mut map = VariableMap::new();
|
||||||
|
|
||||||
@ -718,11 +630,11 @@ impl TryFrom<&JsonValue> for Value {
|
|||||||
use JsonValue::*;
|
use JsonValue::*;
|
||||||
|
|
||||||
match json_value {
|
match json_value {
|
||||||
Null => Ok(Value::Primitive(Primitive::Empty)),
|
Null => Ok(Value::Empty),
|
||||||
Short(short) => Ok(Value::Primitive(Primitive::String(short.to_string()))),
|
Short(short) => Ok(Value::String(short.to_string())),
|
||||||
String(string) => Ok(Value::Primitive(Primitive::String(string.clone()))),
|
String(string) => Ok(Value::String(string.clone())),
|
||||||
Number(number) => Ok(Value::Primitive(Primitive::Float(f64::from(*number)))),
|
Number(number) => Ok(Value::Float(f64::from(*number))),
|
||||||
Boolean(boolean) => Ok(Value::Primitive(Primitive::Boolean(*boolean))),
|
Boolean(boolean) => Ok(Value::Boolean(*boolean)),
|
||||||
Object(object) => {
|
Object(object) => {
|
||||||
let mut map = VariableMap::new();
|
let mut map = VariableMap::new();
|
||||||
|
|
||||||
@ -753,7 +665,7 @@ impl TryFrom<Value> for String {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
||||||
if let Value::Primitive(Primitive::String(value)) = value {
|
if let Value::String(value) = value {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedString { actual: value })
|
Err(Error::ExpectedString { actual: value })
|
||||||
@ -765,7 +677,7 @@ impl TryFrom<Value> for f64 {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
||||||
if let Value::Primitive(Primitive::Float(value)) = value {
|
if let Value::Float(value) = value {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedFloat { actual: value })
|
Err(Error::ExpectedFloat { actual: value })
|
||||||
@ -777,7 +689,7 @@ impl TryFrom<Value> for i64 {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
||||||
if let Value::Primitive(Primitive::Integer(value)) = value {
|
if let Value::Integer(value) = value {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedInt { actual: value })
|
Err(Error::ExpectedInt { actual: value })
|
||||||
@ -789,7 +701,7 @@ impl TryFrom<Value> for bool {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
fn try_from(value: Value) -> std::result::Result<Self, Self::Error> {
|
||||||
if let Value::Primitive(Primitive::Boolean(value)) = value {
|
if let Value::Boolean(value) = value {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedBoolean { actual: value })
|
Err(Error::ExpectedBoolean { actual: value })
|
||||||
@ -820,7 +732,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(Value::Primitive(Primitive::Boolean(v)))
|
Ok(Value::Boolean(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_i8<E>(self, v: i8) -> std::result::Result<Self::Value, E>
|
fn visit_i8<E>(self, v: i8) -> std::result::Result<Self::Value, E>
|
||||||
@ -848,7 +760,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(Value::Primitive(Primitive::Integer(v)))
|
Ok(Value::Integer(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_i128<E>(self, v: i128) -> std::result::Result<Self::Value, E>
|
fn visit_i128<E>(self, v: i128) -> std::result::Result<Self::Value, E>
|
||||||
@ -856,9 +768,9 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
if v > i64::MAX as i128 {
|
if v > i64::MAX as i128 {
|
||||||
Ok(Value::Primitive(Primitive::Integer(i64::MAX)))
|
Ok(Value::Integer(i64::MAX))
|
||||||
} else {
|
} else {
|
||||||
Ok(Value::Primitive(Primitive::Integer(v as i64)))
|
Ok(Value::Integer(v as i64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -908,7 +820,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(Value::Primitive(Primitive::Float(v)))
|
Ok(Value::Float(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_char<E>(self, v: char) -> std::result::Result<Self::Value, E>
|
fn visit_char<E>(self, v: char) -> std::result::Result<Self::Value, E>
|
||||||
@ -922,7 +834,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(Value::Primitive(Primitive::String(v.to_string())))
|
Ok(Value::String(v.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
|
fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
|
||||||
@ -936,7 +848,7 @@ impl<'de> Visitor<'de> for ValueVisitor {
|
|||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(Value::Primitive(Primitive::String(v)))
|
Ok(Value::String(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
|
fn visit_bytes<E>(self, v: &[u8]) -> std::result::Result<Self::Value, E>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{Error, Primitive, Result, Value, VariableMap};
|
use crate::{Error, Result, Value, VariableMap};
|
||||||
use comfy_table::{Cell, Color, ContentArrangement, Table as ComfyTable};
|
use comfy_table::{Cell, Color, ContentArrangement, Table as ComfyTable};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
@ -114,7 +114,7 @@ impl Table {
|
|||||||
|
|
||||||
if let Some(index) = new_table_column_index {
|
if let Some(index) = new_table_column_index {
|
||||||
while new_row.len() < index + 1 {
|
while new_row.len() < index + 1 {
|
||||||
new_row.push(Value::Primitive(Primitive::Empty));
|
new_row.push(Value::Empty);
|
||||||
}
|
}
|
||||||
new_row[index] = value.clone();
|
new_row[index] = value.clone();
|
||||||
}
|
}
|
||||||
@ -196,7 +196,7 @@ impl Display for Table {
|
|||||||
Value::Map(map) => format!("Map ({} items)", map.len()),
|
Value::Map(map) => format!("Map ({} items)", map.len()),
|
||||||
Value::Table(table) => format!("Table ({} items)", table.len()),
|
Value::Table(table) => format!("Table ({} items)", table.len()),
|
||||||
Value::Function(_) => "Function".to_string(),
|
Value::Function(_) => "Function".to_string(),
|
||||||
Value::Primitive(Primitive::Empty) => "Empty".to_string(),
|
Value::Empty => "Empty".to_string(),
|
||||||
value => value.to_string(),
|
value => value.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -236,46 +236,38 @@ impl Display for Table {
|
|||||||
impl From<&Value> for Table {
|
impl From<&Value> for Table {
|
||||||
fn from(value: &Value) -> Self {
|
fn from(value: &Value) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Value::Primitive(Primitive::String(string)) => {
|
Value::String(string) => {
|
||||||
let mut table = Table::new(vec!["string".to_string()]);
|
let mut table = Table::new(vec!["string".to_string()]);
|
||||||
|
|
||||||
table
|
table
|
||||||
.insert(vec![Value::Primitive(Primitive::String(
|
.insert(vec![Value::String(string.to_string())])
|
||||||
string.to_string(),
|
|
||||||
))])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
table
|
table
|
||||||
}
|
}
|
||||||
Value::Primitive(Primitive::Float(float)) => {
|
Value::Float(float) => {
|
||||||
let mut table = Table::new(vec!["float".to_string()]);
|
let mut table = Table::new(vec!["float".to_string()]);
|
||||||
|
|
||||||
table
|
table.insert(vec![Value::Float(*float)]).unwrap();
|
||||||
.insert(vec![Value::Primitive(Primitive::Float(*float))])
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
table
|
table
|
||||||
}
|
}
|
||||||
Value::Primitive(Primitive::Integer(integer)) => {
|
Value::Integer(integer) => {
|
||||||
let mut table = Table::new(vec!["integer".to_string()]);
|
let mut table = Table::new(vec!["integer".to_string()]);
|
||||||
|
|
||||||
table
|
table.insert(vec![Value::Integer(*integer)]).unwrap();
|
||||||
.insert(vec![Value::Primitive(Primitive::Integer(*integer))])
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
table
|
table
|
||||||
}
|
}
|
||||||
Value::Primitive(Primitive::Boolean(boolean)) => {
|
Value::Boolean(boolean) => {
|
||||||
let mut table = Table::new(vec!["boolean".to_string()]);
|
let mut table = Table::new(vec!["boolean".to_string()]);
|
||||||
|
|
||||||
table
|
table.insert(vec![Value::Boolean(*boolean)]).unwrap();
|
||||||
.insert(vec![Value::Primitive(Primitive::Boolean(*boolean))])
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
table
|
table
|
||||||
}
|
}
|
||||||
Value::List(list) => Self::from(list),
|
Value::List(list) => Self::from(list),
|
||||||
Value::Primitive(Primitive::Empty) => Table::new(Vec::with_capacity(0)),
|
Value::Empty => Table::new(Vec::with_capacity(0)),
|
||||||
Value::Map(map) => Self::from(map),
|
Value::Map(map) => Self::from(map),
|
||||||
Value::Table(table) => table.clone(),
|
Value::Table(table) => table.clone(),
|
||||||
Value::Function(function) => {
|
Value::Function(function) => {
|
||||||
@ -298,10 +290,7 @@ impl From<&Vec<Value>> for Table {
|
|||||||
|
|
||||||
for (i, value) in list.iter().enumerate() {
|
for (i, value) in list.iter().enumerate() {
|
||||||
table
|
table
|
||||||
.insert(vec![
|
.insert(vec![Value::Integer(i as i64), value.clone()])
|
||||||
Value::Primitive(Primitive::Integer(i as i64)),
|
|
||||||
value.clone(),
|
|
||||||
])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,10 +307,7 @@ impl From<&mut Vec<Value>> for Table {
|
|||||||
table.insert(list.clone()).unwrap();
|
table.insert(list.clone()).unwrap();
|
||||||
} else {
|
} else {
|
||||||
table
|
table
|
||||||
.insert(vec![
|
.insert(vec![Value::Integer(i as i64), value.clone()])
|
||||||
Value::Primitive(Primitive::Integer(i as i64)),
|
|
||||||
value.clone(),
|
|
||||||
])
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::fmt::{self, Debug, Display, Formatter};
|
use std::fmt::{self, Debug, Display, Formatter};
|
||||||
|
|
||||||
use crate::{Primitive, Value};
|
use crate::Value;
|
||||||
|
|
||||||
/// The type of a `Value`.
|
/// The type of a `Value`.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -94,11 +94,11 @@ impl Debug for ValueType {
|
|||||||
impl From<&Value> for ValueType {
|
impl From<&Value> for ValueType {
|
||||||
fn from(value: &Value) -> Self {
|
fn from(value: &Value) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Value::Primitive(Primitive::String(_)) => ValueType::String,
|
Value::String(_) => ValueType::String,
|
||||||
Value::Primitive(Primitive::Float(_)) => ValueType::Float,
|
Value::Float(_) => ValueType::Float,
|
||||||
Value::Primitive(Primitive::Integer(_)) => ValueType::Integer,
|
Value::Integer(_) => ValueType::Integer,
|
||||||
Value::Primitive(Primitive::Boolean(_)) => ValueType::Boolean,
|
Value::Boolean(_) => ValueType::Boolean,
|
||||||
Value::Primitive(Primitive::Empty) => ValueType::Empty,
|
Value::Empty => ValueType::Empty,
|
||||||
Value::List(list) => {
|
Value::List(list) => {
|
||||||
let values = list.iter().map(|value| value.value_type()).collect();
|
let values = list.iter().map(|value| value.value_type()).collect();
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ use std::{
|
|||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{value::Value, Error, Primitive, Result, Table};
|
use crate::{value::Value, Error, Result, Table};
|
||||||
|
|
||||||
/// A collection dust variables comprised of key-value pairs.
|
/// A collection dust variables comprised of key-value pairs.
|
||||||
///
|
///
|
||||||
@ -77,9 +77,9 @@ impl VariableMap {
|
|||||||
let index = if let Ok(index) = next_identifier.parse::<usize>() {
|
let index = if let Ok(index) = next_identifier.parse::<usize>() {
|
||||||
index
|
index
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::expected_int(Value::Primitive(Primitive::String(
|
return Err(Error::expected_int(Value::String(
|
||||||
next_identifier.to_string(),
|
next_identifier.to_string(),
|
||||||
))));
|
)));
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(list.get(index).cloned())
|
Ok(list.get(index).cloned())
|
||||||
@ -106,9 +106,9 @@ impl VariableMap {
|
|||||||
let index = if let Ok(index) = next_identifier.parse::<usize>() {
|
let index = if let Ok(index) = next_identifier.parse::<usize>() {
|
||||||
index
|
index
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::expected_int(Value::Primitive(Primitive::String(
|
return Err(Error::expected_int(Value::String(
|
||||||
next_identifier.to_string(),
|
next_identifier.to_string(),
|
||||||
))));
|
)));
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut missing_elements = index.saturating_sub(list.len()) + 1;
|
let mut missing_elements = index.saturating_sub(list.len()) + 1;
|
||||||
@ -193,13 +193,9 @@ mod tests {
|
|||||||
fn get_and_set_simple_value() {
|
fn get_and_set_simple_value() {
|
||||||
let mut map = VariableMap::new();
|
let mut map = VariableMap::new();
|
||||||
|
|
||||||
map.set_value("x".to_string(), Value::Primitive(Primitive::Integer(1)))
|
map.set_value("x".to_string(), Value::Integer(1)).unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(Value::Integer(1), map.get_value("x").unwrap().unwrap());
|
||||||
Value::Primitive(Primitive::Integer(1)),
|
|
||||||
map.get_value("x").unwrap().unwrap()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user