use sim_kernel::{CapabilityName, Expr, Result, Symbol};
use sim_lib_stream_core::{
PushResult, StreamEnvelope, StreamInspectorSnapshot, StreamInspectorStatus, StreamItem,
StreamStats, stream_cancel_capability, stream_open_capability, stream_push_capability,
stream_read_capability, stream_stats_capability,
};
use sim_lib_stream_fabric::{
stream_control_cancel_symbol, stream_control_next_symbol, stream_control_open_symbol,
stream_control_push_symbol, stream_control_stats_symbol,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SessionStatus {
Connecting,
Connected,
Disconnected,
Reconnecting,
Closed,
}
impl SessionStatus {
pub fn is_live(self) -> bool {
matches!(self, SessionStatus::Connected)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChangeEvent {
pub resource: Symbol,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BrowserStreamStatus {
Live,
Disconnected,
Reconnecting,
RefusedProfile,
BufferOverflow,
Cancelled,
Ended,
}
impl BrowserStreamStatus {
pub fn wire_label(self) -> &'static str {
match self {
Self::Live => "live",
Self::Disconnected => "disconnected",
Self::Reconnecting => "reconnecting",
Self::RefusedProfile => "refused-profile",
Self::BufferOverflow => "buffer-overflow",
Self::Cancelled => "cancelled",
Self::Ended => "ended",
}
}
pub fn symbol(self) -> Symbol {
Symbol::qualified("stream/browser-status", self.wire_label())
}
pub fn inspector_status(self) -> StreamInspectorStatus {
match self {
Self::Live => StreamInspectorStatus::Live,
Self::Disconnected => StreamInspectorStatus::Disconnected,
Self::Reconnecting => StreamInspectorStatus::Reconnecting,
Self::RefusedProfile => StreamInspectorStatus::RefusedProfile,
Self::BufferOverflow => StreamInspectorStatus::BufferOverflow,
Self::Cancelled => StreamInspectorStatus::Cancelled,
Self::Ended => StreamInspectorStatus::Ended,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WebStreamOperation {
Read,
Subscribe,
Push,
Cancel,
Stats,
}
impl WebStreamOperation {
pub fn wire_label(self) -> &'static str {
match self {
Self::Read => "read",
Self::Subscribe => "subscribe",
Self::Push => "push",
Self::Cancel => "cancel",
Self::Stats => "stats",
}
}
pub fn symbol(self) -> Symbol {
Symbol::qualified("stream/web", self.wire_label())
}
pub fn fabric_symbol(self) -> Symbol {
match self {
Self::Read => stream_control_next_symbol(),
Self::Subscribe => stream_control_open_symbol(),
Self::Push => stream_control_push_symbol(),
Self::Cancel => stream_control_cancel_symbol(),
Self::Stats => stream_control_stats_symbol(),
}
}
pub fn capability(self) -> CapabilityName {
match self {
Self::Read => stream_read_capability(),
Self::Subscribe => stream_open_capability(),
Self::Push => stream_push_capability(),
Self::Cancel => stream_cancel_capability(),
Self::Stats => stream_stats_capability(),
}
}
}
pub fn web_stream_operation_symbols() -> [Symbol; 5] {
[
WebStreamOperation::Read.symbol(),
WebStreamOperation::Subscribe.symbol(),
WebStreamOperation::Push.symbol(),
WebStreamOperation::Cancel.symbol(),
WebStreamOperation::Stats.symbol(),
]
}
pub fn web_stream_operation_capability_names() -> Vec<CapabilityName> {
[
WebStreamOperation::Read,
WebStreamOperation::Subscribe,
WebStreamOperation::Push,
WebStreamOperation::Cancel,
WebStreamOperation::Stats,
]
.into_iter()
.map(WebStreamOperation::capability)
.collect()
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamInspectorRecord {
pub stream_id: Symbol,
pub status: BrowserStreamStatus,
pub buffered: usize,
pub stats: StreamStats,
pub diagnostics: Vec<Symbol>,
pub snapshot: StreamInspectorSnapshot,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransportKind {
Fixture,
Wasm,
LocalServer,
RemoteServer,
Fabric,
}
pub trait Transport {
fn kind(&self) -> TransportKind;
fn status(&self) -> SessionStatus;
fn read(&self, resource: &Symbol) -> Result<Expr>;
fn realize(&mut self, resource: &Symbol, operation: &Expr) -> Result<Expr>;
fn drain_events(&mut self) -> Vec<ChangeEvent>;
fn stream_subscribe(&mut self, stream_id: &Symbol) -> Result<StreamInspectorRecord>;
fn stream_read(&mut self, stream_id: &Symbol, limit: usize) -> Result<Vec<StreamItem>>;
fn stream_push(&mut self, stream_id: &Symbol, envelope: StreamEnvelope) -> Result<PushResult>;
fn stream_cancel(&mut self, stream_id: &Symbol) -> Result<()>;
fn stream_stats(&self, stream_id: &Symbol) -> Result<StreamStats>;
fn stream_inspector(&self, stream_id: &Symbol) -> Result<StreamInspectorRecord>;
}