use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DevKitConfig {
pub network: NetworkConfig,
pub decode: DecodeConfig,
#[serde(default)]
pub storage: StorageConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct NetworkConfig {
pub rpc_url: String,
pub passphrase: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DecodeConfig {
pub max_depth: usize,
pub allow_fallback_hex: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct StorageConfig {
pub max_entries: usize,
pub ttl_warning_days: u32,
}
impl Default for StorageConfig {
fn default() -> Self {
Self {
max_entries: 200,
ttl_warning_days: 30,
}
}
}
impl Default for DevKitConfig {
fn default() -> Self {
Self {
network: NetworkConfig {
rpc_url: "https://soroban-testnet.stellar.org".to_string(),
passphrase: "Test SDF Network ; September 2015".to_string(),
},
decode: DecodeConfig {
max_depth: 32,
allow_fallback_hex: true,
},
storage: StorageConfig::default(),
}
}
}
impl DevKitConfig {
pub fn from_toml(content: &str) -> Result<Self, toml::de::Error> {
toml::from_str(content)
}
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
let path = path.as_ref();
if path.exists() {
let content = fs::read_to_string(path)?;
let config = Self::from_toml(&content)?;
Ok(config)
} else {
Ok(Self::default())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let default_config = DevKitConfig::default();
assert_eq!(default_config.decode.max_depth, 32);
assert!(default_config.decode.allow_fallback_hex);
assert_eq!(default_config.storage.max_entries, 200);
assert_eq!(default_config.storage.ttl_warning_days, 30);
}
#[test]
fn test_parse_valid_toml() {
let toml_data = r#"
[network]
rpc_url = "http://localhost:8000"
passphrase = "Standalone Network"
[decode]
max_depth = 16
allow_fallback_hex = false
[storage]
max_entries = 100
ttl_warning_days = 7
"#;
let parsed = DevKitConfig::from_toml(toml_data).unwrap();
assert_eq!(parsed.network.rpc_url, "http://localhost:8000");
assert_eq!(parsed.decode.max_depth, 16);
assert!(!parsed.decode.allow_fallback_hex);
assert_eq!(parsed.storage.max_entries, 100);
assert_eq!(parsed.storage.ttl_warning_days, 7);
}
#[test]
fn test_old_toml_without_storage() {
let toml_data = r#"
[network]
rpc_url = "http://localhost:8000"
passphrase = "Standalone Network"
[decode]
max_depth = 16
allow_fallback_hex = false
"#;
let parsed = DevKitConfig::from_toml(toml_data).unwrap();
assert_eq!(parsed.storage.max_entries, 200);
}
}