Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: add a ConfigError type for config.rs
Lars Wirzenius committed 2 years ago
commit 0b862ab0ed18777e63fd3e62a3df202a07ddcb3e
parent f31ac159e087e90d621aca6bd78c28f1b0decdd4
2 files changed +21 -13
modified src/config.rs
@@ -8,7 +8,7 @@ use std::{

use serde::{Deserialize, Serialize};

-
use crate::{error::BrokerError, event::EventFilter};
+
use crate::event::EventFilter;

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
@@ -18,10 +18,10 @@ pub struct Config {
}

impl Config {
-
    pub fn load(filename: &Path) -> Result<Self, BrokerError> {
+
    pub fn load(filename: &Path) -> Result<Self, ConfigError> {
        let config =
-
            std::fs::read(filename).map_err(|e| BrokerError::ReadConfig(filename.into(), e))?;
-
        serde_yaml::from_slice(&config).map_err(|e| BrokerError::ParseConfig(filename.into(), e))
+
            std::fs::read(filename).map_err(|e| ConfigError::ReadConfig(filename.into(), e))?;
+
        serde_yaml::from_slice(&config).map_err(|e| ConfigError::ParseConfig(filename.into(), e))
    }

    pub fn adapter(&self, name: &str) -> Option<&Adapter> {
@@ -40,3 +40,15 @@ impl Adapter {
        self.env.iter().map(|(k, v)| (k.as_ref(), v.as_ref()))
    }
}
+

+
/// All possible errors from configuration handling.
+
#[derive(Debug, thiserror::Error)]
+
pub enum ConfigError {
+
    /// Can't read config file.
+
    #[error("could not read config file {0}")]
+
    ReadConfig(PathBuf, #[source] std::io::Error),
+

+
    /// Can't parse config file as YAML.
+
    #[error("failed to parse configuration file as YAML: {0}")]
+
    ParseConfig(PathBuf, #[source] serde_yaml::Error),
+
}
modified src/error.rs
@@ -2,11 +2,15 @@

use std::path::PathBuf;

-
use crate::msg::MessageError;
+
use crate::{config::ConfigError, msg::MessageError};

/// All possible errors from the CI broker messages.
#[derive(Debug, thiserror::Error)]
pub enum BrokerError {
+
    /// A configuration related error.
+
    #[error(transparent)]
+
    Config(#[from] ConfigError),
+

    /// A message related error.
    #[error(transparent)]
    Message(#[from] MessageError),
@@ -27,14 +31,6 @@ pub enum BrokerError {
    #[error("usage: radicle-ci-broker CONFIG")]
    Usage,

-
    /// Can't read config file.
-
    #[error("could not read config file {0}")]
-
    ReadConfig(PathBuf, #[source] std::io::Error),
-

-
    /// Can't parse config file as YAML.
-
    #[error("failed to parse configuration file as YAML: {0}")]
-
    ParseConfig(PathBuf, #[source] serde_yaml::Error),
-

    /// Default adapter is not in list of adapters.
    #[error("default adapter is not in list of adapters")]
    UnknownDefaultAdapter(String),