repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

smith-core/src/config.rs

Raw
File outline
use serde::{Deserialize, Serialize};

/// Runtime configuration (validated, no plugin loading).
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Config {
    /// Session directory path.
    pub session_dir: String,
    /// Maximum frame size in bytes.
    pub max_frame_bytes: usize,
    /// Timeout for tool effects.
    pub tool_timeout_ms: u64,
    /// Whether to preserve unknown frames during repair.
    pub preserve_unknown_frames: bool,
}

impl Config {
    /// Default validated configuration. Session data resolves under the
    /// platform data directory (XDG data home on Linux).
    pub fn default_valid() -> Self {
        let session_dir = match dirs::data_dir() {
            Some(base) => base.join("smith").join("sessions"),
            None => std::path::PathBuf::from(".smith").join("sessions"),
        };
        Self {
            session_dir: session_dir.to_string_lossy().into_owned(),
            max_frame_bytes: 1024 * 1024,
            tool_timeout_ms: 30_000,
            preserve_unknown_frames: true,
        }
    }

    /// Validate the configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.session_dir.trim().is_empty() {
            return Err("session_dir must not be empty".to_string());
        }
        if self.max_frame_bytes < 1024 {
            return Err("max_frame_bytes must be at least 1024".to_string());
        }
        if self.max_frame_bytes > 1024 * 1024 * 100 {
            return Err("max_frame_bytes exceeds safe limit of 100MB".to_string());
        }
        if self.tool_timeout_ms < 100 {
            return Err("tool_timeout_ms must be at least 100ms".to_string());
        }
        Ok(())
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::default_valid()
    }
}