1use std::sync::{Arc, Mutex};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum LookupKind {
7 Account,
8 Commodity,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Lookup {
13 pub kind: LookupKind,
14 pub key: String,
15 pub row_key: bool,
17}
18
19#[derive(Debug, Clone, Default)]
21pub struct LookupLog(Arc<Mutex<Vec<Lookup>>>);
22
23impl LookupLog {
24 pub(crate) fn record(&self, kind: LookupKind, key: &str) {
25 if let Ok(mut lookups) = self.0.lock() {
26 lookups.push(Lookup {
27 kind,
28 key: key.to_string(),
29 row_key: false,
30 });
31 }
32 }
33
34 #[must_use]
36 pub fn lookups(&self) -> Vec<Lookup> {
37 self.0
38 .lock()
39 .map(|lookups| lookups.clone())
40 .unwrap_or_default()
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn clones_share_one_record_in_call_order() {
50 let log = LookupLog::default();
51 let clone = log.clone();
52 clone.record(LookupKind::Account, "Cafe");
53 log.record(LookupKind::Commodity, "USD");
54
55 let keys: Vec<_> = log.lookups().into_iter().map(|l| (l.kind, l.key)).collect();
56 assert_eq!(
57 keys,
58 [
59 (LookupKind::Account, "Cafe".to_string()),
60 (LookupKind::Commodity, "USD".to_string())
61 ]
62 );
63 }
64}