tsgo_rs_core 0.1.0

Shared errors, process lifecycle helpers, and fast-path primitives for tsgo-rs
Documentation
use crate::fast::CompactString;
use std::{sync::Arc, time::Duration};

/// Structured runtime events that embedders can observe.
///
/// The enum intentionally focuses on operationally relevant state transitions
/// such as timeouts, queue saturation, and cache eviction. Variants may be
/// added over time as the workspace exposes more subsystems.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TsgoEvent {
    /// A JSON-RPC request exceeded its configured timeout.
    JsonRpcRequestTimedOut {
        method: CompactString,
        timeout: Duration,
    },
    /// The JSON-RPC writer queue rejected a message because it was full.
    JsonRpcOutboundQueueFull,
    /// Pending JSON-RPC requests were failed because the transport broke.
    JsonRpcPendingRequestsFailed { error: CompactString, count: usize },
    /// A sync msgpack request exceeded its configured timeout.
    MsgpackRequestTimedOut {
        method: CompactString,
        timeout: Duration,
    },
    /// The msgpack worker queue rejected a request because it was full.
    MsgpackWorkerQueueFull { method: CompactString },
    /// The msgpack worker process was explicitly terminated.
    MsgpackWorkerTerminated { reason: CompactString },
    /// A cached snapshot was evicted to stay within configured limits.
    OrchestratorSnapshotEvicted { key: CompactString },
    /// A cached result was evicted to stay within configured limits.
    OrchestratorResultEvicted { key: CompactString },
}

/// Sink for structured operational events emitted by the workspace.
pub trait TsgoObserver: Send + Sync + 'static {
    /// Receives a single event.
    fn on_event(&self, event: &TsgoEvent);
}

/// Shared observer handle used across configs and transports.
pub type SharedObserver = Arc<dyn TsgoObserver>;

/// Emits an event when an observer is configured.
pub fn observe(observer: Option<&SharedObserver>, event: TsgoEvent) {
    if let Some(observer) = observer {
        observer.on_event(&event);
    }
}