Skip to main content

chabeau/cli/settings/
mod.rs

1//! Settings management for CLI set/unset commands.
2//!
3//! This module provides a trait-based architecture for handling configuration
4//! settings, with different handler types for different setting patterns:
5//!
6//! - Simple settings (e.g., `default-provider`, `theme`)
7//! - Boolean settings (e.g., `markdown`, `syntax`)
8//! - String settings (e.g., `refine-instructions`, `refine-prefix`)
9//! - Provider-keyed settings (e.g., `default-model`)
10//! - Provider+model-keyed settings (e.g., `default-character`, `default-persona`)
11
12pub mod error;
13pub mod handlers;
14pub mod helpers;
15pub mod registry;
16
17pub use error::SettingError;
18pub use registry::SettingRegistry;
19
20use crate::character::CharacterService;
21use crate::core::config::data::Config;
22
23/// Context provided to setting handlers during set/unset operations.
24pub struct SetContext<'a> {
25    pub config: &'a Config,
26    pub character_service: &'a mut CharacterService,
27}
28
29/// Trait for handling a configuration setting.
30///
31/// Each implementation handles a specific configuration key,
32/// providing set, unset, and format operations.
33pub trait SettingHandler: Send + Sync {
34    /// Returns the configuration key this handler manages.
35    fn key(&self) -> &'static str;
36
37    /// Set the configuration value.
38    ///
39    /// # Arguments
40    /// * `args` - The arguments provided after the key (may be empty)
41    /// * `ctx` - Context containing config snapshot and services
42    ///
43    /// # Returns
44    /// A success message to display, or an error.
45    fn set(&self, args: &[String], ctx: &mut SetContext<'_>) -> Result<String, SettingError>;
46
47    /// Unset (clear) the configuration value.
48    ///
49    /// # Arguments
50    /// * `args` - Optional argument (e.g., provider name for provider-keyed settings)
51    /// * `_ctx` - Context (unused by most handlers)
52    ///
53    /// # Returns
54    /// A success message to display, or an error.
55    fn unset(&self, args: Option<&str>, _ctx: &mut SetContext<'_>) -> Result<String, SettingError>;
56
57    /// Format the current value for display in `chabeau set` output.
58    fn format(&self, config: &Config) -> String;
59}