Lines
89.87 %
Functions
100 %
Branches
//! Tag-domain natives. Wraps `server::command::tag::{DeleteTag, ListTags}`;
//! `list-tags` is render-safe, `delete-tag` is not.
use finance::tag::Tag;
use scripting::runtime::{
alloc_entity_via_export, alloc_pair_chain, alloc_string_ref, read_string_arg,
};
use server::command::tag::{DeleteTag, ListTags};
use server::command::{CmdError, CmdResult, FinanceEntity};
use uuid::Uuid;
use wasmtime::{AnyRef, ArrayRef, Caller, Linker, Rooted, StructRef, Val};
use crate::host_failure::cmd_failure;
use crate::natives::args::uuid_arg;
use crate::natives::native::register_native;
use crate::session::SessionData;
pub const REGISTERED_COMMANDS: &[&str] = &["delete-tag", "list-tags"];
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
register_readonly(linker)?;
register_native(
linker,
"tag_delete_tag",
|caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
Box::pin(async move {
let user_id = caller.data().ctx().user_id;
let id = read_string_arg(caller, id_arg)?;
run_delete_tag(user_id, id).await
})
},
)?;
Ok(())
}
pub fn register_readonly(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
register_native(linker, "tag_list_tags", |caller, ()| {
let result = ListTags::new().user_id(user_id).run().await;
let tags = list_tag_entries("list-tags", result)?;
alloc_tag_chain(caller, tags).await
/// Removes the tag and every link to it, whatever uses it; an id the user does
/// not hold is `not-found`. Returns `t` on success.
pub(super) async fn run_delete_tag(user_id: Uuid, id_arg: Option<String>) -> wasmtime::Result<i32> {
let tag_id = uuid_arg("delete-tag", "tag-id", id_arg)?;
DeleteTag::new()
.user_id(user_id)
.tag_id(tag_id)
.run()
.await
.map(|_| 1)
.map_err(|err| cmd_failure("delete-tag", err))
fn list_tag_entries(
name: &str,
result: Result<Option<CmdResult>, CmdError>,
) -> wasmtime::Result<Vec<Tag>> {
match result {
Ok(Some(CmdResult::Entities(entities))) => Ok(entities
.into_iter()
.filter_map(|entity| match entity {
FinanceEntity::Tag(tag) => Some(tag),
_ => None,
.collect()),
Ok(Some(other)) => Err(wasmtime::Error::msg(format!(
"{name}: expected Entities, got {other:?}"
))),
Ok(None) => Ok(Vec::new()),
Err(err) => Err(cmd_failure(name, err)),
/// An unset description rides as a null string ref.
async fn alloc_tag_entity(
caller: &mut Caller<'_, SessionData>,
tag: &Tag,
) -> wasmtime::Result<Rooted<StructRef>> {
let id_ref = alloc_string_ref(caller, tag.id.to_string().as_bytes())?;
let name_ref = alloc_string_ref(caller, tag.tag_name.as_bytes())?;
let value_ref = alloc_string_ref(caller, tag.tag_value.as_bytes())?;
let description_ref = tag
.description
.as_deref()
.map(|s| alloc_string_ref(caller, s.as_bytes()))
.transpose()?;
let args = [
Val::AnyRef(Some(id_ref.to_anyref())),
Val::AnyRef(Some(name_ref.to_anyref())),
Val::AnyRef(Some(value_ref.to_anyref())),
Val::AnyRef(description_ref.map(|r| r.to_anyref())),
];
alloc_entity_via_export(caller, "alloc_tag_entity", &args).await
async fn alloc_tag_chain(
tags: Vec<Tag>,
) -> wasmtime::Result<Option<Rooted<StructRef>>> {
let mut anyrefs: Vec<Option<Rooted<AnyRef>>> = Vec::with_capacity(tags.len());
for tag in &tags {
let entity_ref = alloc_tag_entity(caller, tag).await?;
anyrefs.push(Some(entity_ref.to_anyref()));
alloc_pair_chain(caller, anyrefs).await