use derive_more::{Display, From, Into};
use core::str::FromStr;
use crate::error::*;
use super::macros::*;
#[derive(
Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Display, From, Into,
)]
#[display("{}", self.as_str())]
#[repr(transparent)]
pub struct Minute(pub(crate) u8);
impl FromStr for Minute {
type Err = ParseMinuteError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_str(s: &str) -> Result<Self, Self::Err> {
minute_from_str!(s)
}
}
impl Minute {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new() -> Self {
Self::with(0)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn with(value: u8) -> Self {
if value > 59 {
panic!("Minute value must be between 0-59");
}
Self(value)
}
pub const fn try_with(value: u8) -> Option<Self> {
if value > 59 { None } else { Some(Self(value)) }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
minute_to_str!(self.0)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Display, Into)]
#[display("{}", self.as_str())]
#[repr(transparent)]
pub struct Second(pub(crate) u8);
impl FromStr for Second {
type Err = ParseSecondError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_str(s: &str) -> Result<Self, Self::Err> {
second_from_str!(s)
}
}
impl Second {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new() -> Self {
Self::with(0)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn with(value: u8) -> Self {
if value > 59 {
panic!("Second value must be between 0-59");
}
Self(value)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_with(value: u8) -> Option<Self> {
if value > 59 { None } else { Some(Self(value)) }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
second_to_str!(self.0)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Display, Into)]
#[display("{}", self.as_str())]
#[repr(transparent)]
pub struct Millisecond(pub(crate) u16);
impl FromStr for Millisecond {
type Err = ParseMillisecondError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn from_str(s: &str) -> Result<Self, Self::Err> {
millisecond_from_str!(s)
}
}
impl Millisecond {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new() -> Self {
Self::with(0)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn with(value: u16) -> Self {
if value > 999 {
panic!("Millisecond value must be between 0-999");
}
Self(value)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_with(value: u16) -> Option<Self> {
if value > 999 { None } else { Some(Self(value)) }
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn as_str(&self) -> &'static str {
millisecond_to_str!(self.0)
}
}
#[test]
#[should_panic]
fn minute_panic() {
let _ = Minute::with(60);
}
#[test]
#[should_panic]
fn second_panic() {
let _ = Second::with(60);
}
#[test]
#[should_panic]
fn millisecond_panic() {
let _ = Millisecond::with(1000);
}