1
//! The one rule that tells the transaction note from its other tags.
2

            
3
use std::collections::HashMap;
4

            
5
use finance::tag::{Tag, normalize};
6

            
7
use 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]
12
647056
pub fn is_note(tag: &Tag) -> bool {
13
647056
    is_note_name(&tag.tag_name)
14
647056
}
15

            
16
/// Whether a stored or typed tag name stands for the note.
17
#[must_use]
18
679999
pub fn is_note_name(name: &str) -> bool {
19
679999
    normalize(name, "").0 == "note"
20
679999
}
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.
24
116587
pub fn pick_note<'a>(tags: impl Iterator<Item = &'a Tag>) -> Option<&'a Tag> {
25
187433
    tags.filter(|tag| is_note(tag))
26
97474
        .max_by_key(|tag| (&tag.tag_name, &tag.tag_value))
27
116587
}
28

            
29
/// The value of the note among a listing's tags, which are keyed by stored name.
30
#[must_use]
31
24484
pub fn note_value(tags: &HashMap<String, FinanceEntity>) -> Option<&str> {
32
25385
    let stored = tags.values().filter_map(|entity| match entity {
33
25385
        FinanceEntity::Tag(tag) => Some(tag),
34
        _ => None,
35
25385
    });
36
24484
    pick_note(stored).map(|tag| tag.tag_value.as_str())
37
24484
}
38

            
39
#[cfg(test)]
40
#[path = "note_tests.rs"]
41
mod tests;