From 8b0af49817a96ceec4ab8c585872c084e7aae28e Mon Sep 17 00:00:00 2001 From: syui Date: Sun, 1 Mar 2026 15:50:53 +0900 Subject: [PATCH] fix cfg path --- src/core/config.rs | 60 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/core/config.rs b/src/core/config.rs index 4e1c8ec..9185e78 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -1,17 +1,28 @@ -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use serde_json::json; use std::fs; use std::path::PathBuf; use chrono::Utc; -#[derive(Debug, Serialize, Deserialize)] pub struct Config { pub path: Option, pub did: Option, pub handle: Option, } +#[derive(Debug, Deserialize)] +struct ConfigFile { + bot: Option, +} + +#[derive(Debug, Deserialize)] +struct BotConfig { + did: Option, + handle: Option, + path: Option, +} + pub fn config_file() -> PathBuf { dirs::config_dir() .unwrap_or_else(|| PathBuf::from(".")) @@ -19,15 +30,30 @@ pub fn config_file() -> PathBuf { .join("config.json") } -pub fn load() -> Config { - let path = config_file(); - match fs::read_to_string(&path) { - Ok(content) => serde_json::from_str(&content).unwrap_or(defaults()), - Err(_) => defaults(), +fn expand_path(p: &str) -> PathBuf { + if p.starts_with("~/") { + dirs::home_dir() + .unwrap_or_else(|| PathBuf::from(".")) + .join(&p[2..]) + } else { + PathBuf::from(p) } } -fn defaults() -> Config { +pub fn load() -> Config { + let cfg_path = config_file(); + if let Ok(content) = fs::read_to_string(&cfg_path) { + if let Ok(file) = serde_json::from_str::(&content) { + if let Some(bot) = file.bot { + return Config { + path: bot.path, + did: bot.did, + handle: bot.handle, + }; + } + } + } + Config { path: None, did: None, @@ -42,9 +68,11 @@ pub fn init() { let _ = fs::create_dir_all(parent); } let default_cfg = json!({ - "path": null, - "did": null, - "handle": null + "bot": { + "did": null, + "handle": null, + "path": null + } }); let _ = fs::write(&cfg_path, serde_json::to_string_pretty(&default_cfg).unwrap()); } @@ -80,15 +108,7 @@ pub fn init() { pub fn base_dir(cfg: &Config) -> PathBuf { match &cfg.path { - Some(p) => { - if p.starts_with("~/") { - dirs::home_dir() - .unwrap_or_else(|| PathBuf::from(".")) - .join(&p[2..]) - } else { - PathBuf::from(p) - } - } + Some(p) => expand_path(p), None => dirs::config_dir() .unwrap_or_else(|| PathBuf::from(".")) .join("ai.syui.gpt"),