Lines
100 %
Functions
8.68 %
Branches
//! Script rows as the script forms and natives edit them. The caller compiles
//! the source; the commands store source and bytecode together.
use sqlx::types::Uuid;
use supp_macro::command;
use super::{CmdError, CmdResult};
use crate::error::ServerError;
use crate::script_run::{RunError, run_on_account};
use crate::script_state::STALE_SCRIPT;
use crate::user::User;
command! {
CreateScript {
#[required]
user_id: Uuid,
source: String,
bytecode: Vec<u8>,
enabled: bool,
#[optional]
name: String,
} => {
let id = User { id: user_id }
.create_script_from_source(source, bytecode, name, enabled)
.await?;
Ok(Some(CmdResult::Uuid(id)))
}
EditScript {
script_id: Uuid,
revision: String,
let updated = User { id: user_id }
.update_script(script_id, &revision, name, enabled, (&source, &bytecode))
.await;
match updated {
Err(ServerError::StaleScript) => Err(CmdError::Args(STALE_SCRIPT.to_string())),
other => other.map(|()| Some(CmdResult::Uuid(script_id))).map_err(Into::into),
DeleteScript {
match (User { id: user_id }).delete_script(script_id).await? {
true => Ok(Some(CmdResult::Bool(true))),
false => Err(CmdError::NotFound(format!("no script {script_id}"))),
RunScriptOnAccount {
account_id: Uuid,
match run_on_account(&User { id: user_id }, script_id, account_id).await {
Ok(run) => Ok(Some(CmdResult::ScriptRun(run))),
Err(err @ RunError::Refused { .. }) => Err(CmdError::Refused(err.to_string())),
Err(err @ RunError::ScriptNotFound) => Err(CmdError::NotFound(err.to_string())),
Err(RunError::Failed { source: err, .. } | RunError::Server(err)) => Err(err.into()),