use std::sync::Arc;
use adk_core::{
AdkError, ApprovalPolicy, ApprovalService, ApproverSelector, Result, StoreApprovalService,
};
use adk_kernel::Partitions;
use adk_kernel::stores::KernelStores;
fn store(error: impl std::fmt::Display) -> AdkError {
AdkError::config(format!("the site's stores could not open: {error}"))
}
#[derive(Clone)]
pub struct SiteStores {
pub kernel: KernelStores,
pub approvals: Arc<dyn ApprovalService>,
pub partitions: Partitions,
pub rules: Arc<adk_harness::SqliteStandingRuleStore>,
pub governance: adk_core::GovernanceControl,
}
impl std::fmt::Debug for SiteStores {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SiteStores").field("kernel", &self.kernel).finish_non_exhaustive()
}
}
impl SiteStores {
pub async fn open(url: &str, owner: &str) -> Result<Self> {
Self::open_with(url, owner, Vec::new()).await
}
pub async fn open_with(
url: &str,
owner: &str,
notifiers: Vec<Arc<dyn adk_core::ApprovalNotifier>>,
) -> Result<Self> {
let pool = sqlx::sqlite::SqlitePoolOptions::new()
.max_connections(8)
.connect(url)
.await
.map_err(store)?;
let kernel =
KernelStores::from_sqlite_pool(pool.clone()).await.map_err(store)?.with_label(url);
let mut service = StoreApprovalService::new(kernel.approvals.clone())
.with_router(ApprovalPolicy::new([ApproverSelector::human(owner)]))
.with_directory(Arc::new(adk_kernel::charter::CharterDirectory::new(
kernel.charter.charters.clone(),
)));
for notifier in notifiers {
service = service.with_notifier(notifier);
}
let rules = Arc::new(adk_harness::SqliteStandingRuleStore::new(pool));
rules.migrate().await?;
Ok(Self {
approvals: Arc::new(service),
partitions: Partitions::new(kernel.partitions.clone()),
governance: adk_kernel::KernelConfig::mirrored_governance(kernel.governance.clone()),
kernel,
rules,
})
}
}