From: Ian Jackson Date: Sat, 20 Mar 2021 21:31:30 +0000 (+0000) Subject: clock: Tidying, code motion X-Git-Tag: otter-0.5.0~613 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=1485fd622ed3becc5c2e11a6ad70f422bc8659ed;p=otter.git clock: Tidying, code motion Signed-off-by: Ian Jackson --- diff --git a/src/clock.rs b/src/clock.rs index 27a8690e..604c59db 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -9,80 +9,12 @@ use shapelib::Rectangle; use nix::sys::time::TimeValLike as TVL; -// ==================== users ==================== - -struct UserInfo { - idchar: char, -} +// ========== definitions ========== const N: usize = 2; type Time = i32; -#[derive(Copy,Clone,Serialize,Deserialize)] -#[derive(Eq,Ord,PartialEq,PartialOrd,Hash)] -#[serde(try_from="u8", into="u8")] -struct User(bool); - -impl Index for [T;2] { - type Output = T; - fn index(&self, index: User) -> &T { &self[index.0 as usize] } -} -impl IndexMut for [T;2] { - fn index_mut(&mut self, index: User) -> &mut T { &mut self[index.0 as usize] } -} - -const USERINFOS: [UserInfo; N] = [ - UserInfo { idchar: 'x' }, - UserInfo { idchar: 'y' }, -]; - -const USERS: [User; N] = [ User(false), User(true) ]; - -impl fmt::Display for User { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_char(USERINFOS[*self].idchar) - } -} -impl fmt::Debug for User { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "User({})", self) - } -} - -#[derive(Debug,Clone,Copy,Error,Serialize,Deserialize)] -struct BadClockUserError; -display_as_debug!{BadClockUserError} - -impl TryFrom for User { - type Error = BadClockUserError; - #[throws(BadClockUserError)] - fn try_from(u: u8) -> User { User(match u { - 0 => false, - 1 => true, - _ => throw!(BadClockUserError), - }) } -} - -impl TryFrom for User { - type Error = BadClockUserError; - #[throws(BadClockUserError)] - fn try_from(c: char) -> User { User(match c { - 'x' | 'X' => false, - 'y' | 'Y' => true, - _ => throw!(BadClockUserError), - }) } -} - -impl From for u8 { - fn from(user: User) -> u8 { user.0 as u8 } -} - -impl std::ops::Not for User { - type Output = User; - fn not(self) -> User { User(! self.0) } -} - // ==================== state ==================== #[derive(Debug,Clone,Serialize,Deserialize)] @@ -104,6 +36,29 @@ struct State { #[serde(skip)] running: Option, } + +#[derive(Debug,Copy,Clone,Serialize,Deserialize)] +struct UState { + player: PlayerId, + #[serde(with="timespec_serde")] remaining: TimeSpec, // -ve means flag +} + +#[derive(Debug,Copy,Clone,Eq,PartialEq,Serialize,Deserialize)] +struct Current { + user: User, +} + +#[derive(Debug,Clone,Copy)] +struct Running { + expires: TimeSpec, +} + +impl ChessClock { + fn initial_time(&self) -> TimeSpec { + TVL::seconds(self.time.into()) + } +} + impl State { fn new(spec: &ChessClock) -> Self { let mut state = State::dummy(); @@ -146,26 +101,74 @@ impl PieceXData for State { } -#[derive(Debug,Copy,Clone,Serialize,Deserialize)] -struct UState { - player: PlayerId, - #[serde(with="timespec_serde")] remaining: TimeSpec, // -ve means flag +// ==================== users ==================== + +struct UserInfo { + idchar: char, } -#[derive(Debug,Copy,Clone,Eq,PartialEq,Serialize,Deserialize)] -struct Current { - user: User, +#[derive(Copy,Clone,Serialize,Deserialize)] +#[derive(Eq,Ord,PartialEq,PartialOrd,Hash)] +#[serde(try_from="u8", into="u8")] +struct User(bool); + +impl Index for [T;2] { + type Output = T; + fn index(&self, index: User) -> &T { &self[index.0 as usize] } +} +impl IndexMut for [T;2] { + fn index_mut(&mut self, index: User) -> &mut T { &mut self[index.0 as usize] } +} + +const USERINFOS: [UserInfo; N] = [ + UserInfo { idchar: 'x' }, + UserInfo { idchar: 'y' }, +]; + +const USERS: [User; N] = [ User(false), User(true) ]; + +impl fmt::Display for User { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_char(USERINFOS[*self].idchar) + } +} +impl fmt::Debug for User { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "User({})", self) + } } -#[derive(Debug,Clone,Copy)] -struct Running { - expires: TimeSpec, +#[derive(Debug,Clone,Copy,Error,Serialize,Deserialize)] +struct BadClockUserError; +display_as_debug!{BadClockUserError} + +impl TryFrom for User { + type Error = BadClockUserError; + #[throws(BadClockUserError)] + fn try_from(u: u8) -> User { User(match u { + 0 => false, + 1 => true, + _ => throw!(BadClockUserError), + }) } } -impl ChessClock { - fn initial_time(&self) -> TimeSpec { - TVL::seconds(self.time.into()) - } +impl TryFrom for User { + type Error = BadClockUserError; + #[throws(BadClockUserError)] + fn try_from(c: char) -> User { User(match c { + 'x' | 'X' => false, + 'y' | 'Y' => true, + _ => throw!(BadClockUserError), + }) } +} + +impl From for u8 { + fn from(user: User) -> u8 { user.0 as u8 } +} + +impl std::ops::Not for User { + type Output = User; + fn not(self) -> User { User(! self.0) } } // ==================== rendering, abstract ====================