use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SecondOfDay(pub u32);
impl SecondOfDay {
pub const ZERO: SecondOfDay = SecondOfDay(0);
pub const MAX: SecondOfDay = SecondOfDay(u32::MAX);
pub const fn from_secs(s: u32) -> Self {
SecondOfDay(s)
}
pub const fn hms(h: u32, m: u32, s: u32) -> Self {
SecondOfDay(h * 3600 + m * 60 + s)
}
pub const fn as_secs(self) -> u32 {
self.0
}
pub const fn as_hms(self) -> (u32, u32, u32) {
(self.0 / 3600, (self.0 / 60) % 60, self.0 % 60)
}
pub fn every(
start: SecondOfDay,
end: SecondOfDay,
step: u32,
) -> impl Iterator<Item = SecondOfDay> + Clone {
(start.0..end.0)
.step_by(step.max(1) as usize)
.map(SecondOfDay)
}
}
impl From<u32> for SecondOfDay {
fn from(s: u32) -> Self {
SecondOfDay(s)
}
}
impl fmt::Display for SecondOfDay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::ops::Add<Duration> for SecondOfDay {
type Output = SecondOfDay;
fn add(self, d: Duration) -> SecondOfDay {
SecondOfDay(self.0.saturating_add(d.0))
}
}
impl std::ops::Sub<SecondOfDay> for SecondOfDay {
type Output = Duration;
fn sub(self, other: SecondOfDay) -> Duration {
Duration(self.0.saturating_sub(other.0))
}
}
impl std::ops::Sub<Duration> for SecondOfDay {
type Output = SecondOfDay;
fn sub(self, d: Duration) -> SecondOfDay {
SecondOfDay(self.0.saturating_sub(d.0))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Duration(pub u32);
impl Duration {
pub const ZERO: Duration = Duration(0);
pub const MAX: Duration = Duration(u32::MAX);
pub const fn from_secs(s: u32) -> Self {
Duration(s)
}
pub const fn as_secs(self) -> u32 {
self.0
}
}
impl From<u32> for Duration {
fn from(s: u32) -> Self {
Duration(s)
}
}
impl fmt::Display for Duration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::ops::Add<Duration> for Duration {
type Output = Duration;
fn add(self, o: Duration) -> Duration {
Duration(self.0.saturating_add(o.0))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Transfers(pub u8);
impl Transfers {
pub const ZERO: Transfers = Transfers(0);
pub const MAX: Transfers = Transfers(u8::MAX);
pub const fn new(n: u8) -> Self {
Transfers(n)
}
pub const fn get(self) -> u8 {
self.0
}
}
impl From<u8> for Transfers {
fn from(n: u8) -> Self {
Transfers(n)
}
}
impl fmt::Display for Transfers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}