This commit is contained in:
Quaternions 2024-07-31 14:35:30 -07:00
parent 912ec33dbc
commit 3fd6a16124

View File

@ -1,4 +1,4 @@
use crate::timer::{Timer,Scaled,Error as TimerError}; use crate::timer::{TimerFixed,Realtime,Paused,Unpaused};
use crate::integer::Time; use crate::integer::Time;
pub enum InvalidationReason{ pub enum InvalidationReason{
@ -19,9 +19,9 @@ impl std::fmt::Display for Error{
impl std::error::Error for Error{} impl std::error::Error for Error{}
enum RunState{ enum RunState{
Created{timer:Timer<Paused>}, Created,
Started{timer:Timer<Unpaused>}, Started{timer:TimerFixed<Realtime,Unpaused>},
Finished{timer:Timer<Paused>}, Finished{timer:TimerFixed<Realtime,Paused>},
} }
pub struct Run{ pub struct Run{
@ -33,38 +33,40 @@ pub struct Run{
impl Run{ impl Run{
pub fn new(created:Time)->Self{ pub fn new(created:Time)->Self{
Self{ Self{
timer:Timer::scaled_paused(
created,
Time::ZERO,
crate::integer::Ratio64::ONE,
),
invalidated:None, invalidated:None,
created, created,
started:None, state:RunState::Created,
finished:None, }
}
pub fn time(&self,time:Time)->Time{
match &self.state{
RunState::Created=>Time::ZERO,
RunState::Started{timer}=>timer.time(time),
RunState::Finished{timer}=>timer.time(time),
} }
} }
pub fn start(&mut self,time:Time)->Result<(),Error>{ pub fn start(&mut self,time:Time)->Result<(),Error>{
match self.started{ match &self.state{
Some(_)=>Err(Error::AlreadyStarted), RunState::Created=>{
None=>{ self.state=RunState::Started{
self.started=Some(time); timer:TimerFixed::<Realtime,Unpaused>::new(time,Time::ZERO),
self.timer.unpause(time).map_err(Error::Timer)?; };
Ok(()) Ok(())
} },
RunState::Started{..}=>Err(Error::AlreadyStarted),
RunState::Finished{..}=>Err(Error::AlreadyFinished),
} }
} }
pub fn finish(&mut self,time:Time)->Result<(),Error>{ pub fn finish(&mut self,time:Time)->Result<(),Error>{
if self.started.is_none(){ match &self.state{
return Err(Error::NotStarted); RunState::Created=>Err(Error::NotStarted),
} RunState::Started{timer}=>{
match self.finished{ self.state=RunState::Finished{
Some(_)=>Err(Error::AlreadyFinished), timer:TimerFixed::<Realtime,Paused>::new(time,timer.time(time)),
None=>{ };
self.finished=Some(time);
self.timer.pause(time).map_err(Error::Timer)?;
Ok(()) Ok(())
} },
RunState::Finished{..}=>Err(Error::AlreadyFinished),
} }
} }
} }