magnumdb 0.1.0

High-performance embeddable database engine in Rust with B+ Tree storage, WAL durability, Volcano query engine, and Postgres wire protocol support.
Documentation
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

/// The main configuration structure for MagnumDB.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    pub storage: StorageConfig,
    pub wal: WalConfig,
    pub network: NetworkConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    pub path: PathBuf,
    pub cache_size_mb: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WalConfig {
    pub enabled: bool,
    pub sync_on_write: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
    pub host: String,
    pub port: u16,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            storage: StorageConfig {
                path: PathBuf::from("./data"),
                cache_size_mb: 256,
            },
            wal: WalConfig {
                enabled: true,
                sync_on_write: false,
            },
            network: NetworkConfig {
                host: "127.0.0.1".to_string(),
                port: 7432,
            },
        }
    }
}

impl Config {
    /// Overrides the storage path. Useful for testing or programmatic setup.
    pub fn with_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.storage.path = path.as_ref().to_path_buf();
        self
    }

    /// Loads the configuration from a TOML file.
    pub fn load_from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self> {
        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }
}