use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Display,
};
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
#[serde(transparent)]
pub struct Group(pub String);
impl From<&Group> for String {
fn from(val: &Group) -> Self {
val.0.clone()
}
}
impl Display for Group {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
pub(crate) type GroupIndices = BTreeSet<usize>;
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct Groups(
#[serde(deserialize_with = "crate::serde_helpers::deserialize_usize_index_btreemap")]
BTreeMap<usize, Group>,
);
impl Groups {
pub fn get(&self, index: usize) -> Option<&Group> {
self.0.get(&index)
}
pub(crate) fn remove_nonexistant_groups(&mut self, available: &[Group]) {
self.0.retain(|_, group| available.contains(group));
}
pub fn contains_index(&self, index: usize) -> bool {
self.0.contains_key(&index)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn insert(&mut self, index: usize, group: Group) {
self.0.insert(index, group);
}
pub fn remove(&mut self, index: usize) {
self.0.remove(&index);
}
pub fn clear(&mut self) {
self.0.clear();
}
pub fn iter(&self) -> impl Iterator<Item = (&usize, &Group)> {
self.0.iter()
}
}