scosh-core 0.1.0

Portable session state and terminal event core for scosh
Documentation
//! Bounded, ordered input values for the host-to-session path.

use std::fmt;

use crate::{CoreError, types::MAX_INPUT_BYTES};

/// Opaque identifier for one input submission.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct InputId(pub(crate) u64);

impl fmt::Debug for InputId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("InputId(<opaque>)")
    }
}

impl InputId {
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Input bytes emitted by the core for the host's transport adapter.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InputRequest {
    id: InputId,
    bytes: Vec<u8>,
}

impl InputRequest {
    pub(crate) fn new(id: InputId, bytes: Vec<u8>) -> Result<Self, CoreError> {
        if bytes.is_empty() || bytes.len() > MAX_INPUT_BYTES {
            return Err(CoreError::InputTooLarge { size: bytes.len() });
        }
        Ok(Self { id, bytes })
    }

    pub const fn id(&self) -> InputId {
        self.id
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }
}

/// Result reported by the transport adapter for one input request.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InputOutcome {
    Accepted,
    Rejected,
    Uncertain,
    Superseded,
}

/// Owned input result returned to a host after transport completion.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InputResult {
    pub id: InputId,
    pub outcome: InputOutcome,
}