1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::collections::HashMap;
use std::fs;
use std::path::Path;

use crate::{hash40, Hash40};
use parking_lot::RwLock;

pub struct HashLabels {
    pub(crate) labels: HashMap<Hash40, String>,
}

impl HashLabels {
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, std::io::Error> {
        fn inner(path: &Path) -> Result<HashLabels, std::io::Error> {
            Ok(HashLabels::from_string(&fs::read_to_string(path)?))
        }

        inner(path.as_ref())
    }

    pub fn from_string(text: &str) -> Self {
        HashLabels {
            labels: text
                .lines()
                .map(|line| (hash40(line), line.to_owned()))
                .collect(),
        }
    }

    pub(crate) fn add_label<S: Into<String>>(&mut self, label: S) -> Hash40 {
        let label = label.into();
        let hash = hash40(&label);
        self.labels.insert(hash, label);

        hash
    }

    pub fn new() -> Self {
        Self {
            labels: Default::default(),
        }
    }
}

impl Hash40 {
    pub fn label(self, labels: &HashLabels) -> Option<&str> {
        labels.labels.get(&self).map(|x| &**x)
    }

    pub fn global_label(self) -> Option<String> {
        GLOBAL_LABELS.read().labels.get(&self).map(Clone::clone)
    }

    //pub fn global_label<'a>(self) -> MappedRwLockReadGuard<'a, Option<&'a str>> {
    //    RwLockReadGuard::map(
    //        GLOBAL_LABELS.read(),
    //        |labels| &labels.labels.get(&self).map(|x| &**x)
    //    )
    //}

    pub fn set_global_labels_file<P: AsRef<Path>>(label_file: P) -> Result<(), std::io::Error> {
        Self::set_global_labels(HashLabels::from_file(label_file)?);
        Ok(())
    }

    pub fn set_global_labels(labels: HashLabels) {
        *GLOBAL_LABELS.write() = labels;
    }
}

lazy_static::lazy_static! {
    pub static ref GLOBAL_LABELS: RwLock<HashLabels> = RwLock::new(HashLabels::new());
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "search")]
    #[test]
    fn search_labels() {
        let labels = HashLabels::from_file("hash_labels.txt").unwrap();
        let before = std::time::Instant::now();
        let matches = labels.get_ordered_matches("mar");
    }

    #[test]
    fn from_string_line_feed() {
        let text = "a\nbc\ndef\n";
        let labels = HashLabels::from_string(&text);

        let hash_a = hash40("a");
        let hash_bc = hash40("bc");
        let hash_def = hash40("def");

        assert_eq!("a", hash_a.label(&labels).unwrap());
        assert_eq!("bc", hash_bc.label(&labels).unwrap());
        assert_eq!("def", hash_def.label(&labels).unwrap());
    }

    #[test]
    fn from_string_carriage_return_line_feed() {
        // Ensure the hash label file still works when edited on Windows.
        let text = "a\r\nbc\r\ndef\r\n";
        let labels = HashLabels::from_string(&text);

        let hash_a = hash40("a");
        let hash_bc = hash40("bc");
        let hash_def = hash40("def");

        assert_eq!("a", hash_a.label(&labels).unwrap());
        assert_eq!("bc", hash_bc.label(&labels).unwrap());
        assert_eq!("def", hash_def.label(&labels).unwrap());
    }
}