config-easy 0.2.1

A small interactive settings menu for command-line Rust applications
Documentation
//! 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
//!
//! ```no_run
//! # #[cfg(feature = "rusqlite")]
//! # {
//! 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()?;
//! # }
//!
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # Custom Store Example
//!
//! Implement [`SettingsStore`] when you want to use a different database layer,
//! avoid a `rusqlite` version conflict, or store settings somewhere else.
//!
//! ```no_run
//! 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"]);
//! ```

mod action;
mod error;
mod identifier;
mod menu;
mod row;
#[cfg(feature = "rusqlite")]
pub mod sqlite;
mod store;

pub use error::ConfigEasyError;
pub use menu::ConfigMenu;
pub use row::SettingRow;
pub use store::{SettingsQuery, SettingsStore};

/// Creates a configurable settings menu for the provided settings store.
///
/// By default, the menu reads from `settings(key, value)` and orders rows by
/// `key`. Use the returned [`ConfigMenu`] to customize table names, secret keys,
/// validation, ordering, or custom actions before calling [`ConfigMenu::run`].
pub fn builder<'a, S>(store: S) -> ConfigMenu<'a, S>
where
    S: SettingsStore,
{
    ConfigMenu::new(store)
}

/// Runs a settings menu with the default configuration.
///
/// This is equivalent to `config_easy::builder(store).run()`.
pub fn run<S>(store: S) -> Result<(), ConfigEasyError>
where
    S: SettingsStore,
{
    builder(store).run()
}