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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//! Structured content nodes — the output of Content.render().
//!
//! ContentNode is a rich, structured representation of rendered output that
//! supports styled text, tables, code blocks, charts, key-value pairs, and
//! fragments (compositions of multiple nodes).

use serde::{Deserialize, Serialize};
use std::fmt;

/// A structured content node — the output of Content.render()
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentNode {
    /// Styled text with spans
    Text(StyledText),
    /// Table with headers, rows, and optional styling
    Table(ContentTable),
    /// Code block with optional language
    Code {
        language: Option<String>,
        source: String,
    },
    /// Chart specification
    Chart(ChartSpec),
    /// Key-value pairs
    KeyValue(Vec<(String, ContentNode)>),
    /// Composition of multiple content nodes
    Fragment(Vec<ContentNode>),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StyledText {
    pub spans: Vec<StyledSpan>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StyledSpan {
    pub text: String,
    pub style: Style,
}

#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Style {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fg: Option<Color>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bg: Option<Color>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub bold: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub italic: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub underline: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub dim: bool,
}

fn is_false(v: &bool) -> bool {
    !v
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Color {
    Named(NamedColor),
    Rgb(u8, u8, u8),
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NamedColor {
    Red,
    Green,
    Blue,
    Yellow,
    Magenta,
    Cyan,
    White,
    Default,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContentTable {
    pub headers: Vec<String>,
    pub rows: Vec<Vec<ContentNode>>,
    pub border: BorderStyle,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_rows: Option<usize>,
    /// Column type hints: "string", "number", "date", etc.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub column_types: Option<Vec<String>>,
    /// Total row count before truncation (for display: "showing 50 of 1000").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub total_rows: Option<usize>,
    /// Whether interactive renderers should enable column sorting.
    #[serde(default)]
    pub sortable: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BorderStyle {
    Rounded,
    Sharp,
    Heavy,
    Double,
    Minimal,
    None,
}

impl Default for BorderStyle {
    fn default() -> Self {
        BorderStyle::Rounded
    }
}

// ========== Chart types ==========

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChartSpec {
    pub chart_type: ChartType,
    /// Data channels populated by typed builder methods (.x(), .y(), .open(), etc.)
    pub channels: Vec<ChartChannel>,
    /// Categorical x-axis labels (bar charts, box plots)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub x_categories: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub x_label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub y_label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub height: Option<usize>,
    /// Full ECharts option JSON override (injected by chart detection).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub echarts_options: Option<serde_json::Value>,
    /// Whether this chart should be rendered interactively (default true).
    #[serde(default = "default_true")]
    pub interactive: bool,
}

fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChartType {
    Line,
    Bar,
    Scatter,
    Area,
    Candlestick,
    Histogram,
    BoxPlot,
    Heatmap,
    Bubble,
}

/// A named data channel in a chart (e.g. "x", "y", "open", "high", "low", "close").
///
/// Channel names are internal — Shape users call typed methods like `.x()`, `.y()`,
/// never write string keys directly.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChartChannel {
    /// Role name — set by builder method, never by user (e.g. "x", "y", "open")
    pub name: String,
    /// Display label
    pub label: String,
    /// Extracted numeric data
    pub values: Vec<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<Color>,
}

impl ChartType {
    /// Returns the channel names required for this chart type.
    pub fn required_channels(&self) -> &[&str] {
        match self {
            ChartType::Line | ChartType::Area => &["x", "y"],
            ChartType::Bar => &["y"],
            ChartType::Scatter => &["x", "y"],
            ChartType::Candlestick => &["x", "open", "high", "low", "close"],
            ChartType::BoxPlot => &["x", "min", "q1", "median", "q3", "max"],
            ChartType::Histogram => &["values"],
            ChartType::Heatmap => &["x", "y", "value"],
            ChartType::Bubble => &["x", "y", "size"],
        }
    }
}

impl ChartSpec {
    /// Get a channel by role name.
    pub fn channel(&self, name: &str) -> Option<&ChartChannel> {
        self.channels.iter().find(|c| c.name == name)
    }

    /// Get all channels with a given role name (e.g. multiple "y" channels).
    pub fn channels_by_name(&self, name: &str) -> Vec<&ChartChannel> {
        self.channels.iter().filter(|c| c.name == name).collect()
    }

    /// Number of data points (from the first channel).
    pub fn data_len(&self) -> usize {
        self.channels.first().map(|c| c.values.len()).unwrap_or(0)
    }
}

// ========== Backward compatibility ==========

/// Backward-compatible type alias. New code should use `ChartChannel` directly.
pub type ChartSeries = ChartChannel;

/// Helper to create a ChartSpec from legacy series data (Vec<(f64, f64)> pairs).
///
/// Converts old-style `ChartSeries { label, data: Vec<(f64, f64)>, color }` into
/// channel-based representation with "x" and "y" channels.
impl ChartSpec {
    pub fn from_series(
        chart_type: ChartType,
        series: Vec<(String, Vec<(f64, f64)>, Option<Color>)>,
    ) -> Self {
        let mut channels = Vec::new();
        if let Some(first) = series.first() {
            // x channel from first series
            let x_values: Vec<f64> = first.1.iter().map(|(x, _)| *x).collect();
            channels.push(ChartChannel {
                name: "x".to_string(),
                label: "x".to_string(),
                values: x_values,
                color: None,
            });
        }
        // y channels from each series
        for (label, data, color) in &series {
            let y_values: Vec<f64> = data.iter().map(|(_, y)| *y).collect();
            channels.push(ChartChannel {
                name: "y".to_string(),
                label: label.clone(),
                values: y_values,
                color: color.clone(),
            });
        }
        ChartSpec {
            chart_type,
            channels,
            x_categories: None,
            title: None,
            x_label: None,
            y_label: None,
            width: None,
            height: None,
            echarts_options: None,
            interactive: true,
        }
    }
}

// ========== ContentNode helpers ==========

impl ContentNode {
    /// Create a plain text node.
    pub fn plain(text: impl Into<String>) -> Self {
        ContentNode::Text(StyledText {
            spans: vec![StyledSpan {
                text: text.into(),
                style: Style::default(),
            }],
        })
    }

    /// Create a styled text node.
    pub fn styled(text: impl Into<String>, style: Style) -> Self {
        ContentNode::Text(StyledText {
            spans: vec![StyledSpan {
                text: text.into(),
                style,
            }],
        })
    }

    /// Apply foreground color to this node.
    pub fn with_fg(self, color: Color) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.fg = Some(color.clone());
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }

    /// Apply background color.
    pub fn with_bg(self, color: Color) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.bg = Some(color.clone());
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }

    /// Apply bold.
    pub fn with_bold(self) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.bold = true;
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }

    /// Apply italic.
    pub fn with_italic(self) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.italic = true;
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }

    /// Apply underline.
    pub fn with_underline(self) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.underline = true;
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }

    /// Apply dim.
    pub fn with_dim(self) -> Self {
        match self {
            ContentNode::Text(mut st) => {
                for span in &mut st.spans {
                    span.style.dim = true;
                }
                ContentNode::Text(st)
            }
            other => other,
        }
    }
}

impl fmt::Display for ContentNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ContentNode::Text(st) => {
                for span in &st.spans {
                    write!(f, "{}", span.text)?;
                }
                Ok(())
            }
            ContentNode::Table(table) => {
                if !table.headers.is_empty() {
                    for (i, header) in table.headers.iter().enumerate() {
                        if i > 0 {
                            write!(f, " | ")?;
                        }
                        write!(f, "{}", header)?;
                    }
                    writeln!(f)?;
                    for (i, _) in table.headers.iter().enumerate() {
                        if i > 0 {
                            write!(f, "-+-")?;
                        }
                        write!(f, "---")?;
                    }
                    writeln!(f)?;
                }
                let limit = table.max_rows.unwrap_or(table.rows.len());
                for row in table.rows.iter().take(limit) {
                    for (i, cell) in row.iter().enumerate() {
                        if i > 0 {
                            write!(f, " | ")?;
                        }
                        write!(f, "{}", cell)?;
                    }
                    writeln!(f)?;
                }
                Ok(())
            }
            ContentNode::Code { source, .. } => write!(f, "{}", source),
            ContentNode::Chart(spec) => {
                write!(
                    f,
                    "[Chart: {}]",
                    spec.title.as_deref().unwrap_or("untitled")
                )
            }
            ContentNode::KeyValue(pairs) => {
                for (i, (key, value)) in pairs.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "{}: {}", key, value)?;
                }
                Ok(())
            }
            ContentNode::Fragment(parts) => {
                for part in parts {
                    write!(f, "{}", part)?;
                }
                Ok(())
            }
        }
    }
}

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

    #[test]
    fn test_plain_text_node() {
        let node = ContentNode::plain("hello world");
        match &node {
            ContentNode::Text(st) => {
                assert_eq!(st.spans.len(), 1);
                assert_eq!(st.spans[0].text, "hello world");
                assert_eq!(st.spans[0].style, Style::default());
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_styled_text_node() {
        let style = Style {
            bold: true,
            fg: Some(Color::Named(NamedColor::Red)),
            ..Default::default()
        };
        let node = ContentNode::styled("warning", style.clone());
        match &node {
            ContentNode::Text(st) => {
                assert_eq!(st.spans.len(), 1);
                assert_eq!(st.spans[0].text, "warning");
                assert_eq!(st.spans[0].style, style);
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_content_node_display() {
        assert_eq!(ContentNode::plain("hello").to_string(), "hello");

        let code = ContentNode::Code {
            language: Some("rust".into()),
            source: "fn main() {}".into(),
        };
        assert_eq!(code.to_string(), "fn main() {}");

        let chart = ContentNode::Chart(ChartSpec {
            chart_type: ChartType::Line,
            channels: vec![],
            x_categories: None,
            title: Some("My Chart".into()),
            x_label: None,
            y_label: None,
            width: None,
            height: None,
            echarts_options: None,
            interactive: true,
        });
        assert_eq!(chart.to_string(), "[Chart: My Chart]");

        let chart_no_title = ContentNode::Chart(ChartSpec {
            chart_type: ChartType::Bar,
            channels: vec![],
            x_categories: None,
            title: None,
            x_label: None,
            y_label: None,
            width: None,
            height: None,
            echarts_options: None,
            interactive: true,
        });
        assert_eq!(chart_no_title.to_string(), "[Chart: untitled]");
    }

    #[test]
    fn test_with_fg_color() {
        let node = ContentNode::plain("text").with_fg(Color::Named(NamedColor::Green));
        match &node {
            ContentNode::Text(st) => {
                assert_eq!(st.spans[0].style.fg, Some(Color::Named(NamedColor::Green)));
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_with_bold() {
        let node = ContentNode::plain("text").with_bold();
        match &node {
            ContentNode::Text(st) => {
                assert!(st.spans[0].style.bold);
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_with_italic() {
        let node = ContentNode::plain("text").with_italic();
        match &node {
            ContentNode::Text(st) => {
                assert!(st.spans[0].style.italic);
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_with_underline() {
        let node = ContentNode::plain("text").with_underline();
        match &node {
            ContentNode::Text(st) => {
                assert!(st.spans[0].style.underline);
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_with_dim() {
        let node = ContentNode::plain("text").with_dim();
        match &node {
            ContentNode::Text(st) => {
                assert!(st.spans[0].style.dim);
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_with_bg_color() {
        let node = ContentNode::plain("text").with_bg(Color::Rgb(255, 0, 0));
        match &node {
            ContentNode::Text(st) => {
                assert_eq!(st.spans[0].style.bg, Some(Color::Rgb(255, 0, 0)));
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_style_chaining() {
        let node = ContentNode::plain("text")
            .with_bold()
            .with_fg(Color::Named(NamedColor::Cyan))
            .with_underline();
        match &node {
            ContentNode::Text(st) => {
                assert!(st.spans[0].style.bold);
                assert!(st.spans[0].style.underline);
                assert_eq!(st.spans[0].style.fg, Some(Color::Named(NamedColor::Cyan)));
            }
            _ => panic!("expected Text variant"),
        }
    }

    #[test]
    fn test_non_text_node_style_passthrough() {
        let code = ContentNode::Code {
            language: None,
            source: "x = 1".into(),
        };
        let result = code.with_bold();
        match &result {
            ContentNode::Code { source, .. } => assert_eq!(source, "x = 1"),
            _ => panic!("expected Code variant"),
        }
    }

    #[test]
    fn test_fragment_composition() {
        let frag = ContentNode::Fragment(vec![
            ContentNode::plain("hello "),
            ContentNode::plain("world"),
        ]);
        assert_eq!(frag.to_string(), "hello world");
    }

    #[test]
    fn test_key_value_display() {
        let kv = ContentNode::KeyValue(vec![
            ("name".into(), ContentNode::plain("Alice")),
            ("age".into(), ContentNode::plain("30")),
        ]);
        assert_eq!(kv.to_string(), "name: Alice\nage: 30");
    }

    #[test]
    fn test_table_display() {
        let table = ContentNode::Table(ContentTable {
            headers: vec!["Name".into(), "Value".into()],
            rows: vec![
                vec![ContentNode::plain("a"), ContentNode::plain("1")],
                vec![ContentNode::plain("b"), ContentNode::plain("2")],
            ],
            border: BorderStyle::default(),
            max_rows: None,
            column_types: None,
            total_rows: None,
            sortable: false,
        });
        let output = table.to_string();
        assert!(output.contains("Name"));
        assert!(output.contains("Value"));
        assert!(output.contains("a"));
        assert!(output.contains("1"));
        assert!(output.contains("b"));
        assert!(output.contains("2"));
    }

    #[test]
    fn test_table_max_rows() {
        let table = ContentNode::Table(ContentTable {
            headers: vec!["X".into()],
            rows: vec![
                vec![ContentNode::plain("1")],
                vec![ContentNode::plain("2")],
                vec![ContentNode::plain("3")],
            ],
            border: BorderStyle::None,
            max_rows: Some(2),
            column_types: None,
            total_rows: None,
            sortable: false,
        });
        let output = table.to_string();
        assert!(output.contains("1"));
        assert!(output.contains("2"));
        assert!(!output.contains("3"));
    }

    #[test]
    fn test_content_node_equality() {
        let a = ContentNode::plain("hello");
        let b = ContentNode::plain("hello");
        let c = ContentNode::plain("world");
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn test_border_style_default() {
        assert_eq!(BorderStyle::default(), BorderStyle::Rounded);
    }

    #[test]
    fn test_chart_spec_channel_helpers() {
        let spec = ChartSpec {
            chart_type: ChartType::Line,
            channels: vec![
                ChartChannel {
                    name: "x".into(),
                    label: "Time".into(),
                    values: vec![1.0, 2.0, 3.0],
                    color: None,
                },
                ChartChannel {
                    name: "y".into(),
                    label: "Price".into(),
                    values: vec![10.0, 20.0, 30.0],
                    color: None,
                },
                ChartChannel {
                    name: "y".into(),
                    label: "Volume".into(),
                    values: vec![100.0, 200.0, 300.0],
                    color: None,
                },
            ],
            x_categories: None,
            title: None,
            x_label: None,
            y_label: None,
            width: None,
            height: None,
            echarts_options: None,
            interactive: true,
        };
        assert_eq!(spec.channel("x").unwrap().label, "Time");
        assert_eq!(spec.channels_by_name("y").len(), 2);
        assert_eq!(spec.data_len(), 3);
    }

    #[test]
    fn test_chart_type_required_channels() {
        assert_eq!(ChartType::Line.required_channels(), &["x", "y"]);
        assert_eq!(
            ChartType::Candlestick.required_channels(),
            &["x", "open", "high", "low", "close"]
        );
        assert_eq!(ChartType::Bar.required_channels(), &["y"]);
        assert_eq!(ChartType::Histogram.required_channels(), &["values"]);
    }

    #[test]
    fn test_chart_spec_from_series() {
        let spec = ChartSpec::from_series(
            ChartType::Line,
            vec![(
                "Revenue".to_string(),
                vec![(1.0, 100.0), (2.0, 200.0)],
                None,
            )],
        );
        assert_eq!(spec.channels.len(), 2); // x + y
        assert_eq!(spec.channel("x").unwrap().values, vec![1.0, 2.0]);
        assert_eq!(spec.channels_by_name("y")[0].label, "Revenue");
        assert_eq!(spec.channels_by_name("y")[0].values, vec![100.0, 200.0]);
    }

    #[test]
    fn test_content_node_serde_roundtrip() {
        let node = ContentNode::Chart(ChartSpec {
            chart_type: ChartType::Line,
            channels: vec![ChartChannel {
                name: "y".into(),
                label: "Price".into(),
                values: vec![1.0, 2.0],
                color: Some(Color::Named(NamedColor::Red)),
            }],
            x_categories: None,
            title: Some("Test".into()),
            x_label: None,
            y_label: None,
            width: None,
            height: None,
            echarts_options: None,
            interactive: true,
        });
        let json = serde_json::to_string(&node).unwrap();
        let roundtrip: ContentNode = serde_json::from_str(&json).unwrap();
        assert_eq!(node, roundtrip);
    }

    #[test]
    fn test_content_table_serde_roundtrip() {
        let node = ContentNode::Table(ContentTable {
            headers: vec!["A".into()],
            rows: vec![vec![ContentNode::plain("1")]],
            border: BorderStyle::Rounded,
            max_rows: None,
            column_types: None,
            total_rows: None,
            sortable: false,
        });
        let json = serde_json::to_string(&node).unwrap();
        let roundtrip: ContentNode = serde_json::from_str(&json).unwrap();
        assert_eq!(node, roundtrip);
    }
}