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<T> Index<User> for [T;2] {
- type Output = T;
- fn index(&self, index: User) -> &T { &self[index.0 as usize] }
-}
-impl<T> IndexMut<User> 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<u8> for User {
- type Error = BadClockUserError;
- #[throws(BadClockUserError)]
- fn try_from(u: u8) -> User { User(match u {
- 0 => false,
- 1 => true,
- _ => throw!(BadClockUserError),
- }) }
-}
-
-impl TryFrom<char> 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<User> 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)]
#[serde(skip)] running: Option<Running>,
}
+
+#[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();
}
-#[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<T> Index<User> for [T;2] {
+ type Output = T;
+ fn index(&self, index: User) -> &T { &self[index.0 as usize] }
+}
+impl<T> IndexMut<User> 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<u8> 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<char> 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<User> 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 ====================