alma 0.1.1

A Bevy-native modal text editor with Vim-style navigation.
Documentation
//! Workspace path decoder.

use super::{DecodedGuestString, GuestDecodeError, GuestDecodeField, GuestDecodeLimits};
use crate::plugin::{WorkspacePath, WorkspacePathRef};

/// Workspace path after UTF-8, size, and path-vocabulary checks.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DecodedWorkspacePath {
    /// Normalized plugin path vocabulary.
    value: WorkspacePath,
}

impl DecodedWorkspacePath {
    /// Decodes a guest workspace path.
    ///
    /// # Errors
    ///
    /// Returns [`GuestDecodeError`] when the path is oversized, non-UTF-8, or not normalized.
    pub fn from_bytes(
        field: GuestDecodeField,
        bytes: &[u8],
        limits: GuestDecodeLimits,
    ) -> Result<Self, GuestDecodeError> {
        let value = WorkspacePath::try_new(
            DecodedGuestString::from_bytes(field, bytes, limits)?.into_string(),
        )
        .map_err(|source| GuestDecodeError::InvalidWorkspacePath { field, source })?;
        Ok(Self { value })
    }

    /// Carries the path proof into capability checks.
    #[must_use]
    pub const fn as_ref(&self) -> WorkspacePathRef<'_> {
        self.value.as_ref()
    }

    /// Returns the normalized path string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.value.as_str()
    }
}