pants_store/
reads.rs

1use std::{collections::HashMap, fmt::Display};
2
3#[derive(Debug)]
4pub struct Reads<T> {
5    pub data: HashMap<String, T>,
6}
7
8impl<T> Default for Reads<T> {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14impl<T> Reads<T> {
15    pub fn new() -> Self {
16        Self {
17            data: HashMap::default(),
18        }
19    }
20
21    pub fn insert(&mut self, key: String, value: T) -> Option<T> {
22        self.data.insert(key, value)
23    }
24
25    pub fn remove(&mut self, key: &str) -> Option<T> {
26        self.data.remove(key)
27    }
28}
29
30impl<T: Display + Clone> Display for Reads<T> {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        if self.data.len() == 1 {
33            write!(
34                f,
35                "{}",
36                self.data.clone().into_values().collect::<Vec<_>>()[0]
37            )
38        } else {
39            for (key, value) in self.data.clone().into_iter() {
40                write!(f, "{}: {}", key, value)?;
41            }
42            Ok(())
43        }
44    }
45}