use serde::{Deserialize, Serialize};
use crate::config::AppConfig;
use crate::error::AppError;
use crate::store::KeyspaceHandle;
const REST_KEY: &str = "runtime-state:service:rest";
const DIDCOMM_KEY: &str = "runtime-state:service:didcomm";
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ServiceState {
enabled: bool,
}
pub async fn is_rest_enabled(ks: &KeyspaceHandle) -> Result<bool, AppError> {
Ok(ks
.get::<ServiceState>(REST_KEY.to_string())
.await?
.map(|s| s.enabled)
.unwrap_or(true))
}
pub async fn is_didcomm_enabled(ks: &KeyspaceHandle) -> Result<bool, AppError> {
Ok(ks
.get::<ServiceState>(DIDCOMM_KEY.to_string())
.await?
.map(|s| s.enabled)
.unwrap_or(true))
}
pub async fn set_rest_enabled(ks: &KeyspaceHandle, enabled: bool) -> Result<(), AppError> {
ks.insert(REST_KEY.to_string(), &ServiceState { enabled })
.await
}
pub async fn set_didcomm_enabled(ks: &KeyspaceHandle, enabled: bool) -> Result<(), AppError> {
ks.insert(DIDCOMM_KEY.to_string(), &ServiceState { enabled })
.await
}
pub async fn migrate_from_config(ks: &KeyspaceHandle, config: &AppConfig) -> Result<(), AppError> {
if ks
.get::<ServiceState>(REST_KEY.to_string())
.await?
.is_none()
{
set_rest_enabled(ks, config.services.rest).await?;
}
if ks
.get::<ServiceState>(DIDCOMM_KEY.to_string())
.await?
.is_none()
{
set_didcomm_enabled(ks, config.services.didcomm).await?;
}
Ok(())
}