use core::fmt::{self, Debug, Formatter};
const WHOLE: f32 = 1.0;
#[derive(Clone, Copy)]
pub struct Progress {
run: f32,
ends: bool,
}
impl Progress {
pub(crate) fn new(run: f32, ends: bool) -> Self {
Self {
run: match run.is_finite() {
true => run.max(0.0),
false => 0.0,
},
ends,
}
}
pub fn ended(&self) -> bool {
self.ends && self.run >= WHOLE
}
pub fn past(&self, fraction: f32) -> bool {
self.fraction() >= fraction
}
pub fn cycle(&self) -> u32 {
self.counted().floor().max(0.0) as u32
}
pub fn fraction(&self) -> f32 {
match self.ended() {
true => WHOLE,
false => self.counted().fract().clamp(0.0, WHOLE),
}
}
fn counted(&self) -> f32 {
match self.ends {
true => self.run.min(WHOLE),
false => self.run,
}
}
}
impl Debug for Progress {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Progress")
.field("cycle", &self.cycle())
.field("fraction", &self.fraction())
.field("ended", &self.ended())
.finish()
}
}