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
use std::collections::{hash_map::Iter, HashMap};

#[derive(Debug, Clone)]
pub struct CaseMap {
	map: HashMap<String, String>,
}

impl CaseMap {
	pub fn new() -> CaseMap {
		CaseMap {
			map: HashMap::new(),
		}
	}

	fn get_proper_key<'a>(&'a self, key: &'a str) -> Option<&str> {
		if self.map.contains_key(key) {
			return Some(key);
		}

		let case_key = format!("__cased__{}", key.to_lowercase());
		if self.map.contains_key(&case_key) {
			let real_key = match self.map.get(&case_key) {
				Some(real_key) => real_key,
				None => {
					return None;
				}
			};

			return Some(real_key);
		}

		None
	}

	pub fn insert(&mut self, key: &str, value: &str) {
		let case_key = format!("__cased__{}", key.to_lowercase());
		self.map.insert(key.to_owned(), value.to_owned());
		self.map.insert(case_key, key.to_owned());
	}

	pub fn get(&self, key: &str) -> Option<&String> {
		match self.get_proper_key(key) {
			Some(key) => self.map.get(key),
			None => None,
		}
	}

	pub fn contains_key(&self, key: &str) -> bool {
		match self.get_proper_key(key) {
			Some(_) => true,
			None => false,
		}
	}

	pub fn len(&self) -> usize {
		self.map.len() / 2
	}

	pub fn iter(&self) -> Iter<'_, String, String> {
		self.map.iter()
	}
}