pub(crate) struct OrderedMap<Value> {
entries: Vec<(String, Value)>,
}
impl<Value> Default for OrderedMap<Value> {
fn default() -> OrderedMap<Value> {
OrderedMap {
entries: Vec::new(),
}
} }
impl<Value> OrderedMap<Value> {
pub(crate) fn set(&mut self, key: &str, value: Value) {
match self
.entries
.iter_mut()
.find(|(existing, _)| existing == key)
{
Some(entry) => entry.1 = value,
None => self.entries.push((key.to_owned(), value)),
}
}
pub(crate) fn get(&self, key: &str) -> Option<&Value> {
self.entries
.iter()
.find(|(existing, _)| existing == key)
.map(|(_, value)| value)
}
pub(crate) fn remove(&mut self, key: &str) {
self.entries.retain(|(existing, _)| existing != key);
}
pub(crate) fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
self.entries
.iter()
.map(|(key, value)| (key.as_str(), value))
}
pub(crate) fn keys(&self) -> Vec<String> {
self.entries.iter().map(|(key, _)| key.clone()).collect()
}
pub(crate) fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub(crate) fn clear(&mut self) {
self.entries.clear();
} }