Skip to main content

laurus_server/
config.rs

1//! Server configuration types deserialized from a TOML file.
2//!
3//! The top-level [`Config`] struct contains sections for the gRPC/HTTP server,
4//! index storage, and logging. All sections have sensible defaults so that
5//! a minimal (or even empty) TOML file produces a working configuration.
6
7use serde::Deserialize;
8use std::path::{Path, PathBuf};
9
10/// Top-level configuration loaded from a TOML file.
11#[derive(Debug, Deserialize, Default)]
12pub struct Config {
13    /// Network settings for the gRPC server and the optional HTTP gateway.
14    #[serde(default)]
15    pub server: ServerConfig,
16    /// Index storage settings (e.g. data directory path).
17    #[serde(default)]
18    pub index: IndexConfig,
19}
20
21/// Server network configuration.
22#[derive(Debug, Deserialize)]
23pub struct ServerConfig {
24    /// Listen address for the gRPC server.
25    #[serde(default = "default_host")]
26    pub host: String,
27    /// Listen port for the gRPC server.
28    #[serde(default = "default_port")]
29    pub port: u16,
30    /// Listen port for the HTTP Gateway. The Gateway is started only when this is set.
31    #[serde(default)]
32    pub http_port: Option<u16>,
33}
34
35impl Default for ServerConfig {
36    fn default() -> Self {
37        Self {
38            host: default_host(),
39            port: default_port(),
40            http_port: None,
41        }
42    }
43}
44
45/// Index storage settings.
46#[derive(Debug, Deserialize)]
47pub struct IndexConfig {
48    /// Filesystem path where the index data (schema and store) is persisted.
49    /// Defaults to `"./laurus_data"`.
50    #[serde(default = "default_data_dir")]
51    pub data_dir: PathBuf,
52}
53
54impl Default for IndexConfig {
55    fn default() -> Self {
56        Self {
57            data_dir: default_data_dir(),
58        }
59    }
60}
61
62fn default_host() -> String {
63    "0.0.0.0".to_string()
64}
65
66fn default_port() -> u16 {
67    50051
68}
69
70fn default_data_dir() -> PathBuf {
71    PathBuf::from("./laurus_data")
72}
73
74impl Config {
75    /// Load configuration from a TOML file.
76    ///
77    /// # Arguments
78    ///
79    /// * `path` - Filesystem path to the TOML configuration file.
80    ///
81    /// # Returns
82    ///
83    /// A fully populated [`Config`] instance with defaults applied for any
84    /// missing sections or fields.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if the file cannot be read or if the TOML content
89    /// cannot be deserialized into a [`Config`].
90    pub fn from_file(path: &Path) -> anyhow::Result<Self> {
91        let content = std::fs::read_to_string(path)?;
92        let config: Config = toml::from_str(&content)?;
93        Ok(config)
94    }
95}