1
//! What a transaction the browser submitted stored.
2

            
3
use anyhow::Result;
4
use uuid::Uuid;
5

            
6
use super::harness::Harness;
7
use crate::common::tenant::tenant_connection;
8

            
9
21
pub async fn snapshot(h: &Harness, transaction: Uuid) -> Result<Vec<String>> {
10
21
    let mut conn = tenant_connection(h.tenant.id).await;
11
21
    Ok(server::lossless::snapshot(&mut *conn, transaction).await?)
12
21
}
13

            
14
/// `account commodity value` of every split, sorted.
15
14
pub fn legs(snapshot: &[String]) -> Vec<String> {
16
14
    let mut legs: Vec<String> = snapshot
17
14
        .iter()
18
85
        .filter_map(|fact| fact.strip_prefix("split "))
19
58
        .map(|fact| fact.split(' ').take(3).collect::<Vec<_>>().join(" "))
20
        .collect();
21
14
    legs.sort();
22
14
    legs
23
14
}
24

            
25
/// `account value name value` of every split tag, sorted.
26
11
pub fn leg_tags(snapshot: &[String]) -> Vec<String> {
27
11
    let mut tags: Vec<String> = snapshot
28
11
        .iter()
29
71
        .filter_map(|fact| fact.strip_prefix("split-tag "))
30
11
        .map(|fact| {
31
11
            let words: Vec<&str> = fact.split(' ').collect();
32
11
            let tail = words.len().saturating_sub(3);
33
11
            format!(
34
                "{} {} {} {}",
35
11
                words.first().unwrap_or(&""),
36
11
                words.get(2).unwrap_or(&""),
37
11
                words.get(tail).unwrap_or(&""),
38
11
                words.get(tail + 1).unwrap_or(&"")
39
            )
40
11
        })
41
        .collect();
42
11
    tags.sort();
43
11
    tags
44
11
}
45

            
46
/// One expected leg: account, commodity and the value as stored.
47
70
pub fn leg(account: Uuid, commodity: Uuid, value: &str) -> String {
48
70
    format!("{account} {commodity} {value}")
49
70
}