Skip to main content

rpc/natives/
template_store.rs

1//! Template storage natives. Wraps `server::command::template::DeleteTemplate`;
2//! not render-safe, so it is never linked into the render surface.
3
4use scripting::runtime::read_string_arg;
5use server::command::template::DeleteTemplate;
6use uuid::Uuid;
7use wasmtime::{ArrayRef, Linker, Rooted};
8
9use crate::host_failure::cmd_failure;
10use crate::natives::args::uuid_arg;
11use crate::natives::native::register_native;
12use crate::session::SessionData;
13
14pub const REGISTERED_COMMANDS: &[&str] = &["delete-template"];
15
16pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
17    register_native(
18        linker,
19        "template_store_delete_template",
20        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
21            Box::pin(async move {
22                let user_id = caller.data().ctx().user_id;
23                let id = read_string_arg(caller, id_arg)?;
24                run_delete_template(user_id, id).await
25            })
26        },
27    )
28}
29
30/// Removes the template; an id the user does not hold is `not-found`. Returns
31/// `t` on success.
32pub(super) async fn run_delete_template(
33    user_id: Uuid,
34    id_arg: Option<String>,
35) -> wasmtime::Result<i32> {
36    let template_id = uuid_arg("delete-template", "template-id", id_arg)?;
37    DeleteTemplate::new()
38        .user_id(user_id)
39        .template_id(template_id)
40        .run()
41        .await
42        .map(|_| 1)
43        .map_err(|err| cmd_failure("delete-template", err))
44}