Lines
83.33 %
Functions
18.12 %
Branches
100 %
//! `SetCommodityTag` and `ClearCommodityTag` on the tag lock protocol.
use crate::command::connection;
use finance::tag::{Tag, normalize};
use sqlx::types::Uuid;
use supp_macro::command;
use super::attempt::settled;
use super::{CmdError, CmdResult};
use crate::attempt::Tries;
use crate::{tag::owned::Owner, user::User};
/// Names every commodity keeps: they identify it.
pub const IDENTITY_TAGS: [&str; 2] = ["symbol", "name"];
pub(super) fn not_found() -> CmdError {
CmdError::NotFound("Commodity not found".to_string())
}
command! {
SetCommodityTag {
#[required]
user_id: Uuid,
commodity_id: Uuid,
tag_name: String,
tag_value: String,
#[optional]
description: String,
} => {
let tag = Tag {
id: Uuid::new_v4(),
tag_name,
tag_value,
description: description.filter(|text| !text.trim().is_empty()),
};
if tag.is_blank() {
return Err(CmdError::Args("Tag name and value must not be blank".to_string()));
let user = User { id: user_id };
let mut conn = connection(&user).await?;
let mut tries = Tries::default();
while tries
.step(settled(Owner::Commodity.set(&mut conn, commodity_id, &tag).await, not_found))?
.is_none()
{}
Ok(Some(CmdResult::String("ok".to_string())))
ClearCommodityTag {
let (tag_name, _) = normalize(&tag_name, "");
if tag_name.is_empty() {
return Err(CmdError::Args("Tag name must not be blank".to_string()));
if IDENTITY_TAGS.contains(&tag_name) {
return Err(CmdError::Args("The symbol and name tags cannot be cleared".to_string()));
.step(settled(Owner::Commodity.clear(&mut conn, commodity_id, tag_name).await, not_found))?