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