mod contains;
use std::collections::{BTreeMap, HashMap};
pub trait InteractsWithData {
type Value;
type Item;
fn data(&self) -> &Self::Value;
fn all(&self) -> Self::Value where Self::Value: Clone {
self.data().clone()
}
fn exists(&self, key: &Self::Item) -> bool where Self::Value: Contains<Self::Item> {
self.data().contains_item(key)
}
fn has(&self, key: &Self::Item) -> bool where Self::Value: Contains<Self::Item> {
self.exists(key)
}
fn when_has<F>(&mut self, key: &Self::Item, mut closure: F )
where
Self::Value: Contains<Self::Item>,
F: FnMut(&mut Self),
{
if self.has(key) {
closure(self);
}
}
fn filled(&self, key: &Self::Item) -> bool {
false
}
fn is_not_filled(&self, key: &Self::Item) -> bool {
true
}
fn any_filled(&self, keys: Vec<&Self::Item>) -> bool {
false
}
}
pub trait IntoData {
type Item;
type IntoIter: IntoIterator<Item = Self::Item>;
fn data(self) -> Self::IntoIter;
}
pub trait Contains<T> {
fn contains_item(&self, item: &T) -> bool;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vec_interacts_with_data() {
let vec = vec![1, 2, 3, 4];
assert_eq!(vec.all(), vec![1, 2, 3, 4]);
assert!(vec.exists(&3));
assert!(!vec.exists(&5));
}
#[test]
fn test_hashmap_interacts_with_data() {
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
let expected_map = HashMap::from([("a", 1), ("b", 2)]);
assert_eq!(map.all(), expected_map);
assert!(map.exists(&"b"));
assert!(!map.exists(&"c"));
}
#[test]
fn test_btreemap_interacts_with_data() {
let mut map = BTreeMap::new();
map.insert("a", 1);
map.insert("b", 2);
let expected_map = BTreeMap::from([("a", 1), ("b", 2)]);
assert_eq!(map.all(), expected_map);
assert!(map.exists(&"b"));
assert!(!map.exists(&"c"));
}
}