alma 0.1.1

A Bevy-native modal text editor with Vim-style navigation.
Documentation
//! Per-update host authority snapshots.

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};

/// Host authority captured for one guest update.
#[derive(Clone, Eq, PartialEq)]
pub(in crate::plugin) struct PluginHostImportSnapshot {
    /// Effective host-import authority captured for this update.
    authority: PluginHostAuthority,
    /// Live handles captured for this update.
    handles: PluginHandleStoreSnapshot,
}

impl PluginHostImportSnapshot {
    /// Captures host authority and live handles for one guest update.
    ///
    /// # Errors
    ///
    /// Returns [`PluginHostStateMismatch`] when the host context and handle store belong to
    /// different plugin identities.
    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(),
        })
    }

    /// Returns the captured identity proof.
    #[must_use]
    pub(in crate::plugin) const fn identity_proof(&self) -> &PluginIdentity {
        self.authority.identity_proof()
    }

    /// Authorizes a capability against the captured grant set.
    pub(super) fn authorize(
        &self,
        capability: PluginCapabilityRef<'_>,
    ) -> Result<(), PluginAuthorizationError> {
        self.authority.authorize(capability)
    }

    /// Returns the handles captured for this update.
    #[must_use]
    pub(super) const fn handles(&self) -> &PluginHandleStoreSnapshot {
        &self.handles
    }

    /// Authorizes a validated workspace I/O request.
    pub(super) fn authorize_workspace_read(
        &self,
        path: WorkspacePathRef<'_>,
    ) -> Result<AuthorizedWorkspaceReadAccess, PluginAuthorizationError> {
        self.authority.authorize_workspace_observe(path)
    }

    /// Authorizes a validated workspace write request.
    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()
    }
}