1
//! Template storage natives. Wraps `server::command::template::DeleteTemplate`;
2
//! not render-safe, so it is never linked into the render surface.
3

            
4
use scripting::runtime::read_string_arg;
5
use server::command::template::DeleteTemplate;
6
use uuid::Uuid;
7
use wasmtime::{ArrayRef, Linker, Rooted};
8

            
9
use crate::host_failure::cmd_failure;
10
use crate::natives::args::uuid_arg;
11
use crate::natives::native::register_native;
12
use crate::session::SessionData;
13

            
14
pub const REGISTERED_COMMANDS: &[&str] = &["delete-template"];
15

            
16
230199
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
17
230199
    register_native(
18
230199
        linker,
19
230199
        "template_store_delete_template",
20
323
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
21
323
            Box::pin(async move {
22
323
                let user_id = caller.data().ctx().user_id;
23
323
                let id = read_string_arg(caller, id_arg)?;
24
323
                run_delete_template(user_id, id).await
25
323
            })
26
323
        },
27
    )
28
230199
}
29

            
30
/// Removes the template; an id the user does not hold is `not-found`. Returns
31
/// `t` on success.
32
323
pub(super) async fn run_delete_template(
33
323
    user_id: Uuid,
34
323
    id_arg: Option<String>,
35
323
) -> wasmtime::Result<i32> {
36
323
    let template_id = uuid_arg("delete-template", "template-id", id_arg)?;
37
322
    DeleteTemplate::new()
38
322
        .user_id(user_id)
39
322
        .template_id(template_id)
40
322
        .run()
41
322
        .await
42
        .map(|_| 1)
43
1
        .map_err(|err| cmd_failure("delete-template", err))
44
323
}