Files
komp_ac/server/src/steel/server/execution.rs
2025-03-11 18:52:58 +01:00

46 lines
1.3 KiB
Rust

// src/steel/server/execution.rs
use steel::steel_vm::engine::Engine;
use steel::rvals::SteelVal;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ExecutionError {
#[error("Script execution failed: {0}")]
RuntimeError(String),
#[error("Type conversion error: {0}")]
TypeConversionError(String),
#[error("Unsupported target type: {0}")]
UnsupportedType(String),
}
#[derive(Debug)]
pub enum Value {
String(String),
}
pub fn execute_script(
script: String,
target_type: &str,
) -> Result<Value, ExecutionError> {
let mut vm = Engine::new();
let results = vm.compile_and_run_raw_program(script)
.map_err(|e| ExecutionError::RuntimeError(e.to_string()))?;
let last_result = results.last()
.ok_or_else(|| ExecutionError::TypeConversionError("Script returned no values".to_string()))?;
match target_type {
"STRING" => {
if let SteelVal::StringV(s) = last_result {
Ok(Value::String(s.to_string()))
} else {
Err(ExecutionError::TypeConversionError(
format!("Expected string, got {:?}", last_result)
))
}
}
_ => Err(ExecutionError::UnsupportedType(target_type.to_string())),
}
}