From: Ian Jackson Date: Sat, 22 May 2021 21:26:54 +0000 (+0100) Subject: bundles: Split out base64_usvg X-Git-Tag: otter-0.6.0~58 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=c450ca088b668185a8306b277777bfb47730d970;p=otter.git bundles: Split out base64_usvg Signed-off-by: Ian Jackson --- diff --git a/src/bundles.rs b/src/bundles.rs index 9b285562..9c142899 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -716,30 +716,36 @@ enum PictureFormat { Png, } +#[derive(Serialize,Copy,Clone,Debug)] +struct Base64Meta { + width: u32, + height: u32, + ctype: &'static str, +} + #[throws(LE)] fn image_usvg(zfname: &str, mut input: BufReader, output: File, format: image::ImageFormat, ctype: &'static str) { - #[derive(Serialize,Copy,Clone,Debug)] - struct Render { - width: u32, - height: u32, - ctype: &'static str, - } - - let mut output = BufWriter::new(output); - let image = image::io::Reader::with_format(&mut input, format); let (width, height) = image.into_dimensions().map_err( |e| LE::BadBundle(format!("{}: image examination failed: {}", zfname, e)))?; - let render = Render { width, height, ctype }; + let render = Base64Meta { width, height, ctype }; + base64_usvg(zfname, input, output, &render)?; +} + +#[throws(LE)] +fn base64_usvg(zfname: &str, mut input: BufReader, output: File, + render: &Base64Meta) { + input.rewind().context("rewind input").map_err(IE::from)?; + let mut output = BufWriter::new(output); + let rendered = nwtemplates::render("image-usvg.tera", &render) .map_err(IE::from)?; let (head, tail) = rendered.rsplit_once("@DATA@").ok_or_else( || IE::from(anyhow!("image-usvg template did not produce @DATA@")))?; - input.rewind().context("rewind input").map_err(IE::from)?; write!(output,"{}",head).context("write head to output").map_err(IE::from)?; let charset = base64::CharacterSet::Standard; let b64cfg = base64::Config::new(charset,true);