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
use crate::alias_format::AliasFormat;
use std::collections::HashMap;
pub struct AliasTranslator {
format: AliasFormat,
table_index: u16,
translations: HashMap<String, String>,
}
impl AliasTranslator {
pub fn new(format: AliasFormat) -> Self {
AliasTranslator {
format,
table_index: 0,
translations: HashMap::new(),
}
}
pub fn translate(&mut self, canonical_alias: &str) -> String {
use std::collections::hash_map::Entry;
let a = match self.translations.entry(canonical_alias.to_owned()) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => {
let alias = match self.format {
AliasFormat::TinyIndex => {
self.table_index += 1;
AliasFormat::tiny_index(self.table_index)
}
AliasFormat::ShortIndex => {
self.table_index += 1;
AliasFormat::short_index(&canonical_alias, self.table_index)
}
AliasFormat::MediumIndex => {
self.table_index += 1;
AliasFormat::medium_index(&canonical_alias, self.table_index)
}
_ => canonical_alias.to_owned(),
};
v.insert(alias)
}
}
.to_owned();
a
}
}