commit 16ffa9a2ff3736412af3dd3807a501d37f20f7fc
parent 79417cefc3ad6844277860c8931655b237fba83c
Author: MTRNord <mtrnord1@gmail.com>
Date: Fri, 8 Jan 2021 14:14:53 +0100
fix: Use a proper error instead of returning a dyn trait
Diffstat:
5 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/mrsbfh-macros/src/lib.rs b/mrsbfh-macros/src/lib.rs
@@ -181,8 +181,8 @@ pub fn config_derive(input: TokenStream) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let expanded = quote! {
impl #impl_generics mrsbfh::config::Config for #name #ty_generics #where_clause {
- fn load<P: AsRef<std::path::Path> + std::fmt::Debug>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
- let contents = std::fs::read_to_string(path).expect("Something went wrong reading the file");
+ fn load<P: AsRef<std::path::Path> + std::fmt::Debug>(path: P) -> Result<Self, mrsbfh::errors::ConfigError> {
+ let contents = std::fs::read_to_string(path)?;
let config: Self = mrsbfh::serde_yaml::from_str(&contents)?;
Ok(config)
}
diff --git a/mrsbfh/Cargo.toml b/mrsbfh/Cargo.toml
@@ -13,6 +13,8 @@ rev = "b311a31c9ef942c357ceb16f07586246655191ce"
[dependencies]
url = "2.1.1"
+thiserror = "1.0"
+
# Command macros
mrsbfh-macros = {path = "../mrsbfh-macros"}
const-concat = {git = "https://github.com/Vurich/const-concat", rev = "f836e77a4ecadf6a11994340fdcbbdadcdc4906d"}
diff --git a/mrsbfh/src/config.rs b/mrsbfh/src/config.rs
@@ -32,13 +32,13 @@
use serde::de::DeserializeOwned;
use serde::Serialize;
-use std::error::Error;
use std::path::Path;
+use crate::errors::ConfigError;
pub use mrsbfh_macros::ConfigDerive;
pub trait Config {
- fn load<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<Self, Box<dyn Error>>
+ fn load<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<Self, ConfigError>
where
Self: Sized + Serialize + DeserializeOwned;
}
diff --git a/mrsbfh/src/errors.rs b/mrsbfh/src/errors.rs
@@ -0,0 +1,9 @@
+use thiserror::Error;
+
+#[derive(Error, Debug)]
+pub enum ConfigError {
+ #[error(transparent)]
+ IOError(#[from] std::io::Error),
+ #[error(transparent)]
+ SerdeError(#[from] serde_yaml::Error),
+}
diff --git a/mrsbfh/src/lib.rs b/mrsbfh/src/lib.rs
@@ -24,6 +24,7 @@
pub mod commands;
pub mod config;
+pub mod errors;
pub mod utils;
pub type Sender = tokio::sync::mpsc::Sender<matrix_sdk::events::AnyMessageEventContent>;