use super::super::{
PluginHandleStore, PluginHandleStoreSnapshot, PluginHostContext, context::PluginHostAuthority,
};
use super::error::PluginHostStateMismatch;
use crate::plugin::{
AuthorizedWorkspaceReadAccess, AuthorizedWorkspaceWriteAccess, PluginAuthorizationError,
PluginCapabilityRef, PluginIdentity, WorkspacePathRef,
};
use std::fmt::{Debug, Formatter};
#[derive(Clone, Eq, PartialEq)]
pub(in crate::plugin) struct PluginHostImportSnapshot {
authority: PluginHostAuthority,
handles: PluginHandleStoreSnapshot,
}
impl PluginHostImportSnapshot {
pub(in crate::plugin) fn new(
host: &PluginHostContext,
handles: &PluginHandleStore,
) -> Result<Self, PluginHostStateMismatch> {
if host.identity_proof() != handles.identity() {
return Err(PluginHostStateMismatch::new(
host.identity_proof().clone(),
handles.identity().clone(),
));
}
Ok(Self {
authority: host.authority().clone(),
handles: handles.snapshot(),
})
}
#[must_use]
pub(in crate::plugin) const fn identity_proof(&self) -> &PluginIdentity {
self.authority.identity_proof()
}
pub(super) fn authorize(
&self,
capability: PluginCapabilityRef<'_>,
) -> Result<(), PluginAuthorizationError> {
self.authority.authorize(capability)
}
#[must_use]
pub(super) const fn handles(&self) -> &PluginHandleStoreSnapshot {
&self.handles
}
pub(super) fn authorize_workspace_read(
&self,
path: WorkspacePathRef<'_>,
) -> Result<AuthorizedWorkspaceReadAccess, PluginAuthorizationError> {
self.authority.authorize_workspace_observe(path)
}
pub(super) fn authorize_workspace_write(
&self,
path: WorkspacePathRef<'_>,
) -> Result<AuthorizedWorkspaceWriteAccess, PluginAuthorizationError> {
self.authority.authorize_workspace_artifact_write(path)
}
}
impl Debug for PluginHostImportSnapshot {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PluginHostImportSnapshot")
.field("identity", self.identity_proof())
.field("capabilities", self.authority.capabilities())
.field("handles", &self.handles)
.finish()
}
}