Skip to main content

config_easy/
lib.rs

1//! A small SQLite-backed interactive settings menu for command-line applications.
2//!
3//! `config-easy` reads key/value settings from a SQLite table, displays them in
4//! a terminal menu, prompts for edits, optionally validates new values, and saves
5//! updates back to SQLite.
6//!
7//! Applications remain responsible for creating tables, seeding defaults,
8//! parsing typed configuration, and deciding when to show the menu.
9//!
10//! # Example
11//!
12//! ```no_run
13//! let connection = rusqlite::Connection::open("app.db")?;
14//!
15//! config_easy::builder(&connection)
16//!     .secret_keys(["api_token"])
17//!     .validator(|key, value| {
18//!         if key == "log_level" && !["debug", "info", "warn", "error"].contains(&value) {
19//!             return Err(std::io::Error::new(
20//!                 std::io::ErrorKind::InvalidInput,
21//!                 "expected one of debug, info, warn, error",
22//!             )
23//!             .into());
24//!         }
25//!
26//!         Ok(())
27//!     })
28//!     .run()?;
29//!
30//! # Ok::<(), Box<dyn std::error::Error>>(())
31//! ```
32
33mod action;
34mod error;
35mod identifier;
36mod menu;
37mod row;
38
39pub use error::ConfigEasyError;
40pub use menu::ConfigMenu;
41
42/// Creates a configurable settings menu for the provided SQLite connection.
43///
44/// By default, the menu reads from `settings(key, value)` and orders rows by
45/// `key`. Use the returned [`ConfigMenu`] to customize table names, secret keys,
46/// validation, ordering, or custom actions before calling [`ConfigMenu::run`].
47pub fn builder(conn: &rusqlite::Connection) -> ConfigMenu<'_> {
48    ConfigMenu::new(conn)
49}
50
51/// Runs a settings menu with the default configuration.
52///
53/// This is equivalent to `config_easy::builder(conn).run()`.
54pub fn run(conn: &rusqlite::Connection) -> Result<(), ConfigEasyError> {
55    builder(conn).run()
56}