use super::PluginWorkspaceObserveOutcomeShape;
use crate::plugin::{PluginIdentity, PluginWorkspaceObserveOutcome};
use std::{
fmt::{Debug, Display, Formatter},
num::NonZeroU64,
};
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PluginWorkspaceObserveTaskId(NonZeroU64);
impl PluginWorkspaceObserveTaskId {
pub const fn try_new(value: u64) -> Result<Self, PluginWorkspaceObserveTaskIdError> {
match NonZeroU64::new(value) {
Some(value) => Ok(Self(value)),
None => Err(PluginWorkspaceObserveTaskIdError::Zero),
}
}
#[must_use]
pub const fn get(self) -> u64 {
self.0.get()
}
pub(in crate::plugin::host::workspace_io) const fn first() -> Self {
Self(NonZeroU64::MIN)
}
pub(in crate::plugin::host::workspace_io) const fn next(self) -> Option<Self> {
match self.0.get().checked_add(1) {
Some(next) => match NonZeroU64::new(next) {
Some(next) => Some(Self(next)),
None => None,
},
None => None,
}
}
}
impl Debug for PluginWorkspaceObserveTaskId {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter
.debug_tuple("PluginWorkspaceObserveTaskId")
.field(&self.get())
.finish()
}
}
impl Display for PluginWorkspaceObserveTaskId {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}", self.get())
}
}
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct PluginWorkspaceObserveTaskHandle {
identity: PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
}
impl PluginWorkspaceObserveTaskHandle {
#[must_use]
pub(in crate::plugin) const fn new(
identity: PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
) -> Self {
Self { identity, task_id }
}
#[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 task_id(&self) -> PluginWorkspaceObserveTaskId {
self.task_id
}
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn lookup(
&self,
) -> PluginWorkspaceObserveTaskLookup<'_> {
PluginWorkspaceObserveTaskLookup::new(self.identity_proof(), self.task_id)
}
}
impl Display for PluginWorkspaceObserveTaskHandle {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
let identity = self.identity();
write!(formatter, "identity={identity:?} task_id={}", self.task_id)
}
}
impl Debug for PluginWorkspaceObserveTaskHandle {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, formatter)
}
}
#[derive(Clone, Eq, PartialEq)]
pub(in crate::plugin::host::workspace_io) struct PluginWorkspaceObserveTaskKey {
handle: PluginWorkspaceObserveTaskHandle,
}
impl PluginWorkspaceObserveTaskKey {
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn new(
identity: PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
) -> Self {
Self {
handle: PluginWorkspaceObserveTaskHandle::new(identity, task_id),
}
}
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn identity_proof(&self) -> &PluginIdentity {
self.handle.identity_proof()
}
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn task_id(
&self,
) -> PluginWorkspaceObserveTaskId {
self.handle.task_id()
}
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn handle(
&self,
) -> &PluginWorkspaceObserveTaskHandle {
&self.handle
}
pub(in crate::plugin::host::workspace_io) fn matches_lookup(
&self,
lookup: PluginWorkspaceObserveTaskLookup<'_>,
) -> bool {
lookup.matches_handle(&self.handle)
}
}
impl Debug for PluginWorkspaceObserveTaskKey {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PluginWorkspaceObserveTaskKey")
.field("handle", &self.handle)
.finish()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum PluginWorkspaceObserveTaskIdError {
#[error("plugin workspace observe task id must be non-zero")]
Zero,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::plugin::host::workspace_io) struct PluginWorkspaceObserveTaskLookup<'identity> {
identity: &'identity PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
}
impl<'identity> PluginWorkspaceObserveTaskLookup<'identity> {
#[must_use]
pub(in crate::plugin::host::workspace_io) const fn new(
identity: &'identity PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
) -> Self {
Self { identity, task_id }
}
pub(in crate::plugin::host::workspace_io) fn matches_task(
self,
identity: &PluginIdentity,
task_id: PluginWorkspaceObserveTaskId,
) -> bool {
identity == self.identity && task_id == self.task_id
}
pub(in crate::plugin::host::workspace_io) fn matches_key(
self,
key: &PluginWorkspaceObserveTaskKey,
) -> bool {
key.matches_lookup(self)
}
pub(in crate::plugin::host::workspace_io) fn matches_handle(
self,
handle: &PluginWorkspaceObserveTaskHandle,
) -> bool {
self.matches_task(handle.identity_proof(), handle.task_id())
}
pub(in crate::plugin::host::workspace_io) fn matches_completion(
self,
completion: &PluginWorkspaceObserveTaskCompletion,
) -> bool {
self.matches_key(&completion.key)
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum PluginWorkspaceObserveTaskState {
Pending,
Completed,
Unknown,
}
impl PluginWorkspaceObserveTaskState {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Pending => "pending",
Self::Completed => "completed",
Self::Unknown => "unknown",
}
}
#[must_use]
pub const fn is_pending(self) -> bool {
matches!(self, Self::Pending)
}
#[must_use]
pub const fn is_completed(self) -> bool {
matches!(self, Self::Completed)
}
#[must_use]
pub const fn is_unknown(self) -> bool {
matches!(self, Self::Unknown)
}
}
impl Display for PluginWorkspaceObserveTaskState {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
impl Debug for PluginWorkspaceObserveTaskState {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum PluginWorkspaceObserveTaskResultShape {
Pending,
Completed {
outcome: PluginWorkspaceObserveOutcomeShape,
},
Unknown,
}
impl PluginWorkspaceObserveTaskResultShape {
#[must_use]
pub const fn state(self) -> PluginWorkspaceObserveTaskState {
match self {
Self::Pending => PluginWorkspaceObserveTaskState::Pending,
Self::Completed { .. } => PluginWorkspaceObserveTaskState::Completed,
Self::Unknown => PluginWorkspaceObserveTaskState::Unknown,
}
}
#[must_use]
pub const fn outcome_shape(self) -> Option<PluginWorkspaceObserveOutcomeShape> {
match self {
Self::Completed { outcome } => Some(outcome),
Self::Pending | Self::Unknown => None,
}
}
}
impl Display for PluginWorkspaceObserveTaskResultShape {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Pending | Self::Unknown => write!(formatter, "{}", self.state()),
Self::Completed { outcome } => write!(formatter, "completed outcome={outcome}"),
}
}
}
impl Debug for PluginWorkspaceObserveTaskResultShape {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, formatter)
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum PluginWorkspaceObserveTaskPoll {
Pending,
Completed {
outcome: PluginWorkspaceObserveOutcomeShape,
},
Unknown,
}
impl PluginWorkspaceObserveTaskPoll {
#[must_use]
pub const fn state(&self) -> PluginWorkspaceObserveTaskState {
self.shape().state()
}
#[must_use]
pub const fn shape(&self) -> PluginWorkspaceObserveTaskResultShape {
match self {
Self::Pending => PluginWorkspaceObserveTaskResultShape::Pending,
Self::Completed { outcome } => {
PluginWorkspaceObserveTaskResultShape::Completed { outcome: *outcome }
}
Self::Unknown => PluginWorkspaceObserveTaskResultShape::Unknown,
}
}
#[must_use]
pub const fn is_pending(&self) -> bool {
self.state().is_pending()
}
#[must_use]
pub const fn is_completed(&self) -> bool {
self.state().is_completed()
}
#[must_use]
pub const fn is_unknown(&self) -> bool {
self.state().is_unknown()
}
#[must_use]
pub const fn outcome_shape(&self) -> Option<PluginWorkspaceObserveOutcomeShape> {
match self {
Self::Completed { outcome } => Some(*outcome),
Self::Pending | Self::Unknown => None,
}
}
}
impl Debug for PluginWorkspaceObserveTaskPoll {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.shape(), formatter)
}
}
impl Display for PluginWorkspaceObserveTaskPoll {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.shape(), formatter)
}
}
#[derive(Clone, Eq, PartialEq)]
pub enum PluginWorkspaceObserveTaskTake {
Pending,
Completed(PluginWorkspaceObserveOutcome),
Unknown,
}
impl PluginWorkspaceObserveTaskTake {
#[must_use]
pub const fn state(&self) -> PluginWorkspaceObserveTaskState {
self.shape().state()
}
#[must_use]
pub const fn shape(&self) -> PluginWorkspaceObserveTaskResultShape {
match self {
Self::Pending => PluginWorkspaceObserveTaskResultShape::Pending,
Self::Completed(outcome) => PluginWorkspaceObserveTaskResultShape::Completed {
outcome: outcome.shape(),
},
Self::Unknown => PluginWorkspaceObserveTaskResultShape::Unknown,
}
}
#[must_use]
pub const fn is_pending(&self) -> bool {
self.state().is_pending()
}
#[must_use]
pub const fn is_completed(&self) -> bool {
self.state().is_completed()
}
#[must_use]
pub const fn is_unknown(&self) -> bool {
self.state().is_unknown()
}
#[must_use]
pub const fn outcome(&self) -> Option<&PluginWorkspaceObserveOutcome> {
match self {
Self::Completed(outcome) => Some(outcome),
Self::Pending | Self::Unknown => None,
}
}
#[must_use]
pub fn into_outcome(self) -> Option<PluginWorkspaceObserveOutcome> {
match self {
Self::Completed(outcome) => Some(outcome),
Self::Pending | Self::Unknown => None,
}
}
}
impl Debug for PluginWorkspaceObserveTaskTake {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.shape(), formatter)
}
}
impl Display for PluginWorkspaceObserveTaskTake {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.shape(), formatter)
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct PluginWorkspaceObserveTaskCompletionShape {
handle: PluginWorkspaceObserveTaskHandle,
outcome_shape: PluginWorkspaceObserveOutcomeShape,
}
impl PluginWorkspaceObserveTaskCompletionShape {
#[must_use]
pub(in crate::plugin) const fn new(
handle: PluginWorkspaceObserveTaskHandle,
outcome_shape: PluginWorkspaceObserveOutcomeShape,
) -> Self {
Self {
handle,
outcome_shape,
}
}
#[must_use]
pub fn identity(&self) -> &str {
self.identity_proof().as_str()
}
#[must_use]
pub const fn identity_proof(&self) -> &PluginIdentity {
self.handle.identity_proof()
}
#[must_use]
pub const fn task_id(&self) -> PluginWorkspaceObserveTaskId {
self.handle.task_id()
}
#[must_use]
pub const fn handle(&self) -> &PluginWorkspaceObserveTaskHandle {
&self.handle
}
#[must_use]
pub const fn outcome_shape(&self) -> PluginWorkspaceObserveOutcomeShape {
self.outcome_shape
}
#[must_use]
pub const fn is_ok(&self) -> bool {
self.outcome_shape.is_ok()
}
}
impl Display for PluginWorkspaceObserveTaskCompletionShape {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{} outcome={}", self.handle, self.outcome_shape)
}
}
impl Debug for PluginWorkspaceObserveTaskCompletionShape {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, formatter)
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct PluginWorkspaceObserveTaskCompletion {
key: PluginWorkspaceObserveTaskKey,
outcome: PluginWorkspaceObserveOutcome,
}
impl PluginWorkspaceObserveTaskCompletion {
pub(in crate::plugin::host::workspace_io) const fn new(
key: PluginWorkspaceObserveTaskKey,
outcome: PluginWorkspaceObserveOutcome,
) -> Self {
Self { key, outcome }
}
#[must_use]
pub fn identity(&self) -> &str {
self.identity_proof().as_str()
}
#[must_use]
pub const fn identity_proof(&self) -> &PluginIdentity {
self.key.identity_proof()
}
#[must_use]
pub const fn task_id(&self) -> PluginWorkspaceObserveTaskId {
self.key.task_id()
}
#[must_use]
pub const fn handle(&self) -> &PluginWorkspaceObserveTaskHandle {
self.key.handle()
}
#[must_use]
pub const fn outcome(&self) -> &PluginWorkspaceObserveOutcome {
&self.outcome
}
#[must_use]
pub fn shape(&self) -> PluginWorkspaceObserveTaskCompletionShape {
PluginWorkspaceObserveTaskCompletionShape::new(self.handle().clone(), self.outcome.shape())
}
#[must_use]
pub fn into_outcome(self) -> PluginWorkspaceObserveOutcome {
self.outcome
}
}
impl Debug for PluginWorkspaceObserveTaskCompletion {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("PluginWorkspaceObserveTaskCompletion")
.field("shape", &self.shape())
.finish()
}
}