conundrum_db 0.1.1

Be less dum.
use std::sync::Arc;

use conundrum_lang::ecosystem::db::{db_traits::async_traits::try_from_async::FromAsync, tables::DatabaseTable};
use online::tokio::check;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;

use crate::vector::models::ecosystem_data::server_state::server_state::ServerState;

/// Just like the ServerStatus, but minimal, where as that is meant to provide
/// enough information to provide the user with a full UI, this one will not
/// grow beyond this.
#[derive(Serialize, Deserialize, Clone, Debug, specta::Type)]
pub struct BackendStatus {
    pub local_client_access: bool,
    pub remote_client_access: bool,
    pub all_tables_exist: bool,
    pub any_tables_exist: bool,
    pub is_online: bool,
}

impl FromAsync<Arc<ServerState>> for BackendStatus {
    async fn from_async(input: Arc<ServerState>) -> Self
        where Self: Sized {
        let is_online = check(None).await.is_ok();
        let db = input.db.inner_arc().lock_owned().await;
        let mut any_tables_exist = false;
        for k in DatabaseTable::iter() {
            if db.open_table(k.to_string()).execute().await.is_err() {
                return Self { local_client_access: input.local_client.is_some(),
                              remote_client_access: input.remote_client.is_some(),
                              all_tables_exist: false,
                              any_tables_exist,
                              is_online };
            } else {
                any_tables_exist = true;
            }
        }
        Self { local_client_access: input.local_client.is_some(),
               remote_client_access: input.remote_client.is_some(),
               all_tables_exist: true,
               any_tables_exist,
               is_online }
    }
}