1
0
dust/src/abstract_tree/built_in_value.rs

168 lines
5.6 KiB
Rust
Raw Normal View History

2024-01-06 13:11:09 +00:00
use std::{env::args, sync::OnceLock};
2024-01-01 10:20:11 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2024-01-01 13:52:25 +00:00
use crate::{
2024-01-06 13:11:09 +00:00
built_in_functions::string_functions, AbstractTree, BuiltInFunction, Format, Function, List,
Map, Result, Type, Value,
2024-01-01 13:52:25 +00:00
};
2024-01-01 10:20:11 +00:00
static ARGS: OnceLock<Value> = OnceLock::new();
2024-01-01 12:46:47 +00:00
static FS: OnceLock<Value> = OnceLock::new();
static JSON: OnceLock<Value> = OnceLock::new();
2024-01-01 10:20:11 +00:00
static RANDOM: OnceLock<Value> = OnceLock::new();
2024-01-01 13:52:25 +00:00
static STRING: OnceLock<Value> = OnceLock::new();
2024-01-01 10:20:11 +00:00
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum BuiltInValue {
Args,
AssertEqual,
2024-01-01 12:46:47 +00:00
Fs,
Json,
2024-01-01 10:20:11 +00:00
Length,
Output,
Random,
2024-01-01 13:52:25 +00:00
String,
2024-01-01 10:20:11 +00:00
}
impl BuiltInValue {
2024-01-06 13:11:09 +00:00
pub fn name(&self) -> &'static str {
match self {
BuiltInValue::Args => "args",
BuiltInValue::AssertEqual => "assert_equal",
BuiltInValue::Fs => "fs",
BuiltInValue::Json => "json",
BuiltInValue::Length => "length",
BuiltInValue::Output => "output",
BuiltInValue::Random => "random",
BuiltInValue::String => "string",
}
}
2024-01-01 10:20:11 +00:00
fn r#type(&self) -> Type {
match self {
2024-01-01 14:39:59 +00:00
BuiltInValue::Args => Type::list(Type::String),
2024-01-01 10:20:11 +00:00
BuiltInValue::AssertEqual => BuiltInFunction::AssertEqual.r#type(),
2024-01-06 08:47:54 +00:00
BuiltInValue::Fs => Type::Map(None),
BuiltInValue::Json => Type::Map(None),
2024-01-01 10:20:11 +00:00
BuiltInValue::Length => BuiltInFunction::Length.r#type(),
BuiltInValue::Output => BuiltInFunction::Output.r#type(),
2024-01-06 08:47:54 +00:00
BuiltInValue::Random => Type::Map(None),
BuiltInValue::String => Type::Map(None),
2024-01-01 10:20:11 +00:00
}
}
fn get(&self) -> &Value {
match self {
BuiltInValue::Args => ARGS.get_or_init(|| {
let args = args().map(|arg| Value::string(arg.to_string())).collect();
2024-01-01 10:20:11 +00:00
Value::List(List::with_items(args))
}),
BuiltInValue::AssertEqual => {
&Value::Function(Function::BuiltIn(BuiltInFunction::AssertEqual))
}
2024-01-01 12:46:47 +00:00
BuiltInValue::Fs => FS.get_or_init(|| {
let fs_context = Map::new();
fs_context
.set(
"read".to_string(),
Value::Function(Function::BuiltIn(BuiltInFunction::FsRead)),
)
.unwrap();
Value::Map(fs_context)
}),
BuiltInValue::Json => JSON.get_or_init(|| {
let json_context = Map::new();
json_context
.set(
2024-01-06 13:11:09 +00:00
BuiltInFunction::JsonParse.name().to_string(),
2024-01-01 12:46:47 +00:00
Value::Function(Function::BuiltIn(BuiltInFunction::JsonParse)),
)
.unwrap();
Value::Map(json_context)
}),
2024-01-01 10:20:11 +00:00
BuiltInValue::Length => &Value::Function(Function::BuiltIn(BuiltInFunction::Length)),
BuiltInValue::Output => &Value::Function(Function::BuiltIn(BuiltInFunction::Output)),
BuiltInValue::Random => RANDOM.get_or_init(|| {
let random_context = Map::new();
{
let mut variables = random_context.variables_mut().unwrap();
2024-01-01 12:46:47 +00:00
for built_in_function in [
BuiltInFunction::RandomBoolean,
BuiltInFunction::RandomFloat,
BuiltInFunction::RandomFrom,
BuiltInFunction::RandomInteger,
] {
2024-01-01 10:20:11 +00:00
let key = built_in_function.name().to_string();
let value = Value::Function(Function::BuiltIn(built_in_function));
let r#type = built_in_function.r#type();
variables.insert(key, (value, r#type));
}
}
Value::Map(random_context)
}),
2024-01-01 13:52:25 +00:00
BuiltInValue::String => STRING.get_or_init(|| {
let string_context = Map::new();
{
let mut variables = string_context.variables_mut().unwrap();
2024-01-01 14:39:59 +00:00
for string_function in string_functions() {
2024-01-01 13:52:25 +00:00
let key = string_function.name().to_string();
let value = Value::Function(Function::BuiltIn(BuiltInFunction::String(
string_function,
)));
let r#type = string_function.r#type();
variables.insert(key, (value, r#type));
}
}
Value::Map(string_context)
}),
2024-01-01 10:20:11 +00:00
}
}
}
impl AbstractTree for BuiltInValue {
fn from_syntax_node(_source: &str, node: Node, _context: &Map) -> Result<Self> {
let built_in_value = match node.kind() {
"args" => BuiltInValue::Args,
"assert_equal" => BuiltInValue::AssertEqual,
2024-01-01 12:46:47 +00:00
"fs" => BuiltInValue::Fs,
"json" => BuiltInValue::Json,
2024-01-01 10:20:11 +00:00
"length" => BuiltInValue::Length,
"output" => BuiltInValue::Output,
"random" => BuiltInValue::Random,
2024-01-01 13:52:25 +00:00
"string" => BuiltInValue::String,
2024-01-01 10:20:11 +00:00
_ => todo!(),
};
Ok(built_in_value)
}
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
Ok(self.get().clone())
}
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(self.r#type())
}
}
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
impl Format for BuiltInValue {
2024-01-06 13:53:31 +00:00
fn format(&self, output: &mut String, _indent_level: u8) {
2024-01-06 13:11:09 +00:00
output.push_str(&self.get().to_string());
2024-01-06 10:00:36 +00:00
}
}