From bba94e244c2dacaaeb1c4a7f4c6f6c29c9364e05 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 24 Jul 2026 16:36:44 +0200 Subject: [PATCH] Config --- Cargo.lock | 32 ++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/engine/config.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/engine/main.rs | 1 + 4 files changed, 89 insertions(+) create mode 100644 src/engine/config.rs diff --git a/Cargo.lock b/Cargo.lock index a60d9a7..fc34e9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3888,8 +3888,10 @@ dependencies = [ "pulse-ui", "pulse-wire", "rand 0.8.7", + "serde", "serde_json", "tokio", + "toml", ] [[package]] @@ -4729,6 +4731,15 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_spanned" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +dependencies = [ + "serde_core", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -5240,6 +5251,21 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c96ecdfa941c8fc4fcaed14f99ada8ebed502eef533015095a07e3301d4c3c" +dependencies = [ + "indexmap 2.14.0", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + [[package]] name = "toml_datetime" version = "1.1.1+spec-1.1.0" @@ -5270,6 +5296,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "toml_writer" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" + [[package]] name = "tonic" version = "0.14.6" diff --git a/Cargo.toml b/Cargo.toml index 22ba238..f70095e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,8 @@ crossterm = { workspace = true } hypersdk = "0.2.14" serde_json = "1" rand = "0.8.7" +toml = "1.1.3" +serde = { version = "1.0.229", features = ["serde_derive"] } [workspace] members = ["pulse-macros", "pulse-ui", "pulse-wire"] diff --git a/src/engine/config.rs b/src/engine/config.rs new file mode 100644 index 0000000..3380131 --- /dev/null +++ b/src/engine/config.rs @@ -0,0 +1,54 @@ +use std::path::PathBuf; + +pub fn home_dir() -> tokio::io::Result { + std::env::home_dir().ok_or_else(|| { + tokio::io::Error::new(std::io::ErrorKind::NotFound, "Unable to get home directory") + }) +} + +pub fn pulse_directory() -> tokio::io::Result { + Ok(home_dir()?.join(".config/pulse-trader")) +} + +pub fn pulse_config_directory() -> tokio::io::Result { + Ok(home_dir()?.join(".config/pulse-trader/config.toml")) +} + +#[derive(Debug, Default, serde::Serialize, serde::Deserialize)] +pub struct WatchList { + symbols: Vec, +} + +#[derive(Debug, Default, serde::Serialize, serde::Deserialize)] +pub struct Config { + watchlist: WatchList, +} + +impl Config { + pub async fn new() -> tokio::io::Result { + let path = pulse_config_directory()?; + + 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 { + 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 { + toml::to_string(self) + .map_err(|v| tokio::io::Error::new(std::io::ErrorKind::Other, v.to_string())) + } +} diff --git a/src/engine/main.rs b/src/engine/main.rs index bb7f06f..b1a7454 100644 --- a/src/engine/main.rs +++ b/src/engine/main.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod engine; pub mod fetch; pub mod terminal;