use crate::script::*;
#[derive(Debug, Clone)] #[non_exhaustive]
pub enum ArgBy {
Property(CompactString),
Traversal(ScriptGraphTraversalSource),
Label,
Id,
Value,
Key,
Count,
JsonPath(crate::value::JsonPath),
Column(Column),
Order(Order),
Labels(Vec<CompactString>),
}
#[export_module]
#[allow(non_snake_case, non_upper_case_globals)]
pub(crate) mod ArgByModule {
use super::ArgBy;
use crate::value::literal::TokenLiteral;
pub fn Property(prop: String) -> ArgBy {
ArgBy::Property(CompactString::new(prop))
}
pub fn Traversal(prop: ScriptGraphTraversalSource) -> ArgBy {
ArgBy::Traversal(prop)
}
pub const Label: ArgBy = ArgBy::Label;
pub const Id: ArgBy = ArgBy::Id;
pub const Value: ArgBy = ArgBy::Value;
pub const Count: ArgBy = ArgBy::Count;
#[rhai_fn(return_raw)]
pub fn JsonPath(path: String) -> Result<ArgBy, Box<rhai::EvalAltResult>> {
path.parse::<crate::value::JsonPath>()
.map(ArgBy::JsonPath)
.map_err(|err| {
Box::new(rhai::EvalAltResult::ErrorRuntime(
rhai::Dynamic::from(crate::error::format_diagnostic(&err)),
rhai::Position::NONE,
))
})
}
#[rhai_fn(global, get = "enum_type", pure)]
pub fn get_type(arg_by: &mut ArgBy) -> String {
match arg_by {
ArgBy::Property(_) => "Property",
ArgBy::Label => "Label",
ArgBy::Id => "Id",
ArgBy::Value => "Value",
ArgBy::Key => "Key",
ArgBy::Count => "Count",
ArgBy::Traversal(_) => "Traversal",
ArgBy::JsonPath(_) => "JsonPath",
ArgBy::Order(_) => "Order",
ArgBy::Labels(_) => "Labels",
ArgBy::Column(_) => "Column",
}
.to_string()
}
#[rhai_fn(global, get = "value", pure)]
pub fn get_value(arg_by: &mut ArgBy) -> Dynamic {
match arg_by {
ArgBy::Id => Dynamic::UNIT,
ArgBy::Label => Dynamic::UNIT,
ArgBy::Value => Dynamic::UNIT,
ArgBy::Key => Dynamic::UNIT,
ArgBy::Count => Dynamic::UNIT,
ArgBy::Property(p) => Dynamic::from(p.to_string()),
ArgBy::Traversal(p) => Dynamic::from(p.clone()),
ArgBy::JsonPath(p) => Dynamic::from(p.to_string()),
ArgBy::Order(o) => Dynamic::from(o.clone()),
ArgBy::Column(column) => Dynamic::from(*column),
ArgBy::Labels(labels) => Dynamic::from_array(
labels
.iter()
.map(|label| Dynamic::from(label.to_string()))
.collect(),
),
}
}
#[rhai_fn(global, name = "to_string", name = "to_debug", pure)]
pub fn to_string(arg_by: &mut ArgBy) -> String {
arg_by.token_literal()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptBy;
impl crate::value::literal::TokenLiteral for ArgBy {
fn token_literal(&self) -> String {
use crate::value::literal::{rhai_string, rhai_string_array};
match self {
ArgBy::Property(key) => format!("By.Property({})", rhai_string(key)),
ArgBy::Traversal(traversal) => {
format!("By.Traversal({})", traversal.token_literal())
}
ArgBy::Label => "T.label".to_string(),
ArgBy::Id => "T.id".to_string(),
ArgBy::Value => "T.value".to_string(),
ArgBy::Key => "T.key".to_string(),
ArgBy::Count => "By.Count".to_string(),
ArgBy::JsonPath(path) => format!("By.JsonPath({})", rhai_string(&path.to_string())),
ArgBy::Column(column) => column.token_literal(),
ArgBy::Order(order) => order.token_literal(),
ArgBy::Labels(labels) => rhai_string_array(labels),
}
}
}