Lines
89.66 %
Functions
100 %
Branches
use server::command::{
CmdResult, FinanceEntity,
account::{CreateAccount, ListAccounts},
commodity::{CreateCommodity, ListCommodities},
};
use sqlx::types::Uuid;
pub async fn named_account(user_id: Uuid, name: &str) -> Uuid {
let result = CreateAccount::new()
.name(name.to_string())
.user_id(user_id)
.run()
.await
.expect("create account");
match result {
Some(CmdResult::Entity(FinanceEntity::Account(account))) => account.id,
other => panic!("expected a created account, got {other:?}"),
}
pub async fn named_commodity(user_id: Uuid, symbol: &str) -> Uuid {
let result = CreateCommodity::new()
.symbol(symbol.to_string())
.name(symbol.to_string())
.expect("create commodity");
Some(CmdResult::String(id)) => Uuid::parse_str(&id).expect("commodity id"),
other => panic!("expected a created commodity id, got {other:?}"),
pub async fn commodity_with_name(user_id: Uuid, symbol: &str, name: &str) -> Uuid {
fn tagged_id(result: Option<CmdResult>, tag: &str, wanted: &str) -> Option<Uuid> {
let Some(CmdResult::TaggedEntities { entities, .. }) = result else {
return None;
entities.into_iter().find_map(|(entity, tags)| {
let named = matches!(tags.get(tag), Some(FinanceEntity::Tag(t)) if t.tag_value == wanted);
match (named, entity) {
(true, FinanceEntity::Account(a)) => Some(a.id),
(true, FinanceEntity::Commodity(c)) => Some(c.id),
_ => None,
})
/// The id of the user's account whose `name` tag is `name`.
pub async fn account_id(user_id: Uuid, name: &str) -> String {
let listed = ListAccounts::new().user_id(user_id).run().await;
match tagged_id(listed.expect("list accounts"), "name", name) {
Some(id) => id.to_string(),
None => panic!("no account named {name}"),
/// The id of the user's commodity whose `symbol` tag is `symbol`.
pub async fn commodity_id(user_id: Uuid, symbol: &str) -> String {
let listed = ListCommodities::new().user_id(user_id).run().await;
match tagged_id(listed.expect("list commodities"), "symbol", symbol) {
None => panic!("no commodity {symbol}"),