chiark / gitweb /
more code motion
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 22 Aug 2020 21:55:45 +0000 (22:55 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 22 Aug 2020 21:55:45 +0000 (22:55 +0100)
src/imports.rs
src/pieces.rs
src/spec.rs

index d1f04b3875c332b962684b12fd18ecf8d4902dff..141c7d59fdf4ff7b650202117b370b504000b536 100644 (file)
@@ -71,10 +71,10 @@ pub use crate::error::*;
 pub use crate::commands::*;
 pub use crate::slotmap_slot_idx::*;
 pub use crate::cmdlistener::*;
-pub use crate::spec::*;
 pub use crate::mgmtchannel::*;
 pub use crate::api::{Lens,TransparentLens};
 pub use crate::utils::OrdExt;
+pub use crate::spec::*;
 
 pub use libc::uid_t;
 
index facaad9e9139427e787f9e7d513fc24beef7f5ce..45a34eb954ac6261012fa46373d785bd33048775 100644 (file)
@@ -1,3 +1,4 @@
+// pieces
 
 use crate::imports::*;
 
@@ -122,32 +123,6 @@ impl SimpleShape {
   }
 }
 
-#[derive(Serialize,Deserialize)]
-#[derive(Debug,Default)]
-#[repr(transparent)]
-struct ColourSpec(String);
-
-impl TryFrom<&ColourSpec> for Colour {
-  type Error = SE;
-  #[throws(SE)]
-  fn try_from(spec: &ColourSpec) -> Colour {
-    // xxx check syntax
-    spec.0.clone()
-  }
-}
-
-#[derive(Debug,Serialize,Deserialize)]
-struct Disc {
-  diam : Coord,
-  faces : IndexVec<FaceId,ColourSpec>,
-}
-
-#[derive(Debug,Serialize,Deserialize)]
-struct Square {
-  size : Vec<Coord>,
-  faces : IndexVec<FaceId,ColourSpec>,
-}
-
 #[throws(GameError)]
 fn simple_resolve_spec_face(faces: &IndexSlice<FaceId,[ColourSpec]>,
                             face: Option<FaceId>)
@@ -158,7 +133,7 @@ fn simple_resolve_spec_face(faces: &IndexSlice<FaceId,[ColourSpec]>,
 }
 
 #[typetag::serde]
-impl PieceSpec for Disc {
+impl PieceSpec for piece_specs::Disc {
   #[throws(SE)]
   fn load(&self) -> Box<dyn Piece> {
     let unit_path =
@@ -176,7 +151,7 @@ impl PieceSpec for Disc {
 }
 
 #[typetag::serde]
-impl PieceSpec for Square {
+impl PieceSpec for piece_specs::Square {
   #[throws(SE)]
   fn load(&self) -> Box<dyn Piece> {
     let (x, y) = match *self.size.as_slice() {
@@ -194,19 +169,3 @@ impl PieceSpec for Square {
     simple_resolve_spec_face(&self.faces, face)?
   }
 } 
-
-#[allow(clippy::type_complexity)]
-pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn Piece>)>,SE> {
-  Ok(vec![
-    ([ 90, 80 ],
-     Disc {
-       diam : 20,
-       faces : index_vec![ ColourSpec("red".to_string()), ColourSpec("grey".to_string()) ],
-     }.load()?),
-    ([ 90, 60 ],
-     Square {
-       size : vec![20],
-       faces : index_vec![ ColourSpec("blue".to_string()), ColourSpec("grey".to_string()) ],
-     }.load()?),
-  ])
-}
index dc59944ad33e26d37f398594631de8c5efd7e7a4..e06f7a256c68975f12861149036459a9d187ac07 100644 (file)
@@ -2,7 +2,7 @@
 
 use serde::{Serialize,Deserialize};
 use fehler::throws;
-use index_vec::define_index_type;
+use index_vec::{define_index_type,IndexVec,index_vec};
 use crate::gamestate::PieceSpec;
 use std::fmt::Debug;
 use implementation::PlayerAccessSpec;
@@ -22,6 +22,11 @@ define_index_type! {
   pub struct FaceId = u8;
 }
 
+#[derive(Serialize,Deserialize)]
+#[derive(Debug,Default)]
+#[repr(transparent)]
+pub struct ColourSpec(String);
+
 //---------- Table TOML file ----------
 
 #[derive(Debug,Serialize,Deserialize)]
@@ -57,15 +62,36 @@ pub struct PiecesSpec {
   pub count : Option<u32>,
   pub face : Option<FaceId>,
   #[serde(flatten)]
-  pub info : Box<dyn PieceSpec>, // see pieces.rs
+  pub info : Box<dyn PieceSpec>,
+}
+
+//---------- Piece specs ----------
+// the implementations are in pieces.rs
+
+pub mod piece_specs {
+  use super::*;
+
+  #[derive(Debug,Serialize,Deserialize)]
+  pub struct Disc {
+    pub diam : Coord,
+    pub faces : IndexVec<FaceId,ColourSpec>,
+  }
+
+  #[derive(Debug,Serialize,Deserialize)]
+  pub struct Square {
+    pub size : Vec<Coord>,
+    pub faces : IndexVec<FaceId,ColourSpec>,
+  }
+
 }
 
-//----------  Implementation ----------
+//---------- Implementation ----------
 
 mod implementation {
   use super::*;
   use crate::imports::*;
   type Insn = crate::commands::MgmtGameInstruction;
+  type SE = SVGProcessingError;
 
   #[typetag::serde(tag="access")]
   pub trait PlayerAccessSpec : Debug {
@@ -97,4 +123,30 @@ mod implementation {
       }
     }
   }
+
+  impl TryFrom<&ColourSpec> for Colour {
+    type Error = SE;
+    #[throws(SE)]
+    fn try_from(spec: &ColourSpec) -> Colour {
+      // xxx check syntax
+      spec.0.clone()
+    }
+  }
+}
+
+#[allow(clippy::type_complexity)]
+pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn crate::gamestate::Piece>)>,crate::pieces::SVGProcessingError> {
+  use crate::imports::*;
+  Ok(vec![
+    ([ 90, 80 ],
+     piece_specs::Disc {
+       diam : 20,
+       faces : index_vec![ ColourSpec("red".to_string()), ColourSpec("grey".to_string()) ],
+     }.load()?),
+    ([ 90, 60 ],
+     piece_specs::Square {
+       size : vec![20],
+       faces : index_vec![ ColourSpec("blue".to_string()), ColourSpec("grey".to_string()) ],
+     }.load()?),
+  ])
 }