refactor: Config::default_adapter returns an error if no default set

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2025-01-20 17:15:41 +02:00
parent c89b471f84
commit 07d9db2018
2 changed files with 10 additions and 13 deletions
+4 -12
View File
@@ -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),
+6 -1
View File
@@ -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)]