1use sim_kernel::{CapabilityName, Expr, Result, Symbol};
12use sim_lib_stream_core::{
13 PushResult, StreamEnvelope, StreamInspectorSnapshot, StreamInspectorStatus, StreamItem,
14 StreamStats, stream_cancel_capability, stream_open_capability, stream_push_capability,
15 stream_read_capability, stream_stats_capability,
16};
17use sim_lib_stream_fabric::{
18 stream_control_cancel_symbol, stream_control_next_symbol, stream_control_open_symbol,
19 stream_control_push_symbol, stream_control_stats_symbol,
20};
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum SessionStatus {
25 Connecting,
27 Connected,
29 Disconnected,
31 Reconnecting,
33 Closed,
35}
36
37impl SessionStatus {
38 pub fn is_live(self) -> bool {
40 matches!(self, SessionStatus::Connected)
41 }
42}
43
44#[derive(Clone, Debug, PartialEq, Eq)]
46pub struct ChangeEvent {
47 pub resource: Symbol,
49}
50
51#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub enum BrowserStreamStatus {
54 Live,
56 Disconnected,
58 Reconnecting,
60 RefusedProfile,
62 BufferOverflow,
64 Cancelled,
66 Ended,
68}
69
70impl BrowserStreamStatus {
71 pub fn wire_label(self) -> &'static str {
73 match self {
74 Self::Live => "live",
75 Self::Disconnected => "disconnected",
76 Self::Reconnecting => "reconnecting",
77 Self::RefusedProfile => "refused-profile",
78 Self::BufferOverflow => "buffer-overflow",
79 Self::Cancelled => "cancelled",
80 Self::Ended => "ended",
81 }
82 }
83
84 pub fn symbol(self) -> Symbol {
86 Symbol::qualified("stream/browser-status", self.wire_label())
87 }
88
89 pub fn inspector_status(self) -> StreamInspectorStatus {
91 match self {
92 Self::Live => StreamInspectorStatus::Live,
93 Self::Disconnected => StreamInspectorStatus::Disconnected,
94 Self::Reconnecting => StreamInspectorStatus::Reconnecting,
95 Self::RefusedProfile => StreamInspectorStatus::RefusedProfile,
96 Self::BufferOverflow => StreamInspectorStatus::BufferOverflow,
97 Self::Cancelled => StreamInspectorStatus::Cancelled,
98 Self::Ended => StreamInspectorStatus::Ended,
99 }
100 }
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105pub enum WebStreamOperation {
106 Read,
108 Subscribe,
110 Push,
112 Cancel,
114 Stats,
116}
117
118impl WebStreamOperation {
119 pub fn wire_label(self) -> &'static str {
121 match self {
122 Self::Read => "read",
123 Self::Subscribe => "subscribe",
124 Self::Push => "push",
125 Self::Cancel => "cancel",
126 Self::Stats => "stats",
127 }
128 }
129
130 pub fn symbol(self) -> Symbol {
132 Symbol::qualified("stream/web", self.wire_label())
133 }
134
135 pub fn fabric_symbol(self) -> Symbol {
137 match self {
138 Self::Read => stream_control_next_symbol(),
139 Self::Subscribe => stream_control_open_symbol(),
140 Self::Push => stream_control_push_symbol(),
141 Self::Cancel => stream_control_cancel_symbol(),
142 Self::Stats => stream_control_stats_symbol(),
143 }
144 }
145
146 pub fn capability(self) -> CapabilityName {
148 match self {
149 Self::Read => stream_read_capability(),
150 Self::Subscribe => stream_open_capability(),
151 Self::Push => stream_push_capability(),
152 Self::Cancel => stream_cancel_capability(),
153 Self::Stats => stream_stats_capability(),
154 }
155 }
156}
157
158pub fn web_stream_operation_symbols() -> [Symbol; 5] {
160 [
161 WebStreamOperation::Read.symbol(),
162 WebStreamOperation::Subscribe.symbol(),
163 WebStreamOperation::Push.symbol(),
164 WebStreamOperation::Cancel.symbol(),
165 WebStreamOperation::Stats.symbol(),
166 ]
167}
168
169pub fn web_stream_operation_capability_names() -> Vec<CapabilityName> {
171 [
172 WebStreamOperation::Read,
173 WebStreamOperation::Subscribe,
174 WebStreamOperation::Push,
175 WebStreamOperation::Cancel,
176 WebStreamOperation::Stats,
177 ]
178 .into_iter()
179 .map(WebStreamOperation::capability)
180 .collect()
181}
182
183#[derive(Clone, Debug, PartialEq, Eq)]
185pub struct StreamInspectorRecord {
186 pub stream_id: Symbol,
188 pub status: BrowserStreamStatus,
190 pub buffered: usize,
192 pub stats: StreamStats,
194 pub diagnostics: Vec<Symbol>,
196 pub snapshot: StreamInspectorSnapshot,
198}
199
200#[derive(Clone, Copy, Debug, PartialEq, Eq)]
202pub enum TransportKind {
203 Fixture,
205 Wasm,
207 LocalServer,
209 RemoteServer,
211 Fabric,
214}
215
216pub trait Transport {
221 fn kind(&self) -> TransportKind;
223
224 fn status(&self) -> SessionStatus;
226
227 fn read(&self, resource: &Symbol) -> Result<Expr>;
229
230 fn realize(&mut self, resource: &Symbol, operation: &Expr) -> Result<Expr>;
234
235 fn drain_events(&mut self) -> Vec<ChangeEvent>;
237
238 fn stream_subscribe(&mut self, stream_id: &Symbol) -> Result<StreamInspectorRecord>;
240
241 fn stream_read(&mut self, stream_id: &Symbol, limit: usize) -> Result<Vec<StreamItem>>;
243
244 fn stream_push(&mut self, stream_id: &Symbol, envelope: StreamEnvelope) -> Result<PushResult>;
246
247 fn stream_cancel(&mut self, stream_id: &Symbol) -> Result<()>;
249
250 fn stream_stats(&self, stream_id: &Symbol) -> Result<StreamStats>;
252
253 fn stream_inspector(&self, stream_id: &Symbol) -> Result<StreamInspectorRecord>;
255}