1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! Output adapter trait for handling print() results.
//!
//! Different execution modes (script vs REPL vs notebook) control how
//! print() output is surfaced.
//!
//! Per ADR-006 §2.7.4 (output adapter ruling), [`PrintResult`] and
//! [`PrintSpan`] live in `shape-runtime` (see [`crate::print_result`])
//! and `OutputAdapter::print` returns a [`KindedSlot`] — the
//! GENERIC_CARRIER single-value shape (§2.7.1.2). Adapters that have no
//! sensible heap value to return (script mode) return
//! `KindedSlot::none()`; adapters that surface the structured output
//! (REPL) attach the `PrintResult` to a typed-object heap value before
//! returning. In Phase 1.B the `from_print_result` heap-construction is
//! deferred — REPL mode currently returns `none()` and a follow-up wires
//! `PrintResult` through a typed schema.
use crate::print_result::PrintResult;
use shape_value::KindedSlot;
use std::sync::{Arc, Mutex};
/// Trait for handling print() output.
///
/// Different execution modes can provide different adapters:
/// - Scripts: [`StdoutAdapter`] (print and discard spans)
/// - REPL: [`ReplAdapter`] (preserve spans for reformatting)
/// - Tests: [`MockAdapter`] (capture output)
/// - Hosts (server / notebook): [`SharedCaptureAdapter`]
pub trait OutputAdapter: Send + Sync {
/// Handle print() output.
///
/// Returns the value that print() yields. Scripts return
/// [`KindedSlot::none()`]; REPL adapters MAY surface the
/// `PrintResult` via a future typed-schema heap value but currently
/// also return `none()` until that schema lands.
fn print(&mut self, result: PrintResult) -> KindedSlot;
/// Handle Content HTML from printing a Content value.
/// Default implementation does nothing (terminal adapters don't need HTML).
fn print_content_html(&mut self, _html: String) {}
/// Clone the adapter (for trait object cloning)
fn clone_box(&self) -> Box<dyn OutputAdapter>;
}
// Implement Clone for Box<dyn OutputAdapter>
impl Clone for Box<dyn OutputAdapter> {
fn clone(&self) -> Self {
self.clone_box()
}
}
/// Standard output adapter — prints to stdout and discards spans.
///
/// Used for script execution where spans aren't needed.
#[derive(Debug, Clone)]
pub struct StdoutAdapter;
impl OutputAdapter for StdoutAdapter {
fn print(&mut self, result: PrintResult) -> KindedSlot {
println!("{}", result.rendered);
KindedSlot::none()
}
fn clone_box(&self) -> Box<dyn OutputAdapter> {
Box::new(self.clone())
}
}
/// REPL output adapter — preserves spans for reformatting.
///
/// Pre-bulldozer this returned a `ValueWord::from_print_result(..)`
/// pointing at a `RareHeapData::PrintResult` heap arm. Post-ADR-006 that
/// path is gone; the typed-schema replacement (`PrintResult` as a
/// `HeapValue::TypedObject` with a runtime-registered schema) is a
/// follow-up. For now the REPL adapter consumes the rendered text and
/// returns `none()`; the structured `PrintResult` is dropped until the
/// schema lands.
#[derive(Debug, Clone)]
pub struct ReplAdapter;
impl OutputAdapter for ReplAdapter {
fn print(&mut self, _result: PrintResult) -> KindedSlot {
KindedSlot::none()
}
fn clone_box(&self) -> Box<dyn OutputAdapter> {
Box::new(self.clone())
}
}
/// Mock adapter for testing — captures output without printing.
#[derive(Debug, Clone, Default)]
pub struct MockAdapter {
/// Captured print outputs
pub captured: Vec<String>,
}
impl MockAdapter {
pub fn new() -> Self {
MockAdapter {
captured: Vec::new(),
}
}
/// Get all captured output
pub fn output(&self) -> Vec<String> {
self.captured.clone()
}
/// Clear captured output
pub fn clear(&mut self) {
self.captured.clear();
}
}
impl OutputAdapter for MockAdapter {
fn print(&mut self, result: PrintResult) -> KindedSlot {
self.captured.push(result.rendered.clone());
KindedSlot::none()
}
fn clone_box(&self) -> Box<dyn OutputAdapter> {
Box::new(self.clone())
}
}
/// Shared capture adapter for host integrations (server/notebook).
///
/// Captures rendered print output into shared state so the host can
/// surface it in API responses without scraping stdout.
/// Also captures Content HTML when Content values are printed.
#[derive(Debug, Clone, Default)]
pub struct SharedCaptureAdapter {
captured: Arc<Mutex<Vec<String>>>,
captured_full: Arc<Mutex<Vec<PrintResult>>>,
content_html: Arc<Mutex<Vec<String>>>,
}
impl SharedCaptureAdapter {
pub fn new() -> Self {
Self::default()
}
/// Get all captured output lines.
pub fn output(&self) -> Vec<String> {
self.captured
.lock()
.map(|v| v.clone())
.unwrap_or_else(|_| Vec::new())
}
/// Clear captured output lines.
pub fn clear(&self) {
if let Ok(mut v) = self.captured.lock() {
v.clear();
}
}
/// Push Content HTML captured from print(content_value).
pub fn push_content_html(&self, html: String) {
if let Ok(mut v) = self.content_html.lock() {
v.push(html);
}
}
/// Get all captured Content HTML fragments.
pub fn content_html(&self) -> Vec<String> {
self.content_html
.lock()
.map(|v| v.clone())
.unwrap_or_default()
}
/// Get all captured full PrintResults (with spans).
pub fn print_results(&self) -> Vec<PrintResult> {
self.captured_full
.lock()
.map(|v| v.clone())
.unwrap_or_default()
}
}
impl OutputAdapter for SharedCaptureAdapter {
fn print(&mut self, result: PrintResult) -> KindedSlot {
if let Ok(mut v) = self.captured.lock() {
v.push(result.rendered.clone());
}
if let Ok(mut v) = self.captured_full.lock() {
v.push(result);
}
KindedSlot::none()
}
fn print_content_html(&mut self, html: String) {
self.push_content_html(html);
}
fn clone_box(&self) -> Box<dyn OutputAdapter> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::print_result::PrintSpan;
fn make_test_result() -> PrintResult {
PrintResult {
rendered: "Test output".to_string(),
spans: vec![PrintSpan::Literal {
text: "Test output".to_string(),
start: 0,
end: 11,
span_id: "span_1".to_string(),
}],
}
}
#[test]
fn test_stdout_adapter_returns_none() {
let mut adapter = StdoutAdapter;
let result = make_test_result();
let returned = adapter.print(result);
assert_eq!(returned.slot().raw(), 0, "script-mode print returns none");
}
#[test]
fn test_repl_adapter_returns_none_phase1b() {
// Pre-ADR-006 this returned `ValueWord::from_print_result(..)`. The
// typed-schema replacement is deferred; current behaviour is to
// drop the structured payload and return `none()`.
let mut adapter = ReplAdapter;
let result = make_test_result();
let returned = adapter.print(result);
assert_eq!(returned.slot().raw(), 0);
}
#[test]
fn test_mock_adapter_captures() {
let mut adapter = MockAdapter::new();
adapter.print(PrintResult {
rendered: "Output 1".to_string(),
spans: vec![],
});
adapter.print(PrintResult {
rendered: "Output 2".to_string(),
spans: vec![],
});
assert_eq!(adapter.output(), vec!["Output 1", "Output 2"]);
adapter.clear();
assert_eq!(adapter.output().len(), 0);
}
#[test]
fn test_shared_capture_adapter_captures() {
let mut adapter = SharedCaptureAdapter::new();
adapter.print(PrintResult {
rendered: "Output A".to_string(),
spans: vec![],
});
adapter.print(PrintResult {
rendered: "Output B".to_string(),
spans: vec![],
});
assert_eq!(adapter.output(), vec!["Output A", "Output B"]);
adapter.clear();
assert!(adapter.output().is_empty());
}
}