| 1 | use core::{ |
| 2 | borrow::Borrow, |
| 3 | fmt, |
| 4 | hash::{BuildHasher, Hash}, |
| 5 | }; |
| 6 | use std::collections::{BTreeMap, HashMap}; |
| 7 | |
| 8 | use crate::{FormatKey, FormatKeyError}; |
| 9 | |
| 10 | impl<K, V, S> FormatKey for HashMap<K, V, S> |
| 11 | where |
| 12 | K: Borrow<str> + Eq + Hash, |
| 13 | V: fmt::Display, |
| 14 | S: BuildHasher, |
| 15 | { |
| 16 | fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> { |
| 17 | match self.get(key) { |
| 18 | Some(v) => v.fmt(f).map_err(FormatKeyError::Fmt), |
| 19 | None => Err(FormatKeyError::UnknownKey), |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<K, V> FormatKey for BTreeMap<K, V> |
| 25 | where |
| 26 | K: Borrow<str> + Ord, |
| 27 | V: fmt::Display, |
| 28 | { |
| 29 | fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> { |
| 30 | match self.get(key) { |
| 31 | Some(v) => v.fmt(f).map_err(FormatKeyError::Fmt), |
| 32 | None => Err(FormatKeyError::UnknownKey), |
| 33 | } |
| 34 | } |
| 35 | } |