From 591237fe203772d2b056cb263b491ba730478baf Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 20 Sep 2020 12:45:30 +0100 Subject: [PATCH] shapelib: wip, still does not compile Signed-off-by: Ian Jackson --- library/wikimedia.toml | 2 +- src/gamestate.rs | 16 ++--- src/global.rs | 2 +- src/imports.rs | 4 +- src/pieces.rs | 17 +++-- src/shapelib.rs | 144 +++++++++++++++++++++++------------------ 6 files changed, 104 insertions(+), 81 deletions(-) diff --git a/library/wikimedia.toml b/library/wikimedia.toml index 82bd59d8..8697d55f 100644 --- a/library/wikimedia.toml +++ b/library/wikimedia.toml @@ -15,7 +15,7 @@ data_url_prefix = "https://upload.wikimedia.org/wikipedia/commons/" data_url_hashprefix = true [group.chess] -outline.Circle = { } +outline = "Circle" size = [45] scale = 0.20 centre = [22.5, 22.5] diff --git a/src/gamestate.rs b/src/gamestate.rs index aedb3814..0d231c54 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -81,22 +81,22 @@ type IR = Result<(),IE>; type SE = SVGProcessingError; #[typetag::serde] -pub trait Piece : Send + Debug { +pub trait Outline : Send + Debug { + fn surround_path(&self, pri : &PieceRenderInstructions) -> Result; + fn thresh_dragraise(&self, pri : &PieceRenderInstructions) + -> Result, IE>; +} + +#[typetag::serde] +pub trait Piece : Outline + Send + Debug { fn resolve_spec_face(&self, face : Option) -> Result; // #[throws] doesn't work here for some reason fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) -> IR; - #[throws(IE)] - fn surround_path(&self, pri : &PieceRenderInstructions) -> Html; - fn svg_x_defs(&self, f: &mut Html, pri : &PieceRenderInstructions) -> IR; - #[throws(IE)] - fn thresh_dragraise(&self, pri : &PieceRenderInstructions) - -> Option; - fn describe_html(&self, face : Option) -> Html; fn delete_hook(&self, _p: &PieceState, _gs: &mut GameState) diff --git a/src/global.rs b/src/global.rs index 5cec5a4c..7bad84cf 100644 --- a/src/global.rs +++ b/src/global.rs @@ -167,7 +167,7 @@ pub struct Global { config : RwLock>, dirty : Mutex>, save_area_lock : Mutex>, - pub shapelibs : RwLock>, + pub shapelibs : RwLock>, } #[derive(Debug)] diff --git a/src/imports.rs b/src/imports.rs index bdbdb1fe..2c4cb0cd 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -86,6 +86,8 @@ pub use flexi_logger::{LogSpecification}; pub use hashlink::LinkedHashMap; +pub use delegate::delegate; + pub use crate::global::*; pub use crate::gamestate::*; pub use crate::pieces::*; @@ -101,7 +103,7 @@ pub use crate::api::{Lens,TransparentLens}; pub use crate::utils::*; pub use crate::spec::*; pub use crate::debugreader::DebugReader; -pub use crate::shapelib::{self,LibraryContents}; +pub use crate::shapelib; pub use nix::unistd::Uid; diff --git a/src/pieces.rs b/src/pieces.rs index 6eee0e00..a074bc3b 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -89,13 +89,7 @@ pub fn svg_rescale_path(input: &Html, scale: f64) -> Html { } #[typetag::serde] -impl Piece for SimpleShape { - #[throws(IE)] - fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) { - write!(&mut f.0, r##""##, - self.colours[pri.face].0, - &self.path.0)?; - } +impl Outline for SimpleShape { #[throws(IE)] fn surround_path(&self, _pri : &PieceRenderInstructions) -> Html { self.scaled_path.clone() @@ -105,6 +99,15 @@ impl Piece for SimpleShape { -> Option { Some(self.approx_dia / 2) } +} +#[typetag::serde] +impl Piece for SimpleShape { + #[throws(IE)] + fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) { + write!(&mut f.0, r##""##, + self.colours[pri.face].0, + &self.path.0)?; + } #[throws(IE)] fn svg_x_defs(&self, _f: &mut Html, _pri : &PieceRenderInstructions) { } diff --git a/src/shapelib.rs b/src/shapelib.rs index 11d1006a..2838e43b 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -5,16 +5,16 @@ pub use crate::imports::*; // Naming convention: -// *Defn read from library toml file -// *Info, *List from toml etc. but processed +// *Data, *List from toml etc. (processed if need be) +// *Defn raw read from library toml file (where different from Info) // *Details some shared structure // Item } once loaded and part of a game, // Outline } no Arc's as we serialise/deserialize during save/load #[derive(Debug)] -pub struct ContentsDefn { +pub struct Contents { dirname: String, - items: HashMap, + items: HashMap, } #[derive(Debug,Clone)] @@ -24,52 +24,53 @@ pub struct ItemDetails { } #[derive(Debug,Clone)] -pub struct LibraryItemDefn { // xxx ??? - details: Arc, - info: Arc, +pub struct ItemData { + details: Arc, + group: Arc, } -#[derive(Debug,Deserialize,Serialize)] -pub struct GroupInfo { - outline: Box, +#[derive(Debug,Deserialize)] +pub struct GroupDetails { size: Vec, - #[serde(default="num_traits::identities::One::one")] - scale: f64, - #[serde(default)] - centre: Option>, category: String, + #[serde(default)] centre: Option>, + #[serde(default)] flip: bool, + #[serde(default="num_traits::identities::One::one")] scale: f64, + #[serde(flatten)] outline: Box, +} + +#[derive(Debug)] +pub struct GroupData { + groupname: String, + d: GroupDetails, } #[derive(Debug,Deserialize)] struct GroupDefn { + files: FileList, #[serde(default)] item_prefix: String, #[serde(default)] item_suffix: String, - #[serde(default)] stem_prefix: String, - #[serde(default)] stem_suffix: String, - #[serde(default)] flip: bool, - files: FileList, - #[serde(flatten)] info: Arc, + #[serde(flatten)] info: Arc, } #[derive(Deserialize,Debug)] #[serde(try_from="String")] -#[derive(Serialize)] // xxx -struct FileList (Vec); +struct FileList (Vec); #[derive(Deserialize,Debug)] -#[derive(Serialize)] // xxx -struct FileInfo { +struct FileData { item_spec: String, - r_file_spec: String, + r_file_spec: (), // string, in the actual source file desc: Html, } -trait Outline { } +type IE = InternalError; -//#[typetag::deserialize] -#[typetag::serde] // xxx +#[typetag::deserialize(tag="outline")] trait OutlineDefn : Debug + Sync + Send { - fn check(&self, lgi: &LibraryGroupInfo) -> Result<(),LibraryLoadError>; + type Checked; + fn check(&self, lgi: &GroupData) -> Result; + fn load(&self, checked: &Self::Checked) -> Result,IE>; } #[derive(Error,Debug)] @@ -89,7 +90,7 @@ pub enum LibraryLoadError{ #[error("{:?}",&self)] InheritDepthLimitExceeded(String), #[error("{:?}",&self)] - DuplicateItem(String,LibraryItemInfo,LibraryItemInfo), + DuplicateItem { item: String, group1: String, group2: String }, #[error("{:?}",&self)] FilesListLineMissingWhitespace(usize), } @@ -101,16 +102,36 @@ type TV = toml::Value; type SE = SpecError; #[derive(Debug,Serialize,Deserialize)] -pub struct LibPieceSpec { // xxx rename, see above +pub struct ItemSpec { lib: String, item: String, } +define_index_type!{ pub struct DescId = u8; } + #[derive(Debug,Serialize,Deserialize)] -struct Item { // xxx totally redo the contents +struct ItemFace { svg: Html, - details: Arc, - info: Arc, + desc: DescId, +} + +#[derive(Debug,Serialize,Deserialize)] +struct Item { + faces: IndexVec, + descs: IndexVec, + outline: Box, +} + +#[typetag::serde(name="Lib")] +impl Outline for Item { delegate! { to self.outline { + fn surround_path(&self, pri : &PieceRenderInstructions) -> Result; + fn thresh_dragraise(&self, pri : &PieceRenderInstructions) + -> Result, IE>; +}}} + +#[typetag::serde(name="Lib")] +impl Piece for Item { + } /* @@ -123,41 +144,40 @@ impl Item for LibraryItem { #[typetag::serde(name="Lib")] impl PieceSpec for LibPieceSpec { */ -impl LibPieceSpec { +impl ItemSpec { fn load(&self) -> Result,SpecError> { let libs = GLOBAL.shapelibs.read().unwrap(); - let lib = libs.get(&self.lib) - .ok_or(SE::LibraryNotFound)?; - let lii = lib.items.get(&self.item) - .ok_or(SE::LibraryItemNotFound)?; + let lib = libs.get(&self.lib).ok_or(SE::LibraryNotFound)?; + let lii = lib.items.get(&self.item).ok_or(SE::LibraryItemNotFound)?; + let svg_path = format!("{}/{}", lib.dirname, &self.item); - let omg : &dyn Fn(&io::Error, _) -> SE = &|e,m:&str| { - error!("{}: {}: {}", &m, &svg_path, &e); - SE::InternalError(m.to_string()) - }; - let mut f = File::open(&svg_path) + let svg_data = fs::read_to_string(&svg_path) .map_err(|e| if e.kind() == ErrorKind::NotFound { SE::LibraryItemNotFound } else { - omg(&e, "unable to access library itme data file") - } - )?; - let mut svg_data = String::new(); - f.read_to_string(&mut svg_data).map_err( - |e| omg(&e, "unable to read library item data file") - )?; - let lp = LibraryItem { - svg: Html(svg_data), - details: lii.details.clone(), - info: lii.info.clone(), - }; - Box::new(lp); + let m = "error accessing/reading library item data file"; + error!("{}: {}: {}", &m, &svg_path, &e); + SE::InternalError(m.to_string()) + })?; + + let o_checked = lii.group.outline.check(&lii.group) + .map_err(|e| SE::InternalError(format!("rechecking outline: {}",&e)))?; + let outline = lii.group.outline.load(&lii.group)?; + + // xxx do something with flip + + let descs = index_vec![ ]; + let desc = descs.push_back(lii.info.desc.clone()); + let face = ItemFace { svg: Html(svg_data), desc }; + let faces = index_vec![ face ]; + let it = Item { faces, descs, outline }; + Box::new(it); panic!(); } } #[typetag::serde(name="Lib")] -impl PieceSpec for LibPieceSpec { +impl PieceSpec for ItemSpec { fn load(&self) -> Result,SpecError> { self.load() } @@ -241,12 +261,10 @@ pub fn load(libname: String, dirname: String) { info!("loaded library {:?} from {:?}", libname, dirname); } -#[derive(Deserialize,Debug)] -#[derive(Serialize)] // xxx -struct Circle { } -//#[typetag::deserialize] -#[typetag::serde] // xxx -impl OutlineSpec for Circle { +#[derive(Serialize,Deserialize,Debug)] +struct CircleDefn { } +#[typetag::serde(name="Circle")] +impl OutlineSpec for CircleDefn { #[throws(LibraryLoadError)] fn check(&self, lgi: &LibraryGroupInfo) { Self::get_size(lgi)?; -- 2.30.2