Skip to main content

ConfigMenu

Struct ConfigMenu 

Source
pub struct ConfigMenu<'a> { /* private fields */ }
Expand description

A configurable terminal menu for viewing and editing application settings.

ConfigMenu reads key/value settings from a SQLite table, displays them as a numbered menu, and allows the selected value to be updated.

It can optionally mask secret values, validate proposed changes, customise ordering, and register additional user-defined menu actions.

Implementations§

Source§

impl<'a> ConfigMenu<'a>

Source

pub fn table(self, table: impl Into<String>) -> Self

Sets the database table to load settings from. The specified table must have at least two columns: one for keys and one for values.

§Arguments
  • table - The name of the database table to use for loading and updating settings.
§Returns

The ConfigMenu instance with the updated table configuration.

Source

pub fn key_column(self, key_column: impl Into<String>) -> Self

Sets the column name to use for setting keys. The specified column must contain unique values that identify each setting.

§Arguments
  • key_column - The name of the column to use for setting keys.
§Returns

The ConfigMenu instance with the updated key column configuration.

Source

pub fn value_column(self, value_column: impl Into<String>) -> Self

Sets the column name to use for setting values. The specified column must contain the values associated with each setting key.

§Arguments
  • value_column - The name of the column to use for setting values.
§Returns

The ConfigMenu instance with the updated value column configuration.

Source

pub fn order_by(self, order_by: impl Into<String>) -> Self

Sets the column name to use for ordering settings in the menu. If not specified, settings will be ordered by the key column. The specified column must contain values that can be used to determine the display order of settings.

§Arguments
  • order_by - The name of the column to use for ordering settings in the menu.
§Returns

The ConfigMenu instance with the updated ordering configuration.

Source

pub fn secret_keys<I, S>(self, keys: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Sets the keys of settings that should be treated as secrets and have their values masked in the menu. When a setting with a key in this list has a non-empty value, it will be displayed as “********” in the menu to indicate that it is a secret. The actual value of the setting will still be loaded and updated in the database as normal; this only affects how the value is displayed in the menu.

§Arguments
  • keys - An iterable of keys that should be treated as secrets and have their values masked in the menu.
§Returns

The ConfigMenu instance with the updated secret keys configuration.

Source

pub fn validator<F>(self, validator: F) -> Self
where F: Fn(&str, &str) -> Result<(), Box<dyn Error + Send + Sync>> + 'a,

Sets a custom validator function that will be called to validate new values for settings before they are updated in the database. The validator function should take the setting key and the new value as arguments and return Ok(()) if the value is valid or an Err with a message if the value is invalid. If a validator is set and it returns an error for a new value, the menu will display the error message and prevent the update from being applied to the database. This allows you to enforce constraints on setting values, such as requiring a certain format or range of values.

§Arguments
  • validator - A function that takes a setting key and a new value.
§Returns

The ConfigMenu instance with the updated validator configuration.

Source

pub fn action<F>( self, key: impl Into<String>, label: impl Into<String>, callback: F, ) -> Self
where F: Fn(&Connection) -> Result<(), Box<dyn Error + Send + Sync>> + 'a,

Adds a custom action to the menu that can be triggered by entering a specific key. The action will be displayed in the menu prompt with the specified label, and when the user enters the corresponding key, the provided callback function will be executed. The callback function should take a reference to the database connection as an argument and return Ok(()) if the action was successful or an Err with a message if the action failed. This allows you to add additional functionality to the menu, such as resetting settings to default values, syncing settings with an external service, or performing any other custom operation that can be implemented in Rust. The action key must not be empty, must not consist solely of digits, and must not be “q” or “quit” (case-sensitive), as these are reserved for exiting the menu. If an invalid or reserved action key is provided, the menu will return an error when you try to run it.

§Arguments
  • key - The key that the user must enter to trigger the action.
  • label - A description of the action that will be displayed in the menu prompt.
  • callback - A function that will be called when the action is triggered. It should take a reference to the database connection and return a Result indicating success or failure of the action.
§Returns

The ConfigMenu instance with the new action added to its configuration.

Source

pub fn run(&self) -> Result<(), ConfigEasyError>

Runs the configuration menu, allowing the user to view and edit settings in the database according to the menu’s configuration.

§Returns

A Result indicating whether the menu ran successfully or if an error occurred.

Auto Trait Implementations§

§

impl<'a> Freeze for ConfigMenu<'a>

§

impl<'a> !RefUnwindSafe for ConfigMenu<'a>

§

impl<'a> !Send for ConfigMenu<'a>

§

impl<'a> !Sync for ConfigMenu<'a>

§

impl<'a> Unpin for ConfigMenu<'a>

§

impl<'a> UnsafeUnpin for ConfigMenu<'a>

§

impl<'a> !UnwindSafe for ConfigMenu<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,