use std::sync::Arc;
use super::authority::BindingAuthority;
use super::capacity::OwnerIdentity;
use super::process_runtime::{ProcessIndexRuntime, SurfaceKind};
use super::registry::{
ProjectKey, ProjectRegistry, RegistryRefusal, RootProtection, SlotIdentity, StatePlacement,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdmissionPlan {
surface: SurfaceKind,
key: ProjectKey,
placement: StatePlacement,
owner: OwnerIdentity,
protection: RootProtection,
authorized: bool,
touches_source_root: bool,
}
impl AdmissionPlan {
pub fn surface(&self) -> SurfaceKind {
self.surface
}
pub fn key(&self) -> &ProjectKey {
&self.key
}
pub fn placement(&self) -> StatePlacement {
self.placement
}
pub fn owner(&self) -> OwnerIdentity {
self.owner
}
pub fn protection(&self) -> RootProtection {
self.protection
}
pub fn authorized(&self) -> bool {
self.authorized
}
pub fn touches_source_root(&self) -> bool {
self.touches_source_root
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdapterRefusal {
SurfaceNotAttached {
surface: SurfaceKind,
},
Registry(RegistryRefusal),
}
pub fn plan_admission(
runtime: &Arc<ProcessIndexRuntime>,
surface: SurfaceKind,
key: ProjectKey,
protection: RootProtection,
authorized: bool,
requested: StatePlacement,
) -> Result<AdmissionPlan, AdapterRefusal> {
let owner = runtime
.owner_for(surface)
.ok_or(AdapterRefusal::SurfaceNotAttached { surface })?;
let placement = match (protection, authorized, requested) {
(RootProtection::Protected, false, _) => {
return Err(AdapterRefusal::Registry(
RegistryRefusal::ProtectedWithoutAuthorization,
));
}
(RootProtection::Protected, true, StatePlacement::ProjectLocal) => {
return Err(AdapterRefusal::Registry(
RegistryRefusal::ProtectedWithoutAuthorization,
));
}
(RootProtection::Protected, true, chosen) => chosen,
(RootProtection::Normal, _, chosen) => chosen,
};
Ok(AdmissionPlan {
surface,
key,
placement,
owner,
protection,
authorized,
touches_source_root: placement == StatePlacement::ProjectLocal,
})
}
pub fn execute_plan(
registry: &Arc<ProjectRegistry>,
plan: &AdmissionPlan,
binding: BindingAuthority,
) -> Result<(SlotIdentity, OwnerIdentity), AdapterRefusal> {
let slot = registry
.admit(
plan.key().clone(),
binding,
plan.protection(),
plan.authorized(),
plan.placement(),
)
.map_err(AdapterRefusal::Registry)?;
Ok((slot, plan.owner()))
}