45 lines
1.0 KiB
Rust
45 lines
1.0 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct AuthConfig {
|
|
pub handle: String,
|
|
pub did: String,
|
|
pub access_jwt: String,
|
|
pub refresh_jwt: String,
|
|
pub pds: String,
|
|
}
|
|
|
|
pub fn config_dir() -> PathBuf {
|
|
dirs::config_dir()
|
|
.unwrap_or_else(|| PathBuf::from("."))
|
|
.join("syui")
|
|
.join("ai")
|
|
.join("log")
|
|
}
|
|
|
|
pub fn config_path() -> PathBuf {
|
|
config_dir().join("config.json")
|
|
}
|
|
|
|
pub fn load_config() -> Option<AuthConfig> {
|
|
let path = config_path();
|
|
if path.exists() {
|
|
let content = fs::read_to_string(&path).ok()?;
|
|
serde_json::from_str(&content).ok()
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn save_config(config: &AuthConfig) -> Result<(), Box<dyn std::error::Error>> {
|
|
let dir = config_dir();
|
|
fs::create_dir_all(&dir)?;
|
|
let path = config_path();
|
|
let content = serde_json::to_string_pretty(config)?;
|
|
fs::write(&path, content)?;
|
|
println!("Config saved to: {}", path.display());
|
|
Ok(())
|
|
}
|