summaryrefslogtreecommitdiff
path: root/src/diagrams.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/diagrams.rs')
-rw-r--r--src/diagrams.rs39
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 {