Skip to main content

Crate config_easy

Crate config_easy 

Source
Expand description

A small interactive settings menu for command-line applications.

config-easy reads key/value settings from a store, displays them in a terminal menu, prompts for edits, optionally validates new values, and saves updates back to the store.

Applications remain responsible for creating tables, seeding defaults, parsing typed configuration, and deciding when to show the menu.

§Features

Default features are empty. The core crate has no database dependency.

Enable the rusqlite feature to use [sqlite::builder] and [sqlite::run] with a rusqlite::Connection.

§SQLite Example

let connection = rusqlite::Connection::open_in_memory()?;

config_easy::sqlite::builder(&connection)
    .secret_keys(["api_token"])
    .validator(|key, value| {
        if key == "log_level" && !["debug", "info", "warn", "error"].contains(&value) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "expected one of debug, info, warn, error",
            )
            .into());
        }

        Ok(())
    })
    .run()?;

§Custom Store Example

Implement SettingsStore when you want to use a different database layer, avoid a rusqlite version conflict, or store settings somewhere else.

use std::cell::RefCell;

use config_easy::{SettingRow, SettingsQuery, SettingsStore};

struct AppSettings {
    rows: RefCell<Vec<SettingRow>>,
}

impl SettingsStore for AppSettings {
    fn load_settings(
        &self,
        _query: &SettingsQuery<'_>,
    ) -> Result<Vec<SettingRow>, Box<dyn std::error::Error + Send + Sync>> {
        Ok(self.rows.borrow().clone())
    }

    fn update_setting(
        &self,
        _query: &SettingsQuery<'_>,
        key: &str,
        value: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        if let Some(row) = self.rows.borrow_mut().iter_mut().find(|row| row.key == key) {
            row.value = value.to_string();
        }

        Ok(())
    }
}

let store = AppSettings {
    rows: RefCell::new(vec![SettingRow {
        key: "log_level".to_string(),
        value: "info".to_string(),
    }]),
};

let _menu = config_easy::builder(store).secret_keys(["api_token"]);

Structs§

ConfigMenu
A configurable terminal menu for viewing and editing application settings.
SettingRow
SettingsQuery
A database query description for loading and updating settings.

Enums§

ConfigEasyError
A custom error type for the configuration menu, encompassing various error scenarios that may occur during menu operation.

Traits§

SettingsStore
A storage backend that can load and update string settings.

Functions§

builder
Creates a configurable settings menu for the provided settings store.
run
Runs a settings menu with the default configuration.