use std::collections::BTreeMap;
use super::{LoadState, TokenStore};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AppFacts {
pub has_client_id: bool,
pub has_tokens: bool,
}
#[derive(Debug, Clone)]
pub struct StoreSnapshot {
pub apps: BTreeMap<String, AppFacts>,
pub default_app: String,
pub active_app: String,
pub env_client_id_present: bool,
pub load_state: LoadState,
pub store_path: String,
}
impl StoreSnapshot {
#[must_use]
pub fn new(store: &TokenStore, active_app: &str, env_client_id_present: bool) -> Self {
let apps = store
.apps
.iter()
.map(|(name, app)| {
(
name.clone(),
AppFacts {
has_client_id: !app.client_id.is_empty(),
has_tokens: app.has_tokens(),
},
)
})
.collect();
Self {
apps,
default_app: store.default_app.clone(),
active_app: store.get_active_app_name(active_app).to_string(),
env_client_id_present,
load_state: store.load_state,
store_path: store.file_path.display().to_string(),
}
}
#[must_use]
pub fn apps_with_client_id(&self) -> Vec<String> {
self.apps
.iter()
.filter(|(_, facts)| facts.has_client_id)
.map(|(name, _)| name.clone())
.collect()
}
#[must_use]
pub fn apps_ready_to_sign_in(&self) -> Vec<String> {
self.apps
.iter()
.filter(|(_, facts)| facts.has_client_id && !facts.has_tokens)
.map(|(name, _)| name.clone())
.collect()
}
#[must_use]
pub fn apps_with_tokens(&self) -> Vec<String> {
self.apps
.iter()
.filter(|(_, facts)| facts.has_tokens)
.map(|(name, _)| name.clone())
.collect()
}
#[must_use]
pub fn load_failed(&self) -> bool {
matches!(
self.load_state,
LoadState::Unreadable | LoadState::Unparseable
)
}
}