Skip to main content

server/logical/
note.rs

1//! The one rule that tells the transaction note from its other tags.
2
3use std::collections::HashMap;
4
5use finance::tag::{Tag, normalize};
6
7use crate::command::FinanceEntity;
8
9/// Whether `tag` is the note: tags are stored under trimmed names, so a padded
10/// `note ` is the note too.
11#[must_use]
12pub fn is_note(tag: &Tag) -> bool {
13    is_note_name(&tag.tag_name)
14}
15
16/// Whether a stored or typed tag name stands for the note.
17#[must_use]
18pub fn is_note_name(name: &str) -> bool {
19    normalize(name, "").0 == "note"
20}
21
22/// The note among `tags`: the greatest by byte order of (name, value) should a
23/// legacy padded name leave several, whatever the order `tags` come in.
24pub fn pick_note<'a>(tags: impl Iterator<Item = &'a Tag>) -> Option<&'a Tag> {
25    tags.filter(|tag| is_note(tag))
26        .max_by_key(|tag| (&tag.tag_name, &tag.tag_value))
27}
28
29/// The value of the note among a listing's tags, which are keyed by stored name.
30#[must_use]
31pub fn note_value(tags: &HashMap<String, FinanceEntity>) -> Option<&str> {
32    let stored = tags.values().filter_map(|entity| match entity {
33        FinanceEntity::Tag(tag) => Some(tag),
34        _ => None,
35    });
36    pick_note(stored).map(|tag| tag.tag_value.as_str())
37}
38
39#[cfg(test)]
40#[path = "note_tests.rs"]
41mod tests;