config_easy/lib.rs
1//! A small interactive settings menu for command-line applications.
2//!
3//! `config-easy` reads key/value settings from a store, displays them in
4//! a terminal menu, prompts for edits, optionally validates new values, and saves
5//! updates back to the store.
6//!
7//! Applications remain responsible for creating tables, seeding defaults,
8//! parsing typed configuration, and deciding when to show the menu.
9//!
10//! # Features
11//!
12//! Default features are empty. The core crate has no database dependency.
13//!
14//! Enable the `rusqlite` feature to use [`sqlite::builder`] and [`sqlite::run`]
15//! with a `rusqlite::Connection`.
16//!
17//! # SQLite Example
18//!
19//! ```no_run
20//! # #[cfg(feature = "rusqlite")]
21//! # {
22//! let connection = rusqlite::Connection::open_in_memory()?;
23//!
24//! config_easy::sqlite::builder(&connection)
25//! .secret_keys(["api_token"])
26//! .validator(|key, value| {
27//! if key == "log_level" && !["debug", "info", "warn", "error"].contains(&value) {
28//! return Err(std::io::Error::new(
29//! std::io::ErrorKind::InvalidInput,
30//! "expected one of debug, info, warn, error",
31//! )
32//! .into());
33//! }
34//!
35//! Ok(())
36//! })
37//! .run()?;
38//! # }
39//!
40//! # Ok::<(), Box<dyn std::error::Error>>(())
41//! ```
42//!
43//! # Custom Store Example
44//!
45//! Implement [`SettingsStore`] when you want to use a different database layer,
46//! avoid a `rusqlite` version conflict, or store settings somewhere else.
47//!
48//! ```no_run
49//! use std::cell::RefCell;
50//!
51//! use config_easy::{SettingRow, SettingsQuery, SettingsStore};
52//!
53//! struct AppSettings {
54//! rows: RefCell<Vec<SettingRow>>,
55//! }
56//!
57//! impl SettingsStore for AppSettings {
58//! fn load_settings(
59//! &self,
60//! _query: &SettingsQuery<'_>,
61//! ) -> Result<Vec<SettingRow>, Box<dyn std::error::Error + Send + Sync>> {
62//! Ok(self.rows.borrow().clone())
63//! }
64//!
65//! fn update_setting(
66//! &self,
67//! _query: &SettingsQuery<'_>,
68//! key: &str,
69//! value: &str,
70//! ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
71//! if let Some(row) = self.rows.borrow_mut().iter_mut().find(|row| row.key == key) {
72//! row.value = value.to_string();
73//! }
74//!
75//! Ok(())
76//! }
77//! }
78//!
79//! let store = AppSettings {
80//! rows: RefCell::new(vec![SettingRow {
81//! key: "log_level".to_string(),
82//! value: "info".to_string(),
83//! }]),
84//! };
85//!
86//! let _menu = config_easy::builder(store).secret_keys(["api_token"]);
87//! ```
88
89mod action;
90mod error;
91mod identifier;
92mod menu;
93mod row;
94#[cfg(feature = "rusqlite")]
95pub mod sqlite;
96mod store;
97
98pub use error::ConfigEasyError;
99pub use menu::ConfigMenu;
100pub use row::SettingRow;
101pub use store::{SettingsQuery, SettingsStore};
102
103/// Creates a configurable settings menu for the provided settings store.
104///
105/// By default, the menu reads from `settings(key, value)` and orders rows by
106/// `key`. Use the returned [`ConfigMenu`] to customize table names, secret keys,
107/// validation, ordering, or custom actions before calling [`ConfigMenu::run`].
108pub fn builder<'a, S>(store: S) -> ConfigMenu<'a, S>
109where
110 S: SettingsStore,
111{
112 ConfigMenu::new(store)
113}
114
115/// Runs a settings menu with the default configuration.
116///
117/// This is equivalent to `config_easy::builder(store).run()`.
118pub fn run<S>(store: S) -> Result<(), ConfigEasyError>
119where
120 S: SettingsStore,
121{
122 builder(store).run()
123}