1
//! `script-edit` submit: syntax, compile check, then the one `EditScript`
2
//! command under the script's key locks.
3

            
4
use forms::{FormValues, SCRIPT_EDIT};
5
use server::command::script::EditScript;
6
use server::command::{CmdError, CmdResult};
7
use server::script_state::STALE_SCRIPT;
8
use server::user::User;
9
use uuid::Uuid;
10

            
11
use super::Failure;
12
use super::script_create::{compiled, enabled, named};
13
use super::transaction_rows::error_at;
14

            
15
const CTX: &str = "submit-form";
16

            
17
/// Validate `script-edit` values and apply the name, the source and the state.
18
3211
pub(super) async fn edit_script(user_id: Uuid, values: &FormValues) -> Result<Uuid, Failure> {
19
3211
    refuse_bytecode_only_without_source(user_id, values).await?;
20
2569
    let errors = values.check(&SCRIPT_EDIT);
21
2569
    if !errors.is_empty() {
22
1
        return Err(errors.into());
23
2568
    }
24
4655
    let text = |key| values.text(key).unwrap_or_default();
25
2568
    let script_id = Uuid::parse_str(text("id").trim())
26
        .map_err(|_| error_at("id".to_string(), "unknown script"))?;
27
2568
    let (source, bytecode) = compiled(values)?;
28
2087
    let mut command = EditScript::new()
29
2087
        .user_id(user_id)
30
2087
        .script_id(script_id)
31
2087
        .source(source)
32
2087
        .bytecode(bytecode)
33
2087
        .enabled(enabled(values))
34
2087
        .revision(text("revision").trim().to_string());
35
2087
    if let Some(name) = named(values) {
36
2084
        command = command.name(name);
37
3
    }
38
2087
    match command.run().await {
39
1764
        Ok(Some(CmdResult::Uuid(id))) => Ok(id),
40
        Ok(other) => Err(Failure::Internal(wasmtime::Error::msg(format!(
41
            "{CTX}: unexpected edit result {other:?}"
42
        )))),
43
323
        Err(err) => Err(edit_failure(err)),
44
    }
45
3211
}
46

            
47
/// A script stored without source cannot be saved with an empty one.
48
3211
async fn refuse_bytecode_only_without_source(
49
3211
    user_id: Uuid,
50
3211
    values: &FormValues,
51
3211
) -> Result<(), Failure> {
52
3211
    let empty = values
53
3211
        .text("source")
54
3211
        .is_none_or(|text| text.trim().is_empty());
55
3211
    let id = values
56
3211
        .text("id")
57
3211
        .and_then(|id| Uuid::parse_str(id.trim()).ok());
58
3211
    let Some(id) = id.filter(|_| empty) else {
59
2568
        return Ok(());
60
    };
61
643
    let user = User { id: user_id };
62
643
    match user.get_script(id).await {
63
643
        Ok(stored) if stored.source.is_none() => Err(error_at(
64
642
            "source".to_string(),
65
642
            "this script was uploaded as bytecode and has no source; supply its source to save it",
66
642
        )
67
642
        .into()),
68
1
        _ => Ok(()),
69
    }
70
3211
}
71

            
72
/// The stale revision is a field error and an unknown script one at `id`; the
73
/// rest as elsewhere.
74
323
fn edit_failure(err: CmdError) -> Failure {
75
323
    match err {
76
321
        CmdError::Args(message) if message == STALE_SCRIPT => {
77
321
            error_at("revision".to_string(), message).into()
78
        }
79
2
        err if err.is_missing_row() => error_at("id".to_string(), "unknown script").into(),
80
        other => Failure::from_command(CTX, other),
81
    }
82
323
}