use std::collections::HashSet;
use cheetah_string::CheetahString;
use crate::common::attribute::Attribute;
use crate::common::attribute::AttributeBase;
#[derive(Debug, Clone)]
pub struct EnumAttribute {
attribute: AttributeBase,
universe: HashSet<CheetahString>,
default_value: CheetahString,
}
impl EnumAttribute {
pub fn new(
name: CheetahString,
changeable: bool,
universe: HashSet<CheetahString>,
default_value: CheetahString,
) -> Self {
Self {
attribute: AttributeBase::new(name, changeable),
universe,
default_value,
}
}
#[inline]
pub fn default_value(&self) -> &str {
&self.default_value
}
#[inline]
pub fn universe(&self) -> &HashSet<CheetahString> {
&self.universe
}
}
impl Attribute for EnumAttribute {
#[inline]
fn verify(&self, value: &str) -> Result<(), String> {
if !self.universe.contains(value) {
return Err(format!(
"Value '{}' is not in the valid set: {:?}",
value, self.universe
));
}
Ok(())
}
#[inline]
fn name(&self) -> &CheetahString {
self.attribute.name()
}
#[inline]
fn is_changeable(&self) -> bool {
self.attribute.is_changeable()
}
}