From f42f4b2c27af2041436d681e4e292e549985066c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 6 May 2022 09:30:27 +0300 Subject: refactor: replace SubplotError::YamlError with more specific one SubplotError::YamlError is quite generic. We only parse YAML as part of document metadata, so replace the error with a more specific one. Sponsored-by: author --- src/bindings.rs | 3 ++- src/error.rs | 13 +++---------- src/templatespec.rs | 2 +- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index 5c98297..a6f96ca 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -463,7 +463,8 @@ impl Bindings { /// Add bindings from a YAML string pub fn add_from_yaml(&mut self, yaml: &str) -> Result<(), SubplotError> { - let bindings: Vec = serde_yaml::from_str(yaml)?; + let bindings: Vec = + serde_yaml::from_str(yaml).map_err(SubplotError::Metadata)?; for wrapper in bindings { self.add(from_hashmap(&wrapper.binding)?); } diff --git a/src/error.rs b/src/error.rs index 1b95f1c..5286410 100644 --- a/src/error.rs +++ b/src/error.rs @@ -309,16 +309,9 @@ pub enum SubplotError { source: serde_json::Error, }, - /// YAML error - /// - /// Subplot parses YAML. This is a generic wrapper for any kinds - /// of errors related to that. - #[error(transparent)] - YamlError { - /// The wrapped error. - #[from] - source: serde_yaml::Error, - }, + /// Error parsing YAML metadata for document. + #[error("Failed to parse YAML metadata")] + Metadata(#[source] serde_yaml::Error), /// Abstract syntax tree error. #[error(transparent)] diff --git a/src/templatespec.rs b/src/templatespec.rs index 7b8723b..28ab7e1 100644 --- a/src/templatespec.rs +++ b/src/templatespec.rs @@ -21,7 +21,7 @@ pub struct TemplateSpec { impl TemplateSpec { // Create a new TemplateSpec from YAML text. fn from_yaml(yaml: &str) -> Result { - Ok(serde_yaml::from_str(yaml)?) + serde_yaml::from_str(yaml).map_err(SubplotError::Metadata) } // Create a new TemplateSpec. -- cgit v1.2.1