alma 0.1.1

A Bevy-native modal text editor with Vim-style navigation.
Documentation
//! Redacted guest decode errors.

use super::GuestDecodeField;
use crate::plugin::{PluginCapabilityShape, WorkspacePathError};
use std::fmt::{Display, Formatter};

/// Guest decode failure with redacted payload data.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum GuestDecodeError {
    /// Byte field exceeded its configured cap.
    PayloadTooLarge {
        /// Field name.
        field: GuestDecodeField,
        /// Observed bytes.
        size: u64,
        /// Accepted bytes.
        max: u64,
    },
    /// String field was not UTF-8.
    MalformedUtf8 {
        /// Field name.
        field: GuestDecodeField,
        /// Observed bytes.
        size: u64,
    },
    /// Count exceeded runtime or host limits.
    CountTooLarge {
        /// Field name.
        field: GuestDecodeField,
        /// Observed value.
        value: u64,
        /// Accepted value.
        max: u64,
    },
    /// Range start was after range end.
    ReversedRange {
        /// Field name.
        field: GuestDecodeField,
        /// Start byte index.
        start: u64,
        /// End byte index.
        end: u64,
    },
    /// Range exceeded runtime or host limits.
    RangeTooLarge {
        /// Field name.
        field: GuestDecodeField,
        /// Start byte index.
        start: u64,
        /// End byte index.
        end: u64,
        /// Accepted end byte index.
        max: u64,
    },
    /// Capability name is not in the current WIT vocabulary.
    UnknownCapability {
        /// Capability-name byte length.
        byte_len: usize,
    },
    /// Workspace capability omitted its path.
    MissingWorkspacePath {
        /// Capability shape.
        capability: PluginCapabilityShape,
    },
    /// Scalar capability included a path field.
    UnexpectedWorkspacePath {
        /// Capability shape.
        capability: PluginCapabilityShape,
    },
    /// Workspace path failed Alma path validation.
    InvalidWorkspacePath {
        /// Field name.
        field: GuestDecodeField,
        /// Path validation error.
        source: WorkspacePathError,
    },
}

impl Display for GuestDecodeError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::PayloadTooLarge { field, size, max } => {
                write!(formatter, "guest field {field} is {size} bytes, max {max}")
            }
            Self::MalformedUtf8 { field, size } => {
                write!(formatter, "guest field {field} is not UTF-8, {size} bytes")
            }
            Self::CountTooLarge { field, value, max } => {
                write!(formatter, "guest count {field} is {value}, max {max}")
            }
            Self::ReversedRange { field, start, end } => {
                write!(
                    formatter,
                    "guest range {field} has start {start} after end {end}"
                )
            }
            Self::RangeTooLarge {
                field,
                start,
                end,
                max,
            } => write!(
                formatter,
                "guest range {field} is {start}..{end}, max end {max}"
            ),
            Self::UnknownCapability { byte_len } => {
                write!(
                    formatter,
                    "guest capability is unknown, {byte_len} name bytes"
                )
            }
            Self::MissingWorkspacePath { capability } => {
                write!(formatter, "{capability} requires a workspace path")
            }
            Self::UnexpectedWorkspacePath { capability } => {
                write!(formatter, "{capability} does not accept a workspace path")
            }
            Self::InvalidWorkspacePath { field, source } => {
                write!(
                    formatter,
                    "guest workspace path {field} is invalid: {source}"
                )
            }
        }
    }
}