use std::marker::PhantomData;
use mln_style::color::Color;
pub(crate) trait StatisticValue {
fn statistic(&self) -> Option<f32>;
}
impl StatisticValue for f32 {
fn statistic(&self) -> Option<f32> {
Some(*self)
}
}
impl StatisticValue for Color {
fn statistic(&self) -> Option<f32> {
None
}
}
#[derive(Clone, Debug, mln_wire::Wire)]
pub(crate) struct PaintPropertyStatistics<T> {
max: Option<f32>,
#[wire(skip)]
_t: PhantomData<T>,
}
impl<T> Default for PaintPropertyStatistics<T> {
fn default() -> Self {
PaintPropertyStatistics { max: None, _t: PhantomData }
}
}
impl<T: StatisticValue> PaintPropertyStatistics<T> {
#[must_use]
pub(crate) fn max(&self) -> Option<f32> {
self.max
}
pub(crate) fn add(&mut self, value: &T) {
if let Some(value) = value.statistic() {
self.max = Some(match self.max {
Some(max) => {
if max < value {
value
} else {
max
}
}
None => value,
});
}
}
}