From 24876a4fbcb2a55f7086e14497e104e9463cd51c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 22 Aug 2020 22:55:45 +0100 Subject: [PATCH] more code motion --- src/imports.rs | 2 +- src/pieces.rs | 47 +++------------------------------------- src/spec.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 48 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index d1f04b38..141c7d59 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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; diff --git a/src/pieces.rs b/src/pieces.rs index facaad9e..45a34eb9 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -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, -} - -#[derive(Debug,Serialize,Deserialize)] -struct Square { - size : Vec, - faces : IndexVec, -} - #[throws(GameError)] fn simple_resolve_spec_face(faces: &IndexSlice, face: Option) @@ -158,7 +133,7 @@ fn simple_resolve_spec_face(faces: &IndexSlice, } #[typetag::serde] -impl PieceSpec for Disc { +impl PieceSpec for piece_specs::Disc { #[throws(SE)] fn load(&self) -> Box { 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 { 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)>,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()?), - ]) -} diff --git a/src/spec.rs b/src/spec.rs index dc59944a..e06f7a25 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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, pub face : Option, #[serde(flatten)] - pub info : Box, // see pieces.rs + pub info : Box, +} + +//---------- 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, + } + + #[derive(Debug,Serialize,Deserialize)] + pub struct Square { + pub size : Vec, + pub faces : IndexVec, + } + } -//---------- 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)>,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()?), + ]) } -- 2.30.2