Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: Config::default_adapter returns an error if no default set
Lars Wirzenius committed 1 year ago
commit 07d9db2018e6181f2cffafddf5d5b5e36fec3c4f
parent c89b471f847c66761afb1147edf502e937011080
2 files changed +10 -13
modified src/bin/cib.rs
@@ -175,11 +175,7 @@ impl QueuedCmd {

        let mut broker =
            Broker::new(config.db(), config.max_run_time()).map_err(CibError::new_broker)?;
-
        let spec = config
-
            .default_adapter()
-
            .ok_or(CibError::UnknownDefaultAdapter(
-
                config.default_adapter_name().into(),
-
            ))?;
+
        let spec = config.default_adapter()?;
        let adapter = Adapter::new(&spec.command)
            .with_environment(spec.envs())
            .with_sensitive_environment(spec.sensitive_envs());
@@ -270,11 +266,7 @@ impl ProcessEventsCmd {

        let mut broker =
            Broker::new(config.db(), config.max_run_time()).map_err(CibError::new_broker)?;
-
        let spec = config
-
            .default_adapter()
-
            .ok_or(CibError::UnknownDefaultAdapter(
-
                config.default_adapter_name().into(),
-
            ))?;
+
        let spec = config.default_adapter().map_err(CibError::DefaultAdapter)?;
        let adapter = Adapter::new(&spec.command)
            .with_environment(spec.envs())
            .with_sensitive_environment(spec.sensitive_envs());
@@ -338,8 +330,8 @@ enum CibError {
    #[error("failed to add events to queue")]
    AddEvents(#[source] AdderError),

-
    #[error("default adapter is not in list of adapters")]
-
    UnknownDefaultAdapter(String),
+
    #[error(transparent)]
+
    DefaultAdapter(#[from] ConfigError),

    #[error("programming error: failed to set up inter-thread notification channel")]
    Notification(#[source] NotificationError),
modified src/config.rs
@@ -60,8 +60,9 @@ impl Config {
        &self.default_adapter
    }

-
    pub fn default_adapter(&self) -> Option<&Adapter> {
+
    pub fn default_adapter(&self) -> Result<&Adapter, ConfigError> {
        self.adapter(&self.default_adapter)
+
            .ok_or(ConfigError::NoDefaultAdapter)
    }

    pub fn adapter(&self, name: &str) -> Option<&Adapter> {
@@ -137,6 +138,10 @@ pub enum ConfigError {
    /// Can't convert configuration into JSON.
    #[error("failed to convert configuration into JSON")]
    ToJson(#[source] serde_json::Error),
+

+
    /// No default adapter.
+
    #[error("the default adapter is not defined in the configuration")]
+
    NoDefaultAdapter,
}

#[cfg(test)]