use super::proposal::{PluginEffectTargetShape, PluginStatusMessageShape};
use crate::{
buffer::BufferEditShape,
ecs::events::{
edit::{PluginBufferProposalCapability, RevisionGuardedBufferEditRequested},
status::StatusMessageRequested,
},
plugin::PluginIdentity,
text_stream::TextRevision,
};
use std::fmt::{Debug, Formatter};
#[derive(Debug, Eq, PartialEq)]
pub struct PluginEffectDiscardReport {
identity: PluginIdentity,
discarded_effects: usize,
}
impl PluginEffectDiscardReport {
#[must_use]
pub(in crate::plugin::host::effect) const fn new(
identity: PluginIdentity,
discarded_effects: usize,
) -> Self {
Self {
identity,
discarded_effects,
}
}
#[must_use]
pub fn identity(&self) -> &str {
self.identity_proof().as_str()
}
#[must_use]
pub const fn identity_proof(&self) -> &PluginIdentity {
&self.identity
}
#[must_use]
pub const fn discarded_effects(&self) -> usize {
self.discarded_effects
}
}
#[derive(Eq, PartialEq)]
pub struct DrainedPluginEffectReport {
identity: PluginIdentity,
buffer_edits: Vec<RevisionGuardedBufferEditRequested>,
status_messages: Vec<StatusMessageRequested>,
}
impl DrainedPluginEffectReport {
#[must_use]
pub(in crate::plugin::host::effect) const fn from_requests(
identity: PluginIdentity,
buffer_edits: Vec<RevisionGuardedBufferEditRequested>,
status_messages: Vec<StatusMessageRequested>,
) -> Self {
Self {
identity,
buffer_edits,
status_messages,
}
}
#[must_use]
pub fn identity(&self) -> &str {
self.identity_proof().as_str()
}
#[must_use]
pub const fn identity_proof(&self) -> &PluginIdentity {
&self.identity
}
#[must_use]
pub fn buffer_edits(&self) -> &[RevisionGuardedBufferEditRequested] {
&self.buffer_edits
}
#[must_use]
pub fn status_messages(&self) -> &[StatusMessageRequested] {
&self.status_messages
}
#[must_use]
pub fn into_requests(self) -> DrainedPluginEffectRequests {
DrainedPluginEffectRequests {
identity: self.identity,
buffer_edits: self.buffer_edits,
status_messages: self.status_messages,
}
}
}
impl Debug for DrainedPluginEffectReport {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("DrainedPluginEffectReport")
.field("identity", &self.identity)
.field("buffer_edit_count", &self.buffer_edits.len())
.field(
"buffer_edit_shapes",
&self
.buffer_edits
.iter()
.map(DrainedPluginBufferEditShape::from)
.collect::<Vec<_>>(),
)
.field("status_message_count", &self.status_messages.len())
.field(
"status_message_shapes",
&self
.status_messages
.iter()
.map(DrainedPluginStatusMessageShape::from)
.collect::<Vec<_>>(),
)
.finish()
}
}
#[derive(Eq, PartialEq)]
pub struct DrainedPluginEffectRequests {
pub identity: PluginIdentity,
pub buffer_edits: Vec<RevisionGuardedBufferEditRequested>,
pub status_messages: Vec<StatusMessageRequested>,
}
impl DrainedPluginEffectRequests {
#[must_use]
pub fn identity(&self) -> &str {
self.identity_proof().as_str()
}
#[must_use]
pub const fn identity_proof(&self) -> &PluginIdentity {
&self.identity
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct DrainedPluginBufferEditShape {
target: PluginEffectTargetShape,
source_identity: PluginIdentity,
capability: PluginBufferProposalCapability,
base_revision: TextRevision,
edit: BufferEditShape,
}
impl From<&RevisionGuardedBufferEditRequested> for DrainedPluginBufferEditShape {
fn from(request: &RevisionGuardedBufferEditRequested) -> Self {
Self {
target: PluginEffectTargetShape::Buffer,
source_identity: request.provenance.source_identity_proof().clone(),
capability: request.provenance.capability(),
base_revision: request.provenance.base_revision(),
edit: BufferEditShape::from_edit(&request.edit),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct DrainedPluginStatusMessageShape {
target: PluginEffectTargetShape,
status: PluginStatusMessageShape,
}
impl From<&StatusMessageRequested> for DrainedPluginStatusMessageShape {
fn from(request: &StatusMessageRequested) -> Self {
Self {
target: PluginEffectTargetShape::View,
status: PluginStatusMessageShape::from(&request.message),
}
}
}