chiark / gitweb /
svg size handling: Rename and move svg_parse_size
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 2 May 2022 10:31:43 +0000 (11:31 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 2 May 2022 10:32:49 +0000 (11:32 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/prelude.rs
src/shapelib.rs
src/spec.rs
src/utils.rs

index 661c03a00cbde43fc3a2edca66fdce3fd0f0d954..71f18b09ea9f96159cd613a12805af2bf2021f61 100644 (file)
@@ -244,3 +244,6 @@ pub type PUO<NS,ZL> = PieceUpdateOp<NS,ZL>;
 pub type PUOs = PieceUpdateOps;
 pub type WRC = WhatResponseToClientOp;
 #[allow(non_camel_case_types)] pub type PUE_P = PreparedUpdateEntry_Piece;
+
+// utils.rs
+pub use SVGSizeError as SvSE;
index 9d555418893f7072a951fde7723e6dae6ca4493c..e10185d1d823b1a0704bbe90cfe5c738960c66f6 100644 (file)
@@ -520,15 +520,6 @@ impl ItemSpec {
   }
 }
 
-#[derive(Error,Clone,Serialize,Deserialize,Debug)]
-pub enum SVGSizeError {
-  #[error("parse error: {0}")]           ParseError(String),
-  #[error("attribute {0} repeated")]     AttributeRepeated(String),
-  #[error("attribute {0} unparseable")]  AttributeUnparseable(String),
-  #[error("specifies only one of width and height")] OneOfWidthHeight,
-}
-use SVGSizeError as SvSE;
-
 impl Contents {
   #[throws(SpecError)]
   fn load_svg(&self, item_name: &SvgBaseName<str>,
@@ -547,41 +538,7 @@ impl Contents {
         SpE::InternalError(m.to_string())
       })?;
 
-    #[throws(SVGSizeError)]
-    fn get_width_height(xml: &str) -> Option<PosC<f64>> {
-      use xmlparser::Token as Tk;
-      let mut in_svg_element = false;
-      let mut wh = [None; 2];
-      for token in xmlparser::Tokenizer::from(xml) {
-        match token.map_err(|e| SvSE::ParseError(e.to_string()))? {
-          Tk::ElementStart{ local, .. } => {
-            in_svg_element = local.eq_ignore_ascii_case("svg");
-          },
-          Tk::ElementEnd{..} => {
-            if in_svg_element { return None }
-          },
-          Tk::Attribute { local, value, .. } if in_svg_element => {
-            let i =
-              if local.eq_ignore_ascii_case("width" ) { 0 } else
-              if local.eq_ignore_ascii_case("height") { 1 } else { continue };
-            if wh[i].is_some() {
-              throw!(SvSE::AttributeRepeated(local.to_string()))
-            }
-            let v: f64 = value.parse().map_err(
-              |_| SvSE::AttributeUnparseable(local.to_string())
-            )?;
-            wh[i] = Some(v);
-            if wh.iter().all(Option::is_some) { break }
-          },
-          _ => { },
-        }
-      }
-      Some(PosC::try_from_iter_2(
-        wh.into_iter().map(|v| v.ok_or_else(|| SvSE::OneOfWidthHeight))
-      )?)
-    }
-
-    let _ = get_width_height(&svg_data).map_err(|error| SpE::SVGError {
+    let _ = svg_parse_size(&svg_data).map_err(|error| SpE::SVGError {
       error,
       item_name: item_name.as_str().into(),
       item_for_lib: lib_name_for.into(),
index f79f8efdd064743642b4eff5961d2b3549fa9908..9140795eefd7a09a880a8da45c12f672722d3e9b 100644 (file)
@@ -30,6 +30,7 @@ use crate::accounts::AccountName;
 use crate::error::UnsupportedColourSpec;
 use crate::gamestate::PieceSpec;
 use crate::prelude::default;
+use crate::utils::SVGSizeError;
 
 pub use imp::PlayerAccessSpec;
 
@@ -96,7 +97,7 @@ pub enum SpecError {
     item_name: String,
     item_for_lib: String,
     item_for_item: String,
-    error: crate::shapelib::SVGSizeError, // TODO it needs to move
+    error: SVGSizeError,
   },
   #[error("image for supposedly-occultable piece \
            is not itself occultable but has multiple faces")]
index bfa18cd2ec78eb73d8bb3e1fa861c6bc5e494ff1..90a0903d441b87013dd6f303fc028ae0a88cc7c0 100644 (file)
@@ -212,6 +212,50 @@ impl<T> Index<OldNewIndex> for OldNew<T> {
   fn index(&self, i: OldNewIndex) -> &T { &self.0[i as usize] }
 }
 
+//========== obtaining size from xml ==========
+
+#[derive(Error,Clone,Serialize,Deserialize,Debug)]
+pub enum SVGSizeError {
+  #[error("parse error: {0}")]           ParseError(String),
+  #[error("attribute {0} repeated")]     AttributeRepeated(String),
+  #[error("attribute {0} unparseable")]  AttributeUnparseable(String),
+  #[error("specifies only one of width and height")] OneOfWidthHeight,
+}
+
+#[throws(SVGSizeError)]
+pub fn svg_parse_size(xml: &str) -> Option<PosC<f64>> {
+  use xmlparser::Token as Tk;
+  let mut in_svg_element = false;
+  let mut wh = [None; 2];
+  for token in xmlparser::Tokenizer::from(xml) {
+    match token.map_err(|e| SvSE::ParseError(e.to_string()))? {
+      Tk::ElementStart{ local, .. } => {
+        in_svg_element = local.eq_ignore_ascii_case("svg");
+      },
+      Tk::ElementEnd{..} => {
+        if in_svg_element { return None }
+      },
+      Tk::Attribute { local, value, .. } if in_svg_element => {
+        let i =
+          if local.eq_ignore_ascii_case("width" ) { 0 } else
+          if local.eq_ignore_ascii_case("height") { 1 } else { continue };
+        if wh[i].is_some() {
+          throw!(SvSE::AttributeRepeated(local.to_string()))
+        }
+        let v: f64 = value.parse().map_err(
+          |_| SvSE::AttributeUnparseable(local.to_string())
+        )?;
+        wh[i] = Some(v);
+        if wh.iter().all(Option::is_some) { break }
+      },
+      _ => { },
+    }
+  }
+  Some(PosC::try_from_iter_2(
+    wh.into_iter().map(|v| v.ok_or_else(|| SvSE::OneOfWidthHeight))
+  )?)
+}
+
 //========== Thunk ==========
 
 // todo #[derive(Clone)]