From 6498b2eecb21ad87069be8f501f35374745106d9 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 5 May 2022 19:28:57 +0300 Subject: refactor: drop the subplot::Result type alias Replace subplot::Result with Result. I find this now to be clearer, as I don't need to remind myself which Result is being used where. This should not be a breaking change. Sponsored-by: author --- src/diagrams.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/diagrams.rs') diff --git a/src/diagrams.rs b/src/diagrams.rs index 96f5d70..264bd3f 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>; + fn as_svg(&self) -> Result, SubplotError>; } /// A code block with pikchr markup. @@ -99,7 +99,7 @@ impl PikchrMarkup { } impl DiagramMarkup for PikchrMarkup { - fn as_svg(&self) -> Result> { + fn as_svg(&self) -> Result, SubplotError> { let mut flags = pikchr::PikchrFlags::default(); flags.generate_plain_errors(); let image = pikchr::Pikchr::render(&self.markup, self.class.as_deref(), flags) @@ -130,7 +130,7 @@ impl DotMarkup { } impl DiagramMarkup for DotMarkup { - fn as_svg(&self) -> Result> { + fn as_svg(&self) -> Result, SubplotError> { let mut child = Command::new(DOT_PATH.lock().unwrap().clone()) .arg("-Tsvg") .stdin(Stdio::piped()) @@ -191,7 +191,7 @@ impl PlantumlMarkup { } impl DiagramMarkup for PlantumlMarkup { - fn as_svg(&self) -> Result> { + fn as_svg(&self) -> Result, SubplotError> { let mut cmd = Command::new(JAVA_PATH.lock().unwrap().clone()); cmd.arg("-Djava.awt.headless=true") .arg("-jar") -- cgit v1.2.1 From 2b7160ebdf950ef872f785336fcfe17e0c4cb347 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 6 May 2022 08:39:14 +0300 Subject: 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 --- src/diagrams.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/diagrams.rs') 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, 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, 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 { -- cgit v1.2.1