Lines
98.59 %
Functions
53.13 %
Branches
100 %
//! `get-transaction-logical`: a stored transaction as the from→to plist that
//! `create-transaction-logical` / `update-transaction-logical` accept.
use finance::tag::Tag;
use server::command::CmdError;
use server::logical::{RaisedRow, RowTags, pick_note};
use server::user::User;
use uuid::Uuid;
use super::parse::parse_transaction_id_arg;
use super::render::quote_string;
use crate::host_failure::cmd_failure;
use crate::stored::{Detail, TaggedRow, raise_rows};
const CTX: &str = "get-transaction-logical";
fn render_tag(tag: &Tag) -> String {
let description = tag
.description
.as_deref()
.map(|d| format!(" :description {}", quote_string(d)))
.unwrap_or_default();
format!(
"(:name {} :value {}{description})",
quote_string(&tag.tag_name),
quote_string(&tag.tag_value)
)
}
/// `((:name "n" :value "v" [:description "d"]) …)`; `()` when empty.
pub(in crate::natives) fn render_tags(tags: &[Tag]) -> String {
let items: Vec<String> = tags.iter().map(render_tag).collect();
format!("({})", items.join(" "))
fn render_row_tags(key: &str, tags: &[Tag]) -> String {
match tags {
[] => String::new(),
_ => format!(" :{key} {}", render_tags(tags)),
fn render_row(row: &RaisedRow, tags: &RowTags) -> String {
let to_amount = row
.to_amount
.map(|v| format!(" :to-amount {}", amount::wire_rational(v)))
let tags = [
("tags", &tags.shared),
("from-tags", &tags.from),
("to-tags", &tags.to),
]
.map(|(key, tags)| render_row_tags(key, tags))
.concat();
"(:from \"{}\" :to \"{}\" :from-commodity \"{}\" :to-commodity \"{}\" :value {}{to_amount}{tags})",
row.from_account,
row.to_account,
row.from_commodity,
row.to_commodity,
amount::wire_rational(row.value),
fn render_logical(detail: &Detail, rows: &[TaggedRow]) -> String {
let rendered: Vec<String> = rows
.iter()
.map(|(row, tags)| render_row(row, tags))
.collect();
let note = detail
.note
.map(|n| format!(" :note {}", quote_string(n)))
"(:transaction-id \"{}\" :date \"{}\"{note} :splits ({}))",
detail.transaction.id,
detail.transaction.post_date.to_rfc3339(),
rendered.join(" ")
pub(super) async fn run_get_transaction_logical(
user_id: Uuid,
id_arg: Option<String>,
) -> wasmtime::Result<String> {
let transaction_id = parse_transaction_id_arg(id_arg)?;
let snapshot = User { id: user_id }
.transaction_snapshot(transaction_id)
.await
.map_err(|err| cmd_failure(CTX, err.into()))?
.ok_or_else(|| cmd_failure(CTX, CmdError::NotFound("Transaction not found".to_string())))?;
let note = pick_note(snapshot.tags.iter()).map(|tag| tag.tag_value.clone());
let detail = Detail {
transaction: snapshot.transaction,
note,
prices: snapshot.prices,
};
let rows = raise_rows(&snapshot.splits, &snapshot.split_tags, &detail.prices)
.map_err(|err| cmd_failure(CTX, err))?;
Ok(render_logical(&detail, &rows))