From 703646c67970f95240622b8b2cd3b18a79b3763d Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Wed, 22 Dec 2021 22:41:52 -0500 Subject: [PATCH 1/5] [errors] add support for chained norad error reporting #43 --- src/lib/errors.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib/errors.rs b/src/lib/errors.rs index aee3224..f2cf1ef 100644 --- a/src/lib/errors.rs +++ b/src/lib/errors.rs @@ -19,14 +19,29 @@ pub(crate) enum Error { NoradWrite(PathBuf, norad::Error), } +fn chained_error_fmt( + e: &impl std::error::Error, + f: &mut std::fmt::Formatter<'_>, +) -> std::fmt::Result { + writeln!(f, "{}\n", e)?; + let mut current = e.source(); + while let Some(cause) = current { + writeln!(f, "Caused by:\n\t{}", cause)?; + current = cause.source(); + } + Ok(()) +} + impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> { match &self { Error::NoradRead(p, e) => { - write!(f, "norad read error: {}: {}", p.display(), e) + writeln!(f, "norad read error: {}", p.display())?; + chained_error_fmt(e, f) } Error::NoradWrite(p, e) => { - write!(f, "norad write error: {}: {}", p.display(), e) + writeln!(f, "norad write error: {}", p.display())?; + chained_error_fmt(e, f) } Error::InvalidPath(p) => { write!(f, "invalid path error: {} was not found", p.display()) From 0ff35b483f0624ab1eb1660652b11fed561125b6 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Wed, 22 Dec 2021 22:50:27 -0500 Subject: [PATCH 2/5] [errors] refactor chained_error_fmt variable names to improve clarity --- src/lib/errors.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/errors.rs b/src/lib/errors.rs index f2cf1ef..7f98db4 100644 --- a/src/lib/errors.rs +++ b/src/lib/errors.rs @@ -19,15 +19,16 @@ pub(crate) enum Error { NoradWrite(PathBuf, norad::Error), } +// Implementation adapted from https://www.lpalmieri.com/posts/error-handling-rust/ fn chained_error_fmt( e: &impl std::error::Error, f: &mut std::fmt::Formatter<'_>, ) -> std::fmt::Result { writeln!(f, "{}\n", e)?; - let mut current = e.source(); - while let Some(cause) = current { - writeln!(f, "Caused by:\n\t{}", cause)?; - current = cause.source(); + let mut current_err = e.source(); + while let Some(err_cause) = current_err { + writeln!(f, "Caused by:\n\t{}", err_cause)?; + current_err = err_cause.source(); } Ok(()) } From cc56f99de626211ffa16000171d70b3921ffab57 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Fri, 28 Jan 2022 00:36:49 -0500 Subject: [PATCH 3/5] based chained errors on unreleased master branch norad sources --- Cargo.lock | 43 ++++++++++++++++++++++++++----------------- Cargo.toml | 3 ++- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e9a5535..85fa190 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,9 +197,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.1" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "lazy_static" @@ -240,15 +240,14 @@ dependencies = [ [[package]] name = "norad" version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce992b9240bd5d664ac85340de11a3800402f7552673898e176262a59447e016" +source = "git+https://github.com/linebender/norad?branch=master#3444ad39867a053a73c6f53468eb3905201fcf19" dependencies = [ "plist", "quick-xml", - "rayon", "serde", "serde_derive", "serde_repr", + "thiserror", "uuid", ] @@ -262,15 +261,6 @@ dependencies = [ "libc", ] -[[package]] -name = "num_threads" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97ba99ba6393e2c3734791401b66902d981cb03bf190af674ca69949b6d5fb15" -dependencies = [ - "libc", -] - [[package]] name = "output_vt100" version = "0.1.2" @@ -528,15 +518,34 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" -version = "0.3.7" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d" +checksum = "41effe7cfa8af36f439fac33861b66b049edc6f9a32331e2312660529c1c24ad" dependencies = [ "itoa", "libc", - "num_threads", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b6e59d9..07e9831 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,8 @@ exclude = [ ] [dependencies] -norad = { version = "0.6.0", features = ["rayon"] } +# norad = { version = "0.6.0", features = ["rayon"] } +norad = { git = "https://github.com/linebender/norad", branch = "master" } structopt = "0.3" colored = "2.0" rayon = "1.5" From 9015acbb9948e163aa4f868277b62fadb9e99cea Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Fri, 28 Jan 2022 00:37:15 -0500 Subject: [PATCH 4/5] [errors] refactor to new norad load and write error types --- src/lib/errors.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/errors.rs b/src/lib/errors.rs index 7f98db4..a1e2f7c 100644 --- a/src/lib/errors.rs +++ b/src/lib/errors.rs @@ -15,8 +15,8 @@ lazy_static! { #[derive(Debug)] pub(crate) enum Error { InvalidPath(PathBuf), - NoradRead(PathBuf, norad::Error), - NoradWrite(PathBuf, norad::Error), + NoradRead(PathBuf, norad::error::FontLoadError), + NoradWrite(PathBuf, norad::error::FontWriteError), } // Implementation adapted from https://www.lpalmieri.com/posts/error-handling-rust/ @@ -65,14 +65,14 @@ mod tests { #[test] fn test_ufofmterror_read() { - let ne = norad::Error::MissingLayer("test".to_owned()); + let ne = norad::error::FontLoadError::MissingDefaultLayer; let ufe = Error::NoradRead(PathBuf::from("test.ufo"), ne); assert!(ufe.to_string().starts_with("norad read error: ")); } #[test] fn test_ufofmterror_write() { - let ne = norad::Error::MissingLayer("test".to_owned()); + let ne = norad::error::FontWriteError::PreexistingPublicObjectLibsKey; let ufe = Error::NoradWrite(PathBuf::from("test.ufo"), ne); assert!(ufe.to_string().starts_with("norad write error: ")); } From bb323937f8621b679c652e2142825d6cf6a0f759 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Fri, 28 Jan 2022 01:22:14 -0500 Subject: [PATCH 5/5] [Cargo.lock] cargo update --- Cargo.lock | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85fa190..7347b53 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,9 +197,9 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.8" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "lazy_static" @@ -261,6 +261,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97ba99ba6393e2c3734791401b66902d981cb03bf190af674ca69949b6d5fb15" +dependencies = [ + "libc", +] + [[package]] name = "output_vt100" version = "0.1.2" @@ -540,12 +549,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41effe7cfa8af36f439fac33861b66b049edc6f9a32331e2312660529c1c24ad" +checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d" dependencies = [ "itoa", "libc", + "num_threads", ] [[package]]