use super::error::ScriptError;
use crate::script_error;
use evalexpr::Value;
use json5_nodes::{Iter, JsonNode, Location};
#[allow(dead_code)]
pub(crate) trait JsonNodeExtra {
fn is_null(&self) -> bool;
fn is_bool(&self) -> bool;
fn is_integer(&self) -> bool;
fn is_float(&self) -> bool;
fn is_string(&self) -> bool;
fn is_array(&self) -> bool;
fn is_object(&self) -> bool;
fn get_location(&self) -> Option<Location>;
fn get_object_entry(&self, name: &str) -> Result<&JsonNode, ScriptError>;
fn get_object_iter<'a>(&'a self) -> Result<Iter<'a, String, JsonNode>, ScriptError>;
fn get_array_iter<'a>(&'a self) -> Result<std::slice::Iter<'a, JsonNode>, ScriptError>;
fn get_value(&self) -> Value;
fn get_string(&self) -> String;
}
impl JsonNodeExtra for JsonNode {
fn is_null(&self) -> bool {
if let JsonNode::Null(_) = self {
true
} else {
false
}
}
fn is_bool(&self) -> bool {
if let JsonNode::Bool(_, _) = self {
true
} else {
false
}
}
fn is_integer(&self) -> bool {
if let JsonNode::Integer(_, _) = self {
true
} else {
false
}
}
fn is_float(&self) -> bool {
if let JsonNode::Float(_, _) = self {
true
} else {
false
}
}
fn is_string(&self) -> bool {
if let JsonNode::String(_, _) = self {
true
} else {
false
}
}
fn is_array(&self) -> bool {
if let JsonNode::Array(_, _) = self {
true
} else {
false
}
}
fn is_object(&self) -> bool {
if let JsonNode::Object(_, _) = self {
true
} else {
false
}
}
fn get_location(&self) -> Option<Location> {
match self {
JsonNode::Null(location)
| JsonNode::Bool(_, location)
| JsonNode::Integer(_, location)
| JsonNode::Float(_, location)
| JsonNode::String(_, location)
| JsonNode::Array(_, location)
| JsonNode::Object(_, location) => *location,
}
}
fn get_object_entry<'a>(self: &'a Self, name: &str) -> Result<&'a JsonNode, ScriptError> {
if let JsonNode::Object(map, ..) = self {
if let Some(node) = map.get(name) {
Ok(node)
} else {
Err(script_error!(
format!("Object entry '{}' not found", name),
self
))
}
} else {
Err(script_error!("Not an object", self))
}
}
fn get_object_iter<'a>(&'a self) -> Result<Iter<'a, String, JsonNode>, ScriptError> {
if let JsonNode::Object(map, _) = self {
Ok(map.iter())
} else {
Err(script_error!("Not an object", self))
}
}
fn get_array_iter<'a>(&'a self) -> Result<std::slice::Iter<'a, JsonNode>, ScriptError> {
if let JsonNode::Array(array, _) = self {
Ok(array.iter())
} else {
Err(script_error!("Not an array", self))
}
}
fn get_value(self: &Self) -> Value {
match self {
JsonNode::Null(..) => Value::Empty,
JsonNode::Integer(value, ..) => Value::Int(*value as i64),
JsonNode::Float(value, ..) => Value::Float(*value as f64),
JsonNode::Bool(value, ..) => Value::from(*value),
JsonNode::String(value, ..) => Value::from((*value).to_owned()),
_ => Value::Empty,
}
}
fn get_string(self: &Self) -> String {
match self {
JsonNode::Null(..) => "null".to_string(),
JsonNode::Integer(value, ..) => (*value).to_string(),
JsonNode::Float(value, ..) => (*value).to_string(),
JsonNode::Bool(value, ..) => (*value).to_string(),
JsonNode::String(value, ..) => (*value).to_string(),
_ => String::new(),
}
}
}