Lines
100 %
Functions
50 %
Branches
//! How a lookup key reaches an entity, by the rules of [`Directory`].
use uuid::Uuid;
use super::{Directory, Pick, pick};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Resolution {
Id,
Path,
Symbol,
/// A unique account name that is not the full path; holds the path.
Bare(String),
Ambiguous(usize),
Unknown,
}
fn resolution(found: &Pick, key: &str, otherwise: Resolution) -> Option<Resolution> {
match found {
Pick::One(id) if Uuid::parse_str(key) == Ok(*id) => Some(Resolution::Id),
Pick::One(_) => Some(otherwise),
Pick::Many(ids) => Some(Resolution::Ambiguous(ids.len())),
Pick::Missing => None,
impl Directory {
pub(crate) fn classify_account(&self, key: &str) -> Resolution {
let same = |name: &str, key: &str| name == key;
let by_path = pick(&self.account_paths, key, same);
if let Some(found) = resolution(&by_path, key, Resolution::Path) {
return found;
match pick(&self.accounts, key, same) {
Pick::One(id) => Resolution::Bare(self.account_path(id)),
Pick::Many(ids) => Resolution::Ambiguous(ids.len()),
Pick::Missing => Resolution::Unknown,
pub(crate) fn classify_commodity(&self, key: &str) -> Resolution {
let found = pick(&self.commodities, key, str::eq_ignore_ascii_case);
resolution(&found, key, Resolution::Symbol).unwrap_or(Resolution::Unknown)
#[cfg(test)]
mod tests;