use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WasixCheckpointRestorePhase {
#[error("worker readiness")]
Ready,
#[error("sealed input transfer")]
Input,
#[error("execution authorization")]
Authorization,
#[error("checkpoint execution")]
Execution,
#[error("output collection")]
Output,
#[error("worker shutdown")]
Shutdown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WasixCheckpointCapturePhase {
#[error("worker readiness")]
Ready,
#[error("sealed input transfer")]
Input,
#[error("execution authorization")]
Authorization,
#[error("checkpoint execution")]
Execution,
#[error("output collection")]
Output,
#[error("worker shutdown")]
Shutdown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WasixCheckpointRestoreFailureReason {
#[error("worker protocol failure")]
Protocol,
#[error("checkpoint runtime failure")]
Runtime,
#[error("checkpoint resource limit")]
ResourceLimit,
#[error("worker process failure")]
WorkerProcess,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum WasixCheckpointCaptureFailureReason {
#[error("worker protocol failure")]
Protocol,
#[error("checkpoint runtime failure")]
Runtime,
#[error("checkpoint resource limit")]
ResourceLimit,
#[error("worker process failure")]
WorkerProcess,
}
#[derive(Clone, PartialEq, Eq)]
pub struct WasixWorkerDiagnostics {
bytes: Vec<u8>,
truncated: bool,
}
impl std::fmt::Debug for WasixWorkerDiagnostics {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("WasixWorkerDiagnostics")
.field("bytes", &self.bytes.len())
.field("truncated", &self.truncated)
.finish()
}
}
impl WasixWorkerDiagnostics {
#[cfg(feature = "wasix-checkpoint")]
pub(crate) const fn new(bytes: Vec<u8>, truncated: bool) -> Self {
Self { bytes, truncated }
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
#[must_use]
pub const fn is_truncated(&self) -> bool {
self.truncated
}
#[must_use]
pub const fn len(&self) -> usize {
self.bytes.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
}
#[derive(Clone, PartialEq, Eq, thiserror::Error)]
#[error("WASIX checkpoint restore failed during {phase}: {reason}")]
pub struct WasixCheckpointRestoreFailure {
phase: WasixCheckpointRestorePhase,
reason: WasixCheckpointRestoreFailureReason,
diagnostics: WasixWorkerDiagnostics,
exit_code: Option<i32>,
exit_signal: Option<i32>,
}
#[derive(Clone, PartialEq, Eq, thiserror::Error)]
#[error("WASIX checkpoint capture failed during {phase}: {reason}")]
pub struct WasixCheckpointCaptureFailure {
phase: WasixCheckpointCapturePhase,
reason: WasixCheckpointCaptureFailureReason,
diagnostics: WasixWorkerDiagnostics,
exit_code: Option<i32>,
exit_signal: Option<i32>,
}
impl std::fmt::Debug for WasixCheckpointRestoreFailure {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("WasixCheckpointRestoreFailure")
.field("phase", &self.phase)
.field("reason", &self.reason)
.field("diagnostics", &self.diagnostics)
.field("exit_code", &self.exit_code)
.field("exit_signal", &self.exit_signal)
.finish()
}
}
impl std::fmt::Debug for WasixCheckpointCaptureFailure {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("WasixCheckpointCaptureFailure")
.field("phase", &self.phase)
.field("reason", &self.reason)
.field("diagnostics", &self.diagnostics)
.field("exit_code", &self.exit_code)
.field("exit_signal", &self.exit_signal)
.finish()
}
}
impl WasixCheckpointRestoreFailure {
#[cfg(feature = "wasix-checkpoint")]
pub(crate) const fn new(
phase: WasixCheckpointRestorePhase,
reason: WasixCheckpointRestoreFailureReason,
diagnostics: WasixWorkerDiagnostics,
) -> Self {
Self {
phase,
reason,
diagnostics,
exit_code: None,
exit_signal: None,
}
}
#[cfg(feature = "wasix-checkpoint")]
pub(crate) const fn with_exit_status(
mut self,
exit_code: Option<i32>,
exit_signal: Option<i32>,
) -> Self {
self.exit_code = exit_code;
self.exit_signal = exit_signal;
self
}
#[must_use]
pub const fn phase(&self) -> WasixCheckpointRestorePhase {
self.phase
}
#[must_use]
pub const fn reason(&self) -> WasixCheckpointRestoreFailureReason {
self.reason
}
#[must_use]
pub const fn diagnostics(&self) -> &WasixWorkerDiagnostics {
&self.diagnostics
}
#[must_use]
pub const fn exit_code(&self) -> Option<i32> {
self.exit_code
}
#[must_use]
pub const fn exit_signal(&self) -> Option<i32> {
self.exit_signal
}
}
impl WasixCheckpointCaptureFailure {
#[cfg(feature = "wasix-checkpoint")]
pub(crate) const fn new(
phase: WasixCheckpointCapturePhase,
reason: WasixCheckpointCaptureFailureReason,
diagnostics: WasixWorkerDiagnostics,
) -> Self {
Self {
phase,
reason,
diagnostics,
exit_code: None,
exit_signal: None,
}
}
#[cfg(feature = "wasix-checkpoint")]
pub(crate) const fn with_exit_status(
mut self,
exit_code: Option<i32>,
exit_signal: Option<i32>,
) -> Self {
self.exit_code = exit_code;
self.exit_signal = exit_signal;
self
}
#[must_use]
pub const fn phase(&self) -> WasixCheckpointCapturePhase {
self.phase
}
#[must_use]
pub const fn reason(&self) -> WasixCheckpointCaptureFailureReason {
self.reason
}
#[must_use]
pub const fn diagnostics(&self) -> &WasixWorkerDiagnostics {
&self.diagnostics
}
#[must_use]
pub const fn exit_code(&self) -> Option<i32> {
self.exit_code
}
#[must_use]
pub const fn exit_signal(&self) -> Option<i32> {
self.exit_signal
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("invalid runtime configuration: {0}")]
Configuration(String),
#[error("component I/O failed: {0}")]
Io(String),
#[error("component preparation failed: {0}")]
Preparation(String),
#[error("unsupported component: {0}")]
UnsupportedComponent(String),
#[error("component execution failed: {0}")]
Execution(String),
#[error("runtime limit exceeded: {0}")]
Limit(&'static str),
#[error("component execution was cancelled")]
Cancelled,
#[error("paused invocation was evicted after its resident lifetime expired")]
IdleEvicted,
#[error("invalid invocation state: {0}")]
InvalidState(&'static str),
#[error("component execution timed out")]
Timeout,
#[error("AOT cache failed: {0}")]
Cache(String),
#[error("WASIX checkpoint failed: {0}")]
Checkpoint(String),
#[error(transparent)]
CheckpointRestore(WasixCheckpointRestoreFailure),
#[error(transparent)]
CheckpointCapture(WasixCheckpointCaptureFailure),
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self::Io(value.to_string())
}
}