mirror of
https://github.com/hpd840321/craftlabs-authorization-sdk.git
synced 2026-06-09 10:00:30 +08:00
37 lines
798 B
Rust
37 lines
798 B
Rust
use std::fs;
|
|
use std::path::Path;
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize)]
|
|
pub struct Config {
|
|
pub api_base_url: String,
|
|
pub sn: Option<String>,
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load(path: &Path) -> Self {
|
|
if path.exists() {
|
|
fs::read_to_string(path)
|
|
.ok()
|
|
.and_then(|s| serde_json::from_str(&s).ok())
|
|
.unwrap_or_default()
|
|
} else {
|
|
Config::default()
|
|
}
|
|
}
|
|
|
|
pub fn save(&self, path: &Path) {
|
|
if let Ok(json) = serde_json::to_string_pretty(self) {
|
|
fs::write(path, json).ok();
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Config {
|
|
api_base_url: "http://localhost:8080".to_string(),
|
|
sn: None,
|
|
}
|
|
}
|
|
}
|