Files
pulse-trader/src/engine/store/config.rs
T
2026-07-30 02:50:22 +02:00

56 lines
1.5 KiB
Rust

use hypersdk::hypercore::CandleInterval;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Config {
pub watchlist: Vec<String>,
pub strategy: String,
pub cooldown: CandleInterval,
}
impl Default for Config {
fn default() -> Self {
Self {
watchlist: vec!["BTC".to_string(), "SOL".to_string(), "ETH".to_string()],
strategy: String::new(),
cooldown: CandleInterval::ThirtyMinutes,
}
}
}
impl Config {
pub async fn new() -> tokio::io::Result<Self> {
let path = crate::store::pulse_config_file()?;
if !path.exists() {
let default = Self::default();
tokio::fs::create_dir_all(path.parent().unwrap()).await?;
tokio::fs::write(&path, default.to_string()?).await?;
return Ok(default);
}
let output = tokio::fs::read_to_string(path).await?;
Self::from_str(&output)
}
pub async fn save(&self) -> tokio::io::Result<()> {
let path = crate::store::pulse_config_file()?;
tokio::fs::write(path, self.to_string()?).await?;
Ok(())
}
pub fn from_str(s: &str) -> tokio::io::Result<Self> {
toml::from_str(s)
.map_err(|v| tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string()))
}
pub fn to_string(&self) -> tokio::io::Result<String> {
toml::to_string(self)
.map_err(|v| tokio::io::Error::new(std::io::ErrorKind::Other, v.to_string()))
}
}