gled-model 2.30.0

The data model of gled: projects, scenes, effects, animations, curves and assets
Documentation
//! Groups of lamps, named in the project's SVG. Effects draw on one group,
//! picked by index into the project's (or a scene instance's) groups.

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)
    }

    /// Drops groups that are not among `available`, e.g. after a new SVG.
    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()
    }
}