diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-05-06 08:39:14 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-05-06 09:34:28 +0300 |
commit | 2b7160ebdf950ef872f785336fcfe17e0c4cb347 (patch) | |
tree | 216fb5b9c2cf5fa6121e1b2d357508a0c675e3e9 /src/diagrams.rs | |
parent | 6498b2eecb21ad87069be8f501f35374745106d9 (diff) | |
download | subplot-2b7160ebdf950ef872f785336fcfe17e0c4cb347.tar.gz |
refactor! split SubplotError::IoError into more specific errors
Replace SubplotError::IoError with ::Spawn, ::WriteToChild,
::WaitForChild, ::ReadFile, ::CreateFile, ::Writefile.
IoError was a catchall error and as such, so generic that it didn't
help the user to figure out what actually is wrong. For example, there
was no indication what operation was attempted or on what file. The
new error variants are specific.
Sponsored-by: author
Diffstat (limited to 'src/diagrams.rs')
-rw-r--r-- | src/diagrams.rs | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/diagrams.rs b/src/diagrams.rs index 264bd3f..2bdd0ed 100644 --- a/src/diagrams.rs +++ b/src/diagrams.rs @@ -131,15 +131,21 @@ impl DotMarkup { impl DiagramMarkup for DotMarkup { fn as_svg(&self) -> Result<Vec<u8>, SubplotError> { - let mut child = Command::new(DOT_PATH.lock().unwrap().clone()) + 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 { @@ -192,7 +198,8 @@ impl PlantumlMarkup { impl DiagramMarkup for PlantumlMarkup { fn as_svg(&self) -> Result<Vec<u8>, SubplotError> { - let mut cmd = Command::new(JAVA_PATH.lock().unwrap().clone()); + 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 { |