summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorNaz <ndpm13@ch-naseem.com>2025-09-15 14:50:08 +0100
committerNaz <ndpm13@ch-naseem.com>2025-09-15 15:08:15 +0100
commit8b5a71dd3a490718540a7e9d48da2a220256a628 (patch)
treeb1e029fad2a636dabcf053192ded8542e530b3d1 /src/config.rs
parentc53617755ea9f98c8c8e36901db90e19e827a483 (diff)
✨feat: dirty commit bringing basic functionality
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..97dac88
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,68 @@
+use std::path::PathBuf;
+
+use serde::{Deserialize, Serialize};
+
+use crate::{Error, Result};
+
+const STYLE: &str = include_str!("../examples/simple-gruvbox.css");
+const BOOKMARKS: &str = include_str!("../examples/bookmarks.json");
+const FAVICON: &str = include_str!("../examples/favicon.svg");
+
+#[derive(Deserialize, Serialize, Clone)]
+pub struct Config {
+ pub port: u16,
+ pub style_file: PathBuf,
+ pub bookmarks_file: PathBuf,
+ pub favicon_file: PathBuf,
+}
+
+impl Config {
+ pub fn new() -> Result<Config> {
+ let config_home = if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME") {
+ PathBuf::from(xdg_config_home).join("sbm-rs")
+ } else if let Ok(home) = std::env::var("HOME") {
+ PathBuf::from(home).join(".config/sbm-rs")
+ } else {
+ return Err(Error::ConfigNotFound);
+ };
+
+ if config_home.exists() {
+ Config::load_config(&config_home)
+ } else {
+ Config::generate_defaults(&config_home)
+ }
+ }
+ pub fn load_config(config_home: &PathBuf) -> Result<Config> {
+ let config = config_home.join("config.toml");
+
+ let config = std::fs::read_to_string(config)?;
+
+ let config: Config = toml::from_str(&config)?;
+
+ Ok(config)
+ }
+ pub fn generate_defaults(config_home: &PathBuf) -> Result<Config> {
+ std::fs::create_dir_all(&config_home)?;
+
+ let style_file = config_home.join("style.css");
+ let bookmarks_file = config_home.join("bookmarks.json");
+ let favicon_file = config_home.join("favicon.svg");
+ let config_file = config_home.join("config.toml");
+
+ std::fs::write(&style_file, &STYLE)?;
+ std::fs::write(&bookmarks_file, &BOOKMARKS)?;
+ std::fs::write(&favicon_file, &FAVICON)?;
+
+ let config = Config {
+ port: 8080,
+ style_file,
+ bookmarks_file,
+ favicon_file,
+ };
+
+ let config_file_content = toml::to_string(&config)?;
+ std::fs::write(config_file, config_file_content)?;
+
+ Ok(config)
+ }
+}