Lines
94.12 %
Functions
100 %
Branches
//! The one rule that tells the transaction note from its other tags.
use std::collections::HashMap;
use finance::tag::{Tag, normalize};
use crate::command::FinanceEntity;
/// Whether `tag` is the note: tags are stored under trimmed names, so a padded
/// `note ` is the note too.
#[must_use]
pub fn is_note(tag: &Tag) -> bool {
is_note_name(&tag.tag_name)
}
/// Whether a stored or typed tag name stands for the note.
pub fn is_note_name(name: &str) -> bool {
normalize(name, "").0 == "note"
/// The note among `tags`: the greatest by byte order of (name, value) should a
/// legacy padded name leave several, whatever the order `tags` come in.
pub fn pick_note<'a>(tags: impl Iterator<Item = &'a Tag>) -> Option<&'a Tag> {
tags.filter(|tag| is_note(tag))
.max_by_key(|tag| (&tag.tag_name, &tag.tag_value))
/// The value of the note among a listing's tags, which are keyed by stored name.
pub fn note_value(tags: &HashMap<String, FinanceEntity>) -> Option<&str> {
let stored = tags.values().filter_map(|entity| match entity {
FinanceEntity::Tag(tag) => Some(tag),
_ => None,
});
pick_note(stored).map(|tag| tag.tag_value.as_str())
#[cfg(test)]
#[path = "note_tests.rs"]
mod tests;