chiark / gitweb /
clock: Tidying, code motion
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:31:30 +0000 (21:31 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:55:05 +0000 (21:55 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/clock.rs

index 27a8690eac65281a474957257d30d70cd91e90ba..604c59db2b4aac0c5572a37954b1c92ecbe7247b 100644 (file)
@@ -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<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)]
@@ -104,6 +36,29 @@ struct State {
   #[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();
@@ -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<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 ====================