Lines
96.3 %
Functions
62.5 %
Branches
100 %
//! Record of the entity lookups a script performs, for the reference preflight.
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LookupKind {
Account,
Commodity,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lookup {
pub kind: LookupKind,
pub key: String,
/// A logical-row key, which takes an id only.
pub row_key: bool,
/// Shared by every clone of the context that carries it.
#[derive(Debug, Clone, Default)]
pub struct LookupLog(Arc<Mutex<Vec<Lookup>>>);
impl LookupLog {
pub(crate) fn record(&self, kind: LookupKind, key: &str) {
if let Ok(mut lookups) = self.0.lock() {
lookups.push(Lookup {
kind,
key: key.to_string(),
row_key: false,
});
/// The lookups recorded so far, in call order.
#[must_use]
pub fn lookups(&self) -> Vec<Lookup> {
self.0
.lock()
.map(|lookups| lookups.clone())
.unwrap_or_default()
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clones_share_one_record_in_call_order() {
let log = LookupLog::default();
let clone = log.clone();
clone.record(LookupKind::Account, "Cafe");
log.record(LookupKind::Commodity, "USD");
let keys: Vec<_> = log.lookups().into_iter().map(|l| (l.kind, l.key)).collect();
assert_eq!(
keys,
[
(LookupKind::Account, "Cafe".to_string()),
(LookupKind::Commodity, "USD".to_string())
]
);