diff options
author | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-05-06 19:28:34 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2022-05-06 19:28:34 +0000 |
commit | 27b617cbaa9e6d4fb58a0b971b428a05da8ec5d9 (patch) | |
tree | 08067bf4290982e4a717ba55a660714084370158 /src/diagrams.rs | |
parent | c2cbccd45a2c356e458abc8bbef3193434e8572a (diff) | |
parent | 52ccfa5a1bf2893bdc63ff1bad2b2a88ca86ef95 (diff) | |
download | subplot-27b617cbaa9e6d4fb58a0b971b428a05da8ec5d9.tar.gz |
Merge branch 'liw/tidy-up-error' into 'main'
tidy up error handling a bit
See merge request subplot/subplot!275
Diffstat (limited to 'src/diagrams.rs')
-rw-r--r-- | src/diagrams.rs | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/diagrams.rs b/src/diagrams.rs index 96f5d70..2bdd0ed 100644 --- a/src/diagrams.rs +++ b/src/diagrams.rs @@ -1,4 +1,4 @@ -use crate::{Result, SubplotError}; +use crate::SubplotError; use std::env; use std::ffi::OsString; @@ -72,7 +72,7 @@ lazy_static! { /// for the trait. pub trait DiagramMarkup { /// Convert the markup into an SVG. - fn as_svg(&self) -> Result<Vec<u8>>; + fn as_svg(&self) -> Result<Vec<u8>, SubplotError>; } /// A code block with pikchr markup. @@ -99,7 +99,7 @@ impl PikchrMarkup { } impl DiagramMarkup for PikchrMarkup { - fn as_svg(&self) -> Result<Vec<u8>> { + fn as_svg(&self) -> Result<Vec<u8>, SubplotError> { let mut flags = pikchr::PikchrFlags::default(); flags.generate_plain_errors(); let image = pikchr::Pikchr::render(&self.markup, self.class.as_deref(), flags) @@ -130,16 +130,22 @@ impl DotMarkup { } impl DiagramMarkup for DotMarkup { - fn as_svg(&self) -> Result<Vec<u8>> { - let mut child = Command::new(DOT_PATH.lock().unwrap().clone()) + fn as_svg(&self) -> Result<Vec<u8>, SubplotError> { + let path = DOT_PATH.lock().unwrap().clone(); + let mut child = Command::new(&path) .arg("-Tsvg") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .spawn()?; + .spawn() + .map_err(|err| SubplotError::Spawn(path.clone(), err))?; if let Some(stdin) = child.stdin.as_mut() { - stdin.write_all(self.markup.as_bytes())?; - let output = child.wait_with_output()?; + stdin + .write_all(self.markup.as_bytes()) + .map_err(SubplotError::WriteToChild)?; + let output = child + .wait_with_output() + .map_err(SubplotError::WaitForChild)?; if output.status.success() { Ok(output.stdout) } else { @@ -191,8 +197,9 @@ impl PlantumlMarkup { } impl DiagramMarkup for PlantumlMarkup { - fn as_svg(&self) -> Result<Vec<u8>> { - let mut cmd = Command::new(JAVA_PATH.lock().unwrap().clone()); + fn as_svg(&self) -> Result<Vec<u8>, SubplotError> { + let path = JAVA_PATH.lock().unwrap().clone(); + let mut cmd = Command::new(&path); cmd.arg("-Djava.awt.headless=true") .arg("-jar") .arg(PLANTUML_JAR_PATH.lock().unwrap().clone()) @@ -208,10 +215,16 @@ impl DiagramMarkup for PlantumlMarkup { if let Some(path) = Self::build_java_path() { cmd.env("PATH", path); } - let mut child = cmd.spawn()?; + let mut child = cmd + .spawn() + .map_err(|err| SubplotError::Spawn(path.clone(), err))?; if let Some(stdin) = child.stdin.as_mut() { - stdin.write_all(self.markup.as_bytes())?; - let output = child.wait_with_output()?; + stdin + .write_all(self.markup.as_bytes()) + .map_err(SubplotError::WriteToChild)?; + let output = child + .wait_with_output() + .map_err(SubplotError::WaitForChild)?; if output.status.success() { Ok(output.stdout) } else { |