From d97483fbfd27005d04aaa0bfabb6c5957d3578d5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 16 Apr 2022 14:17:22 +0100 Subject: [PATCH] dice: Break out OccultSpec (breaking change) Earlier I wrote: We want to add an ilk here. But this is no longer going to be true, because we are getting rid of ilk mixing for dice. Nevertheless, this change is going in a roughly good direction in spec API terms since it will allow us to add other things to occult. Notably, if we allow composition of a die image from a base and faces, or if we allow specifying an occulted face image separately from the face images. And `occult_label` is just a bad pattern. Signed-off-by: Ian Jackson --- src/dice.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/dice.rs b/src/dice.rs index 429bdc85..33dcaaa9 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -31,7 +31,7 @@ pub struct Spec { // must be >1 faces on image, or >1 texts, and if both, same number image: Box, #[serde(default)] labels: SpecLabels, - #[serde(default)] occult_label: String, + occult: Option, // 1.0 means base outline circle size on most distant corner of bounding box // minimum is 0.5; maximum is 1.5 #[serde(default="default_circle_scale")] circle_scale: f64, @@ -40,6 +40,11 @@ pub struct Spec { itemname: Option, } +#[derive(Debug,Default,Clone,Serialize,Deserialize)] +pub struct OccultSpec { + #[serde(default)] label: String, +} + #[derive(Debug,Clone,Serialize,Deserialize)] #[serde(untagged)] pub enum SpecLabels { @@ -166,17 +171,17 @@ impl PieceSpec for Spec { }; let _state: &mut State = gpc.xdata_mut(|| initial_state)?; - let occultable = match (img_occultable, &self.occult_label) { - (None, l) => if l == "" { - None - } else { + let occultable = match (img_occultable, &self.occult) { + (None, None) => None, + (None, Some(_occ)) => { throw!(SpecError::UnusedOccultLabel) }, - (Some((image_occ_ilk, image_occ_image)), occ_label) => { - let occ_label = if occ_label == "" && labels.iter().any(|l| l != "") { - "?" + (Some((image_occ_ilk, image_occ_image)), occ) => { + let occ = occ.clone().unwrap_or_default(); + let occ_label = if occ.label == "" && labels.iter().any(|l| l != "") { + "?".into() } else { - occ_label + occ.label }; let our_ilk = @@ -201,7 +206,7 @@ impl PieceSpec for Spec { nfaces, cooldown_time, cooldown_radius, surround_outline, itemname: itemname.clone(), image: image_occ_image, - labels: index_vec![occ_label.into()], + labels: index_vec![occ_label], }) as _; Some((our_ilk, our_occ_image)) -- 2.30.2