shape-value 0.1.4

NaN-boxed value representation and heap types for Shape
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! ExternalValue: a serde-serializable value for display, wire, and debug.
//!
//! ExternalValue is the canonical format for values that cross system boundaries:
//! - Wire serialization (JSON, MessagePack, etc.)
//! - Debugger display
//! - REPL output
//! - Remote protocol
//!
//! It contains NO function refs, closures, raw pointers, or VM internals.
//! All variants are safe to serialize with serde.

use crate::heap_value::HeapValue;
use crate::tags;
use crate::value_word::ValueWord;
use std::collections::BTreeMap;
use std::fmt;

/// A serde-serializable value with no VM internals.
///
/// This is the "external" representation of a Shape value, suitable for
/// display, wire serialization, and debugging. It contains no function
/// references, closures, or raw pointers.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "type", content = "value")]
pub enum ExternalValue {
    /// 64-bit float
    Number(f64),
    /// 64-bit signed integer
    Int(i64),
    /// Boolean
    Bool(bool),
    /// String
    String(String),
    /// None / null
    None,
    /// Unit (void)
    Unit,
    /// Homogeneous or heterogeneous array
    Array(Vec<ExternalValue>),
    /// Untyped object (field name -> value)
    Object(BTreeMap<String, ExternalValue>),
    /// Typed object with schema name
    TypedObject {
        name: String,
        fields: BTreeMap<String, ExternalValue>,
    },
    /// Enum variant
    Enum {
        name: String,
        variant: String,
        data: Box<ExternalValue>,
    },
    /// Duration
    Duration { secs: u64, nanos: u32 },
    /// Timestamp (ISO 8601 string for portability)
    Time(String),
    /// Decimal (string representation for precision)
    Decimal(String),
    /// Error message
    Error(String),
    /// Result::Ok
    Ok(Box<ExternalValue>),
    /// Range
    Range {
        start: Option<Box<ExternalValue>>,
        end: Option<Box<ExternalValue>>,
        inclusive: bool,
    },
    /// DataTable summary (not full data — just metadata)
    DataTable { rows: usize, columns: Vec<String> },
    /// Opaque value that cannot be externalized (function, closure, etc.)
    Opaque(String),
}

/// Trait for looking up schema metadata from a schema_id.
///
/// Implemented by `TypeSchemaRegistry` in shape-runtime.
/// This abstraction lets shape-value convert TypedObjects to ExternalValue
/// without depending on shape-runtime.
pub trait SchemaLookup {
    /// Get the type name for a schema_id, or None if unknown.
    fn type_name(&self, schema_id: u64) -> Option<&str>;

    /// Get ordered field names for a schema_id, or None if unknown.
    fn field_names(&self, schema_id: u64) -> Option<Vec<&str>>;
}

/// A no-op schema lookup for contexts where schema info is unavailable.
/// TypedObjects will be converted with placeholder names.
pub struct NoSchemaLookup;

impl SchemaLookup for NoSchemaLookup {
    fn type_name(&self, _schema_id: u64) -> Option<&str> {
        std::option::Option::None
    }
    fn field_names(&self, _schema_id: u64) -> Option<Vec<&str>> {
        std::option::Option::None
    }
}

/// Convert a ValueWord value to an ExternalValue.
///
/// This is the canonical way to externalize a VM value for display, wire, or debug.
/// Schema lookup is needed to resolve TypedObject field names.
pub fn nb_to_external(nb: &ValueWord, schemas: &dyn SchemaLookup) -> ExternalValue {
    let bits = nb.raw_bits();

    if !tags::is_tagged(bits) {
        let f = f64::from_bits(bits);
        return if f.is_nan() {
            ExternalValue::Number(f64::NAN)
        } else {
            ExternalValue::Number(f)
        };
    }

    match tags::get_tag(bits) {
        tags::TAG_INT => ExternalValue::Int(tags::sign_extend_i48(tags::get_payload(bits))),
        tags::TAG_BOOL => ExternalValue::Bool(tags::get_payload(bits) != 0),
        tags::TAG_NONE => ExternalValue::None,
        tags::TAG_UNIT => ExternalValue::Unit,
        tags::TAG_FUNCTION => {
            ExternalValue::Opaque(format!("<function:{}>", tags::get_payload(bits) as u16))
        }
        tags::TAG_MODULE_FN => {
            ExternalValue::Opaque(format!("<module_fn:{}>", tags::get_payload(bits) as u32))
        }
        tags::TAG_REF => ExternalValue::Opaque("<ref>".to_string()),
        tags::TAG_HEAP => {
            if let Some(hv) = nb.as_heap_ref() {
                heap_to_external(hv, schemas)
            } else {
                ExternalValue::Opaque("<invalid_heap>".to_string())
            }
        }
        _ => ExternalValue::Opaque("<unknown_tag>".to_string()),
    }
}

/// Convert a HeapValue to an ExternalValue.
fn heap_to_external(hv: &HeapValue, schemas: &dyn SchemaLookup) -> ExternalValue {
    match hv {
        HeapValue::String(s) => ExternalValue::String((**s).clone()),
        HeapValue::Array(arr) => {
            let items: Vec<ExternalValue> =
                arr.iter().map(|v| nb_to_external(v, schemas)).collect();
            ExternalValue::Array(items)
        }
        HeapValue::TypedObject {
            schema_id,
            slots,
            heap_mask,
        } => {
            let type_name = schemas
                .type_name(*schema_id)
                .unwrap_or("unknown")
                .to_string();
            let field_names_opt = schemas.field_names(*schema_id);

            let mut fields = BTreeMap::new();
            let names: Vec<String> = if let Some(names) = field_names_opt {
                names.iter().map(|s| s.to_string()).collect()
            } else {
                (0..slots.len()).map(|i| format!("_{i}")).collect()
            };

            for (i, name) in names.into_iter().enumerate() {
                if i >= slots.len() {
                    break;
                }
                let is_heap = (heap_mask >> i) & 1 == 1;
                let ev = if is_heap {
                    // Heap slot: convert via HeapValue
                    let nb_val = slots[i].as_heap_nb();
                    nb_to_external(&nb_val, schemas)
                } else {
                    // Non-heap slot: raw bits, interpret as f64 (most common for non-heap fields)
                    ExternalValue::Number(slots[i].as_f64())
                };
                fields.insert(name, ev);
            }

            ExternalValue::TypedObject {
                name: type_name,
                fields,
            }
        }
        HeapValue::Closure { function_id, .. } => {
            ExternalValue::Opaque(format!("<closure:{function_id}>"))
        }
        HeapValue::Decimal(d) => ExternalValue::Decimal(d.to_string()),
        HeapValue::BigInt(i) => ExternalValue::Int(*i),
        HeapValue::HostClosure(_) => ExternalValue::Opaque("<host_closure>".to_string()),

        // DataTable family
        HeapValue::DataTable(dt) => ExternalValue::DataTable {
            rows: dt.row_count(),
            columns: dt.column_names().iter().map(|s| s.to_string()).collect(),
        },
        HeapValue::TypedTable { table, .. } => ExternalValue::DataTable {
            rows: table.row_count(),
            columns: table.column_names().iter().map(|s| s.to_string()).collect(),
        },
        HeapValue::RowView { .. } => ExternalValue::Opaque("<row_view>".to_string()),
        HeapValue::ColumnRef { .. } => ExternalValue::Opaque("<column_ref>".to_string()),
        HeapValue::IndexedTable { table, .. } => ExternalValue::DataTable {
            rows: table.row_count(),
            columns: table.column_names().iter().map(|s| s.to_string()).collect(),
        },
        HeapValue::ProjectedRef(..) => ExternalValue::Opaque("<ref>".to_string()),

        // Container types
        HeapValue::Range {
            start,
            end,
            inclusive,
        } => ExternalValue::Range {
            start: start.as_ref().map(|v| Box::new(nb_to_external(v, schemas))),
            end: end.as_ref().map(|v| Box::new(nb_to_external(v, schemas))),
            inclusive: *inclusive,
        },
        HeapValue::Enum(e) => ExternalValue::Enum {
            name: e.enum_name.clone(),
            variant: e.variant.clone(),
            data: Box::new(match &e.payload {
                crate::enums::EnumPayload::Unit => ExternalValue::None,
                crate::enums::EnumPayload::Tuple(nbs) => {
                    if nbs.len() == 1 {
                        nb_to_external(&nbs[0], schemas)
                    } else {
                        ExternalValue::Array(
                            nbs.iter().map(|v| nb_to_external(v, schemas)).collect(),
                        )
                    }
                }
                crate::enums::EnumPayload::Struct(fields) => {
                    let mut map = BTreeMap::new();
                    for (k, v) in fields {
                        map.insert(k.clone(), nb_to_external(v, schemas));
                    }
                    ExternalValue::Object(map)
                }
            }),
        },
        HeapValue::Some(v) => nb_to_external(v, schemas),
        HeapValue::Ok(v) => ExternalValue::Ok(Box::new(nb_to_external(v, schemas))),
        HeapValue::Err(v) => ExternalValue::Error(format!("{:?}", nb_to_external(v, schemas))),

        // Async
        HeapValue::Future(id) => ExternalValue::Opaque(format!("<future:{id}>")),
        HeapValue::TaskGroup { kind, task_ids } => {
            ExternalValue::Opaque(format!("<task_group:kind={kind},tasks={}>", task_ids.len()))
        }

        // Trait dispatch
        HeapValue::TraitObject { value, .. } => nb_to_external(value, schemas),

        // SQL pushdown
        HeapValue::ExprProxy(s) => ExternalValue::Opaque(format!("<expr_proxy:{s}>")),
        HeapValue::FilterExpr(_) => ExternalValue::Opaque("<filter_expr>".to_string()),

        // Time types
        HeapValue::Time(t) => ExternalValue::Time(t.to_rfc3339()),
        HeapValue::Duration(d) => {
            let secs = d.value as u64;
            ExternalValue::Duration { secs, nanos: 0 }
        }
        HeapValue::TimeSpan(ts) => ExternalValue::Duration {
            secs: ts.num_seconds().unsigned_abs(),
            nanos: (ts.subsec_nanos().unsigned_abs()),
        },
        HeapValue::Timeframe(tf) => ExternalValue::String(format!("{tf:?}")),

        // AST types
        HeapValue::TimeReference(_) => ExternalValue::Opaque("<time_reference>".to_string()),
        HeapValue::DateTimeExpr(_) => ExternalValue::Opaque("<datetime_expr>".to_string()),
        HeapValue::DataDateTimeRef(_) => ExternalValue::Opaque("<data_datetime_ref>".to_string()),
        HeapValue::TypeAnnotation(_) => ExternalValue::Opaque("<type_annotation>".to_string()),
        HeapValue::TypeAnnotatedValue { type_name, value } => {
            let inner = nb_to_external(value, schemas);
            ExternalValue::TypedObject {
                name: type_name.clone(),
                fields: BTreeMap::from([("value".to_string(), inner)]),
            }
        }
        HeapValue::PrintResult(pr) => ExternalValue::String(pr.rendered.clone()),
        HeapValue::SimulationCall(data) => ExternalValue::Opaque(format!(
            "<simulation_call:{} params={}>",
            data.name,
            data.params.len()
        )),
        HeapValue::FunctionRef { name, .. } => {
            ExternalValue::Opaque(format!("<function_ref:{name}>"))
        }
        HeapValue::DataReference(data) => {
            let mut fields = BTreeMap::new();
            fields.insert(
                "datetime".to_string(),
                ExternalValue::Time(data.datetime.to_rfc3339()),
            );
            fields.insert("id".to_string(), ExternalValue::String(data.id.clone()));
            fields.insert(
                "timeframe".to_string(),
                ExternalValue::String(format!("{:?}", data.timeframe)),
            );
            ExternalValue::Object(fields)
        }

        HeapValue::Instant(t) => ExternalValue::Opaque(format!("<instant:{:?}>", t.elapsed())),

        HeapValue::IoHandle(data) => {
            let status = if data.is_open() { "open" } else { "closed" };
            ExternalValue::Opaque(format!("<io_handle:{}:{}>", data.path, status))
        }

        HeapValue::NativeScalar(v) => {
            if let Some(i) = v.as_i64() {
                ExternalValue::Int(i)
            } else {
                ExternalValue::Number(v.as_f64())
            }
        }
        HeapValue::NativeView(v) => ExternalValue::Opaque(format!(
            "<{}:{}@0x{:x}>",
            if v.mutable { "cmut" } else { "cview" },
            v.layout.name,
            v.ptr
        )),
        HeapValue::HashMap(d) => {
            let mut fields = BTreeMap::new();
            for (k, v) in d.keys.iter().zip(d.values.iter()) {
                fields.insert(format!("{}", k), nb_to_external(v, schemas));
            }
            ExternalValue::Object(fields)
        }
        HeapValue::Set(d) => {
            ExternalValue::Array(d.items.iter().map(|v| nb_to_external(v, schemas)).collect())
        }
        HeapValue::Deque(d) => {
            ExternalValue::Array(d.items.iter().map(|v| nb_to_external(v, schemas)).collect())
        }
        HeapValue::PriorityQueue(d) => {
            ExternalValue::Array(d.items.iter().map(|v| nb_to_external(v, schemas)).collect())
        }
        HeapValue::Content(node) => ExternalValue::String(format!("{}", node)),
        HeapValue::SharedCell(arc) => nb_to_external(&arc.read().unwrap(), schemas),
        HeapValue::IntArray(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v)).collect())
        }
        HeapValue::FloatArray(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Number(v)).collect())
        }
        HeapValue::BoolArray(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Bool(v != 0)).collect())
        }
        HeapValue::I8Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::I16Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::I32Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::U8Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::U16Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::U32Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::U64Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Int(v as i64)).collect())
        }
        HeapValue::F32Array(a) => {
            ExternalValue::Array(a.iter().map(|&v| ExternalValue::Number(v as f64)).collect())
        }
        HeapValue::Matrix(m) => {
            ExternalValue::Opaque(format!("<Mat<number>:{}x{}>", m.rows, m.cols))
        }
        HeapValue::Iterator(_) => ExternalValue::Opaque("<iterator>".to_string()),
        HeapValue::Generator(_) => ExternalValue::Opaque("<generator>".to_string()),
        HeapValue::Mutex(_) => ExternalValue::Opaque("<mutex>".to_string()),
        HeapValue::Atomic(a) => {
            ExternalValue::Int(a.inner.load(std::sync::atomic::Ordering::Relaxed))
        }
        HeapValue::Channel(c) => {
            if c.is_sender() {
                ExternalValue::Opaque("<channel:sender>".to_string())
            } else {
                ExternalValue::Opaque("<channel:receiver>".to_string())
            }
        }
        HeapValue::Lazy(l) => {
            if let Ok(guard) = l.value.lock() {
                if let Some(val) = guard.as_ref() {
                    return nb_to_external(val, schemas);
                }
            }
            ExternalValue::Opaque("<lazy:uninitialized>".to_string())
        }
        HeapValue::Char(c) => ExternalValue::String(c.to_string()),
        HeapValue::FloatArraySlice {
            parent,
            offset,
            len,
        } => {
            let slice = &parent.data[*offset as usize..(*offset + *len) as usize];
            ExternalValue::Array(slice.iter().map(|&v| ExternalValue::Number(v)).collect())
        }
    }
}

/// Convert an ExternalValue back to a ValueWord value.
///
/// This is used for deserializing wire values back into the VM.
/// Note: Opaque values cannot be round-tripped.
pub fn external_to_nb(ev: &ExternalValue, schemas: &dyn SchemaLookup) -> ValueWord {
    let _ = schemas; // schemas needed for TypedObject reconstruction (future use)
    match ev {
        ExternalValue::Number(n) => ValueWord::from_f64(*n),
        ExternalValue::Int(i) => ValueWord::from_i64(*i),
        ExternalValue::Bool(b) => ValueWord::from_bool(*b),
        ExternalValue::String(s) => ValueWord::from_string(std::sync::Arc::new(s.clone())),
        ExternalValue::None => ValueWord::none(),
        ExternalValue::Unit => ValueWord::unit(),
        ExternalValue::Array(items) => {
            let nbs: Vec<ValueWord> = items.iter().map(|v| external_to_nb(v, schemas)).collect();
            ValueWord::from_array(std::sync::Arc::new(nbs))
        }
        ExternalValue::Decimal(s) => {
            if let Ok(d) = s.parse::<rust_decimal::Decimal>() {
                ValueWord::from_decimal(d)
            } else {
                ValueWord::from_string(std::sync::Arc::new(s.clone()))
            }
        }
        ExternalValue::Ok(inner) => ValueWord::from_ok(external_to_nb(inner, schemas)),
        ExternalValue::Error(msg) => {
            ValueWord::from_err(ValueWord::from_string(std::sync::Arc::new(msg.clone())))
        }
        ExternalValue::Range {
            start,
            end,
            inclusive,
        } => ValueWord::from_range(
            start.as_ref().map(|v| external_to_nb(v, schemas)),
            end.as_ref().map(|v| external_to_nb(v, schemas)),
            *inclusive,
        ),
        // Complex types that can't be fully round-tripped return None
        ExternalValue::Object(_)
        | ExternalValue::TypedObject { .. }
        | ExternalValue::Enum { .. }
        | ExternalValue::Duration { .. }
        | ExternalValue::Time(_)
        | ExternalValue::DataTable { .. }
        | ExternalValue::Opaque(_) => ValueWord::none(),
    }
}

impl fmt::Display for ExternalValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExternalValue::Number(n) => {
                if n.is_nan() {
                    write!(f, "NaN")
                } else if n.is_infinite() {
                    if n.is_sign_positive() {
                        write!(f, "Infinity")
                    } else {
                        write!(f, "-Infinity")
                    }
                } else if *n == (*n as i64) as f64 && n.is_finite() {
                    // Display whole numbers without decimal point
                    write!(f, "{}", *n as i64)
                } else {
                    write!(f, "{n}")
                }
            }
            ExternalValue::Int(i) => write!(f, "{i}"),
            ExternalValue::Bool(b) => write!(f, "{b}"),
            ExternalValue::String(s) => write!(f, "{s}"),
            ExternalValue::None => write!(f, "none"),
            ExternalValue::Unit => write!(f, "()"),
            ExternalValue::Array(items) => {
                write!(f, "[")?;
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{item}")?;
                }
                write!(f, "]")
            }
            ExternalValue::Object(fields) => {
                write!(f, "{{")?;
                for (i, (k, v)) in fields.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{k}: {v}")?;
                }
                write!(f, "}}")
            }
            ExternalValue::TypedObject { name, fields } => {
                write!(f, "{name} {{")?;
                for (i, (k, v)) in fields.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{k}: {v}")?;
                }
                write!(f, "}}")
            }
            ExternalValue::Enum {
                name,
                variant,
                data,
            } => {
                write!(f, "{name}::{variant}")?;
                if **data != ExternalValue::None {
                    write!(f, "({data})")?;
                }
                Ok(())
            }
            ExternalValue::Duration { secs, nanos } => {
                if *nanos > 0 {
                    write!(f, "{secs}.{:09}s", nanos)
                } else {
                    write!(f, "{secs}s")
                }
            }
            ExternalValue::Time(iso) => write!(f, "{iso}"),
            ExternalValue::Decimal(d) => write!(f, "{d}"),
            ExternalValue::Error(msg) => write!(f, "Error({msg})"),
            ExternalValue::Ok(inner) => write!(f, "Ok({inner})"),
            ExternalValue::Range {
                start,
                end,
                inclusive,
            } => {
                if let Some(s) = start {
                    write!(f, "{s}")?;
                }
                if *inclusive {
                    write!(f, "..=")?;
                } else {
                    write!(f, "..")?;
                }
                if let Some(e) = end {
                    write!(f, "{e}")?;
                }
                Ok(())
            }
            ExternalValue::DataTable { rows, columns } => {
                write!(f, "DataTable({rows} rows, {} cols)", columns.len())
            }
            ExternalValue::Opaque(desc) => write!(f, "{desc}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::value_word::ValueWord;

    #[test]
    fn test_number_roundtrip() {
        let nb = ValueWord::from_f64(3.14);
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert!(matches!(ev, ExternalValue::Number(n) if (n - 3.14).abs() < f64::EPSILON));
        let back = external_to_nb(&ev, &NoSchemaLookup);
        assert!((back.as_f64().unwrap() - 3.14).abs() < f64::EPSILON);
    }

    #[test]
    fn test_int_roundtrip() {
        let nb = ValueWord::from_i64(42);
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert_eq!(ev, ExternalValue::Int(42));
        let back = external_to_nb(&ev, &NoSchemaLookup);
        assert_eq!(back.as_i64().unwrap(), 42);
    }

    #[test]
    fn test_bool_roundtrip() {
        let nb = ValueWord::from_bool(true);
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert_eq!(ev, ExternalValue::Bool(true));
    }

    #[test]
    fn test_string_roundtrip() {
        let nb = ValueWord::from_string(std::sync::Arc::new("hello".to_string()));
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert_eq!(ev, ExternalValue::String("hello".to_string()));
        let back = external_to_nb(&ev, &NoSchemaLookup);
        assert_eq!(back.as_str().unwrap(), "hello");
    }

    #[test]
    fn test_none_and_unit() {
        assert_eq!(
            nb_to_external(&ValueWord::none(), &NoSchemaLookup),
            ExternalValue::None
        );
        assert_eq!(
            nb_to_external(&ValueWord::unit(), &NoSchemaLookup),
            ExternalValue::Unit
        );
    }

    #[test]
    fn test_function_is_opaque() {
        let nb = ValueWord::from_function(42);
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert!(matches!(ev, ExternalValue::Opaque(_)));
    }

    #[test]
    fn test_array() {
        let arr = vec![ValueWord::from_i64(1), ValueWord::from_i64(2)];
        let nb = ValueWord::from_array(std::sync::Arc::new(arr));
        let ev = nb_to_external(&nb, &NoSchemaLookup);
        assert_eq!(
            ev,
            ExternalValue::Array(vec![ExternalValue::Int(1), ExternalValue::Int(2)])
        );
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", ExternalValue::Number(3.14)), "3.14");
        assert_eq!(format!("{}", ExternalValue::Int(42)), "42");
        assert_eq!(format!("{}", ExternalValue::Bool(true)), "true");
        assert_eq!(format!("{}", ExternalValue::String("hi".into())), "hi");
        assert_eq!(format!("{}", ExternalValue::None), "none");
        assert_eq!(format!("{}", ExternalValue::Unit), "()");
        assert_eq!(
            format!(
                "{}",
                ExternalValue::Array(vec![ExternalValue::Int(1), ExternalValue::Int(2)])
            ),
            "[1, 2]"
        );
    }

    #[test]
    fn test_serde_json_roundtrip() {
        let ev = ExternalValue::TypedObject {
            name: "Candle".to_string(),
            fields: BTreeMap::from([
                ("open".to_string(), ExternalValue::Number(100.0)),
                ("close".to_string(), ExternalValue::Number(105.5)),
            ]),
        };
        let json = serde_json::to_string(&ev).unwrap();
        let back: ExternalValue = serde_json::from_str(&json).unwrap();
        assert_eq!(ev, back);
    }
}