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
116
117
118
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use crate::{hash40, Hash40};
use parking_lot::RwLock;
pub struct HashLabels {
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<'a>(&self, labels: &'a HashLabels) -> Option<&'a 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 set_global_labels_file<P: AsRef<Path>>(label_file: P) -> Result<(), std::io::Error> {
Ok(Self::set_global_labels(HashLabels::from_file(label_file)?))
}
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::*;
#[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() {
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());
}
}