use crate::error::StaticError;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Property {
pub name: String,
pub values: Vec<f64>,
}
impl Property {
#[must_use]
pub fn constant(name: impl Into<String>, value: f64, cell_count: usize) -> Self {
Self {
name: name.into(),
values: vec![value; cell_count],
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Properties {
cell_count: usize,
map: HashMap<String, Property>,
}
impl Properties {
#[must_use]
pub fn new(cell_count: usize) -> Self {
Self {
cell_count,
map: HashMap::new(),
}
}
pub fn set(&mut self, prop: Property) -> Result<(), StaticError> {
if prop.values.len() != self.cell_count {
return Err(StaticError::InvalidInput(format!(
"property '{}' has {} values, expected {}",
prop.name,
prop.values.len(),
self.cell_count
)));
}
self.map.insert(prop.name.clone(), prop);
Ok(())
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&Property> {
self.map.get(name)
}
#[must_use]
pub fn take_values(&mut self, name: &str) -> Vec<f64> {
self.map.remove(name).map(|p| p.values).unwrap_or_default()
}
pub(crate) fn set_cell_count(&mut self, cell_count: usize) {
self.cell_count = cell_count;
}
#[must_use]
pub fn len(&self) -> usize {
self.map.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn names(&self) -> impl Iterator<Item = &str> {
self.map.keys().map(String::as_str)
}
}