use std::fmt;
use crate::{ReactionCondition, ReactionValidationError};
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ReactionConditionSet {
conditions: Vec<ReactionCondition>,
}
impl ReactionConditionSet {
#[must_use]
pub const fn new() -> Self {
Self {
conditions: Vec::new(),
}
}
#[must_use]
pub fn with_condition<T>(mut self, condition: T) -> Self
where
T: Into<ReactionCondition>,
{
self.push(condition);
self
}
pub fn push<T>(&mut self, condition: T)
where
T: Into<ReactionCondition>,
{
self.conditions.push(condition.into());
}
#[must_use]
pub fn as_slice(&self) -> &[ReactionCondition] {
&self.conditions
}
pub fn iter(&self) -> impl Iterator<Item = &ReactionCondition> {
self.conditions.iter()
}
#[must_use]
pub fn len(&self) -> usize {
self.conditions.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.conditions.is_empty()
}
pub fn validate(&self) -> Result<(), ReactionValidationError> {
for condition in &self.conditions {
condition.validate()?;
}
Ok(())
}
}
impl fmt::Display for ReactionConditionSet {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, condition) in self.conditions.iter().enumerate() {
if index > 0 {
formatter.write_str(", ")?;
}
write!(formatter, "{condition}")?;
}
Ok(())
}
}