From a8a14c41d818cb8c5ad6a78aca024b855231e027 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 29 Oct 2021 23:39:23 +0200 Subject: driver: recipe: use common error types --- crates/driver/src/recipe.rs | 41 ++++++----------------------------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/crates/driver/src/recipe.rs b/crates/driver/src/recipe.rs index 04f356b..0121800 100644 --- a/crates/driver/src/recipe.rs +++ b/crates/driver/src/recipe.rs @@ -1,10 +1,10 @@ -use std::{collections::HashMap, fmt, fs::File, io, path::Path, result}; +use std::{collections::HashMap, fs::File, path::Path, result}; use scoped_tls_hkt::scoped_thread_local; use serde::{Deserialize, Deserializer}; use walkdir::WalkDir; -use common::types::*; +use common::{error::*, types::*}; use crate::task::{RecipeMeta, TaskDef}; @@ -37,41 +37,12 @@ struct Recipe { pub tasks: HashMap, } -#[derive(Debug)] -pub enum Error { - IOError(io::Error), - YAMLError(serde_yaml::Error), -} - -impl From for Error { - fn from(err: io::Error) -> Self { - Error::IOError(err) - } -} - -impl From for Error { - fn from(err: serde_yaml::Error) -> Self { - Error::YAMLError(err) - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Error::IOError(err) => write!(f, "IO error: {}", err), - Error::YAMLError(err) => write!(f, "YAML error: {}", err), - } - } -} - -impl std::error::Error for Error {} - -pub type Result = std::result::Result; - fn read_recipe(path: &Path) -> Result { - let f = File::open(path)?; + let f = File::open(path).context("IO error")?; - let recipe: Recipe = serde_yaml::from_reader(f)?; + let recipe: Recipe = serde_yaml::from_reader(f) + .map_err(Error::new) + .context("YAML error")?; Ok(recipe) } -- cgit v1.2.3