// ---------- piece trait, and rendering ----------
+type SE = SVGProcessingError;
+type SR = Result<(),SE>;
+
pub trait Piece : Send + Debug {
- fn svg_piece(&self, pri : &PieceRenderInstructions) -> String;
+ fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) -> SR;
fn outline_path(&self, pri : &PieceRenderInstructions) -> String;
fn surround_path(&self, pri : &PieceRenderInstructions) -> String;
fn svg_x_ids(&self) -> VisiblePieceIdSvgIds;
Some(n) => n,
None => -1,
};
- write!(defs, r##"<g id="{}" data-dragraise="{}">{}</g>"##,
+ write!(defs, r##"<g id="{}" data-dragraise="{}">"##,
pri.id_piece(),
- dragraise,
- pr.p.svg_piece(&pri)).unwrap();
+ dragraise).unwrap();
+ pr.p.svg_piece(&mut defs, &pri).unwrap();
+ write!(defs, r##"</g>"##).unwrap();
write!(defs, r##"<path id="{}" stroke="black" fill="none" d="{}"/>"##,
pri.id_select(),
pr.p.surround_path(&pri)).unwrap();
#[derive(Copy,Clone,Debug,Error)]
-pub enum SVGProcessError {
+pub enum SVGProcessingError {
UnknownOperator,
BadNumber,
WriteFail,
}
-display_as_debug!{SVGProcessError}
-error_from_losedetails!{SVGProcessError, WriteFail, fmt::Error}
-error_from_losedetails!{SVGProcessError, BadNumber, std::num::ParseFloatError}
-#[throws(SVGProcessError)]
-pub fn svg_rescale_path(input: &str, scale: f64) -> String {
- use SVGProcessError::*;
+display_as_debug!{SVGProcessingError}
+error_from_losedetails!{SVGProcessingError,WriteFail,fmt::Error}
+error_from_losedetails!{SVGProcessingError,BadNumber,std::num::ParseFloatError}
+
+type SE = SVGProcessingError;
+#[throws(SE)]
+pub fn svg_rescale_path(input: &str, scale: f64) -> String {
type BM = u64;
type BI = u32;
#[derive(Debug,Copy,Clone)]
continue;
}
}
- _ => Err(UnknownOperator)?,
+ _ => Err(SE::UnknownOperator)?,
};
write!(&mut out, "{}", w)?;
}
}
impl Piece for SimpleShape {
- fn svg_piece(&self, pri : &PieceRenderInstructions) -> String {
- format!(r##"<path fill="{}" d="{}"/>"##,
- self.colours[pri.face], self.path)
+ #[throws(SE)]
+ fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) {
+ write!(f, r##"<path fill="{}" d="{}"/>"##,
+ self.colours[pri.face], self.path)?;
}
fn outline_path(&self, _pri : &PieceRenderInstructions) -> String {
self.path.clone()
}
impl SimpleShape {
- #[throws(SVGProcessError)]
+ #[throws(SE)]
fn new_from_path(desc: String, path: String, approx_dia: Coord,
colours: ColourMap) -> Self {
SimpleShape {
desc, approx_dia, path, colours,
}
}
- #[throws(SVGProcessError)]
+ #[throws(SE)]
fn new_circle(dia: Coord, colours: ColourMap) -> Self {
let unit_path =
"M 0 1 a 1 1 0 1 0 0 -2 \
let path = svg_rescale_path(&unit_path, scale)?;
Self::new_from_path("circle".to_owned(), path, dia, colours)?
}
- #[throws(SVGProcessError)]
+ #[throws(SE)]
fn new_square(edgelen: Coord, colours: ColourMap) -> Self {
let unit_path =
"M -1 -1 h 2 v 2 h -2 z";
}
}
-pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn Piece>)>,SVGProcessError> {
+pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn Piece>)>,SE> {
Ok(vec![
([ 90, 80 ],
Box::new(SimpleShape::new_circle(