use std::path::PathBuf;
use serde_json::Value;
pub(super) struct Saved {
pub url: String,
pub token: Option<String>,
}
fn storage_path() -> PathBuf {
shared::fallback_data_dir()
.join(".glacier-storage")
.join("app.json")
}
pub(super) fn list() -> Vec<Saved> {
let Ok(texto) = std::fs::read_to_string(storage_path()) else {
return Vec::new();
};
let Ok(doc) = serde_json::from_str::<Value>(&texto) else {
return Vec::new();
};
doc.get("saved_servers")
.and_then(Value::as_array)
.map(|itens| {
itens
.iter()
.filter_map(|s| {
let url = s.get("url")?.as_str()?.trim();
if url.is_empty() {
return None;
}
Some(Saved {
url: url.to_string(),
token: s
.get("token")
.and_then(Value::as_str)
.map(str::trim)
.filter(|t| !t.is_empty())
.map(str::to_owned),
})
})
.collect()
})
.unwrap_or_default()
}
pub(super) fn token_for(url: &str) -> Option<String> {
let alvo = url.trim().trim_end_matches('/');
list()
.into_iter()
.find(|s| s.url.trim_end_matches('/') == alvo)
.and_then(|s| s.token)
}
pub(super) fn as_json() -> Value {
let itens: Vec<Value> = list()
.into_iter()
.map(|s| {
serde_json::json!({
"url": s.url,
"has_saved_token": s.token.is_some(),
})
})
.collect();
serde_json::json!({ "count": itens.len(), "servers": itens })
}