Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: prevent direct access to struct Config fields
Lars Wirzenius committed 1 year ago
commit 47f776392e7a5b1205e498525cce50af73cb3732
parent 30fcabba09b6812274c61e4eb2940faa7cb6da00
2 files changed +45 -27
modified src/bin/cib.rs
@@ -83,7 +83,7 @@ impl Args {
    }

    fn open_db(&self, config: &Config) -> Result<Db, CibError> {
-
        let db = Db::new(&config.db).map_err(CibError::db)?;
+
        let db = Db::new(config.db()).map_err(CibError::db)?;
        let events = db.queued_events().map_err(CibError::Db)?;
        if events.is_empty() {
            Ok(db)
@@ -150,7 +150,7 @@ impl InsertCmd {
        let adder = QueueAdderBuilder::default()
            .events_tx(events_notification.tx().map_err(CibError::notification)?)
            .db(args.open_db(config)?)
-
            .filters(&config.filters)
+
            .filters(config.filters())
            .build()
            .map_err(CibError::QueueAdder)?;
        let thread = adder.add_events_in_thread();
@@ -175,12 +175,11 @@ impl QueuedCmd {

        let mut broker =
            Broker::new(config.db(), config.max_run_time()).map_err(CibError::new_broker)?;
-
        let spec =
-
            config
-
                .adapter(&config.default_adapter)
-
                .ok_or(CibError::UnknownDefaultAdapter(
-
                    config.default_adapter.clone(),
-
                ))?;
+
        let spec = config
+
            .default_adapter()
+
            .ok_or(CibError::UnknownDefaultAdapter(
+
                config.default_adapter_name().into(),
+
            ))?;
        let adapter = Adapter::new(&spec.command)
            .with_environment(spec.envs())
            .with_sensitive_environment(spec.sensitive_envs());
@@ -201,7 +200,7 @@ impl QueuedCmd {
            .events_rx(event_notifications.rx().map_err(CibError::notification)?)
            .run_tx(run_notifications.tx().map_err(CibError::notification)?)
            .db(db)
-
            .queue_len_interval(config.queue_len_interval)
+
            .queue_len_interval(config.queue_len_interval())
            .broker(broker)
            .build()
            .map_err(CibError::process_queue)?;
@@ -213,7 +212,7 @@ impl QueuedCmd {

        let db = args.open_db(config)?;
        let mut page = StatusPage::default();
-
        if let Some(dirname) = &config.report_dir {
+
        if let Some(dirname) = config.report_dir() {
            page.set_output_dir(dirname);
        }
        let page_updater = page.update_in_thread(
@@ -249,7 +248,7 @@ impl ProcessEventsCmd {
        let adder = QueueAdderBuilder::default()
            .events_tx(events_notification.tx().map_err(CibError::notification)?)
            .db(args.open_db(config)?)
-
            .filters(&config.filters)
+
            .filters(config.filters())
            .build()
            .map_err(CibError::QueueAdder)?;
        adder.add_events_in_thread();
@@ -259,7 +258,7 @@ impl ProcessEventsCmd {
        let db = args.open_db(config)?;

        let mut page = StatusPage::default();
-
        if let Some(dirname) = &config.report_dir {
+
        if let Some(dirname) = config.report_dir() {
            page.set_output_dir(dirname);
        }
        let page_updater = page.update_in_thread(
@@ -271,12 +270,11 @@ impl ProcessEventsCmd {

        let mut broker =
            Broker::new(config.db(), config.max_run_time()).map_err(CibError::new_broker)?;
-
        let spec =
-
            config
-
                .adapter(&config.default_adapter)
-
                .ok_or(CibError::UnknownDefaultAdapter(
-
                    config.default_adapter.clone(),
-
                ))?;
+
        let spec = config
+
            .default_adapter()
+
            .ok_or(CibError::UnknownDefaultAdapter(
+
                config.default_adapter_name().into(),
+
            ))?;
        let adapter = Adapter::new(&spec.command)
            .with_environment(spec.envs())
            .with_sensitive_environment(spec.sensitive_envs());
@@ -287,7 +285,7 @@ impl ProcessEventsCmd {
            .events_rx(events_notification.rx().map_err(CibError::notification)?)
            .run_tx(run_notification.tx().map_err(CibError::notification)?)
            .db(args.open_db(config)?)
-
            .queue_len_interval(config.queue_len_interval)
+
            .queue_len_interval(config.queue_len_interval())
            .broker(broker)
            .build()
            .map_err(CibError::process_queue)?;
modified src/config.rs
@@ -17,20 +17,20 @@ const DEFAULT_STATUS_PAGE_UPDATE_INTERVAL: u64 = 10;

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
-
    pub default_adapter: String,
-
    pub adapters: HashMap<String, Adapter>,
-
    pub filters: Vec<EventFilter>,
-
    pub report_dir: Option<PathBuf>,
-
    pub status_update_interval_seconds: Option<u64>,
-
    pub db: PathBuf,
+
    default_adapter: String,
+
    adapters: HashMap<String, Adapter>,
+
    filters: Vec<EventFilter>,
+
    report_dir: Option<PathBuf>,
+
    status_update_interval_seconds: Option<u64>,
+
    db: PathBuf,

    #[serde(deserialize_with = "deserialize_duration")]
    #[serde(default = "default_max_run_time")]
-
    pub max_run_time: Duration,
+
    max_run_time: Duration,

    #[serde(deserialize_with = "deserialize_duration")]
    #[serde(default = "default_queue_len_interval")]
-
    pub queue_len_interval: Duration,
+
    queue_len_interval: Duration,
}

fn default_max_run_time() -> Duration {
@@ -48,6 +48,22 @@ impl Config {
        serde_yml::from_slice(&config).map_err(|e| ConfigError::ParseConfig(filename.into(), e))
    }

+
    pub fn report_dir(&self) -> Option<&Path> {
+
        self.report_dir.as_deref()
+
    }
+

+
    pub fn filters(&self) -> &[EventFilter] {
+
        &self.filters
+
    }
+

+
    pub fn default_adapter_name(&self) -> &str {
+
        &self.default_adapter
+
    }
+

+
    pub fn default_adapter(&self) -> Option<&Adapter> {
+
        self.adapter(&self.default_adapter)
+
    }
+

    pub fn adapter(&self, name: &str) -> Option<&Adapter> {
        self.adapters.get(name)
    }
@@ -61,6 +77,10 @@ impl Config {
        self.max_run_time
    }

+
    pub fn queue_len_interval(&self) -> Duration {
+
        self.queue_len_interval
+
    }
+

    pub fn db(&self) -> &Path {
        &self.db
    }