50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
use hypersdk::hypercore::CandleInterval;
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
pub struct Config {
|
|
pub watchlist: Vec<String>,
|
|
pub strategy: String,
|
|
pub risk: 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(),
|
|
risk: 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 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()))
|
|
}
|
|
}
|