Skip to main content

shape_wire/
print_result.rs

1//! Wire print result - for terminal display
2//!
3//! This module defines the structure for values that have been
4//! pre-formatted or are destined for terminal output with ANSI colors.
5
6use crate::metadata::{TypeInfo, TypeRegistry};
7use crate::value::WireValue;
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10
11/// Result of a print statement or REPL expression
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct WirePrintResult {
14    /// Fully rendered string with ANSI colors
15    pub rendered: String,
16
17    /// Individual spans for rich interaction (hover, formatting changes)
18    #[serde(default, skip_serializing_if = "Vec::is_empty")]
19    pub spans: Vec<WirePrintSpan>,
20}
21
22/// A span of text within a print result
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24#[serde(tag = "type", rename_all = "lowercase")]
25pub enum WirePrintSpan {
26    /// Literal text
27    Literal {
28        text: String,
29        start: usize,
30        end: usize,
31        span_id: String,
32    },
33    /// A value that can be hovered or reformatted
34    Value {
35        text: String,
36        start: usize,
37        end: usize,
38        span_id: String,
39        variable_name: Option<String>,
40        raw_value: Box<WireValue>,
41        type_info: Box<TypeInfo>,
42        /// Current format name applied to this value
43        current_format: String,
44        /// Available formats for this type
45        type_registry: TypeRegistry,
46        /// Current format parameters
47        format_params: HashMap<String, WireValue>,
48    },
49}
50
51impl WirePrintResult {
52    /// Create a simple unformatted result
53    pub fn simple(text: impl Into<String>) -> Self {
54        WirePrintResult {
55            rendered: text.into(),
56            spans: vec![],
57        }
58    }
59}
60
61/// Represents a result intended for printing to the terminal
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
63pub struct PrintResult {
64    /// The original value
65    pub value: WireValue,
66    /// Type information
67    pub type_info: TypeInfo,
68    /// Available metadata/formats
69    pub type_registry: TypeRegistry,
70}
71
72impl PrintResult {
73    /// Create a new print result
74    pub fn new(value: WireValue, type_info: TypeInfo, type_registry: TypeRegistry) -> Self {
75        PrintResult {
76            value,
77            type_info,
78            type_registry,
79        }
80    }
81
82    /// Create from a number with default formatting
83    pub fn from_number(n: f64) -> Self {
84        PrintResult {
85            value: WireValue::Number(n),
86            type_info: TypeInfo::number(),
87            type_registry: TypeRegistry::for_number(),
88        }
89    }
90}