use super::*;
impl Counter {
pub fn increment(&self) {
let current: i32 = self.get_value().get();
let mut next: i32 = current.saturating_add(self.step);
if let Some(max) = self.max {
next = next.min(max);
}
if let Some(min) = self.min {
next = next.max(min);
}
self.get_value().set(next);
}
pub fn decrement(&self) {
let current: i32 = self.get_value().get();
let mut next: i32 = current.saturating_sub(self.step);
if let Some(min) = self.min {
next = next.max(min);
}
if let Some(max) = self.max {
next = next.min(max);
}
self.get_value().set(next);
}
pub fn set(&self, next: i32) {
let clamped: i32 = match (self.min, self.max) {
(Some(min), Some(max)) => next.clamp(min, max),
(Some(min), None) => next.max(min),
(None, Some(max)) => next.min(max),
(None, None) => next,
};
self.get_value().set(clamped);
}
pub fn set_unchecked(&self, next: i32) {
self.get_value().set(next);
}
pub fn is_at_max(&self) -> bool {
match self.max {
Some(max) => self.get_value().get() >= max,
None => false,
}
}
pub fn is_at_min(&self) -> bool {
match self.min {
Some(min) => self.get_value().get() <= min,
None => false,
}
}
pub fn get(&self) -> i32 {
self.get_value().get()
}
}
impl Display for Counter {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
write!(formatter, "Counter({})", self.get_value().get())
}
}