use mcpr_core::config::{ConfigIssue, ModuleConfig, Severity};
#[derive(serde::Deserialize, Default, Debug, Clone)]
#[serde(default)]
pub struct FileStoreConfig {
pub enabled: Option<bool>,
pub path: Option<String>,
pub name: Option<String>,
}
impl FileStoreConfig {
pub fn is_enabled(&self) -> bool {
self.enabled.unwrap_or(true)
}
}
impl ModuleConfig for FileStoreConfig {
fn name(&self) -> &'static str {
"store"
}
fn validate(&self) -> Vec<ConfigIssue> {
let mut issues = Vec::new();
if let Some(ref p) = self.path
&& p.trim().is_empty()
{
issues.push(ConfigIssue {
severity: Severity::Error,
module: "store",
message: "store.path cannot be an empty string — remove the key to use the platform default, or set a valid path".into(),
});
}
if let Some(ref n) = self.name
&& n.trim().is_empty()
{
issues.push(ConfigIssue {
severity: Severity::Error,
module: "store",
message: "store.name cannot be an empty string — remove the key to auto-derive from the upstream URL, or set a valid name".into(),
});
}
issues
}
}
#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
use super::*;
#[test]
fn file_store_config__default_is_valid() {
let config = FileStoreConfig::default();
assert!(config.is_enabled());
assert!(config.validate().is_empty());
}
#[test]
fn file_store_config__disabled_is_valid() {
let config = FileStoreConfig {
enabled: Some(false),
path: None,
name: None,
};
assert!(!config.is_enabled());
assert!(config.validate().is_empty());
}
#[test]
fn file_store_config__empty_path_is_error() {
let config = FileStoreConfig {
enabled: None,
path: Some("".into()),
name: None,
};
let issues = config.validate();
assert_eq!(issues.len(), 1);
assert_eq!(issues[0].severity, Severity::Error);
assert!(issues[0].message.contains("store.path"));
}
#[test]
fn file_store_config__empty_name_is_error() {
let config = FileStoreConfig {
enabled: None,
path: None,
name: Some(" ".into()),
};
let issues = config.validate();
assert_eq!(issues.len(), 1);
assert!(issues[0].message.contains("store.name"));
}
#[test]
fn file_store_config__parses_from_toml() {
let toml_str = r#"
enabled = false
path = "/tmp/mcpr.db"
name = "my-proxy"
"#;
let config: FileStoreConfig = toml::from_str(toml_str).unwrap();
assert_eq!(config.enabled, Some(false));
assert_eq!(config.path.as_deref(), Some("/tmp/mcpr.db"));
assert_eq!(config.name.as_deref(), Some("my-proxy"));
}
}