Skip to main content

waft_widgets/framework/
element_node.rs

1//! An element's diagnostics node: Dart's `_ElementDiagnosticableTreeNode`, which adds to a tree
2//! node's JSON the element's widget type and whether it is stateful, and which the widget
3//! inspector reads its element's facts from.
4
5use std::any::Any;
6use std::fmt;
7use std::panic::Location;
8use std::rc::Rc;
9
10use waft_foundation::{
11    DiagnosticLevel, DiagnosticableTreeNode, DiagnosticsJsonMap, DiagnosticsNode,
12    DiagnosticsSerializationDelegate, DiagnosticsTreeStyle, HandleId, TextTreeConfiguration,
13    base_json_map,
14};
15
16/// What a node knows of its element, which Dart's node reads from the element itself: the
17/// widget inspector's view of `node.value` when the value is an element.
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub struct ElementFacts {
20    /// The widget's type, as Dart's `widget.runtimeType` prints; `None` once the element has no
21    /// widget.
22    pub widget_runtime_type: Option<&'static str>,
23    /// Whether the element is a `StatefulElement`.
24    pub stateful: bool,
25    /// Where its widget was made, in a debug build.
26    pub creation_location: Option<&'static Location<'static>>,
27}
28
29impl ElementFacts {
30    /// Whether the app's own code made the widget: Dart's `_isValueCreatedByLocalProject`. Rust
31    /// names a file of the workspace being built by a path relative to it, and a dependency's,
32    /// from Cargo's registry or a checkout beside the workspace, by an absolute one.
33    pub fn created_by_local_project(&self) -> bool {
34        self.creation_location
35            .is_some_and(|location| is_local_file(location.file()))
36    }
37}
38
39/// Whether `file`, as a creation location names it, is the app's own: a path relative to the
40/// workspace being built.
41pub(crate) fn is_local_file(file: &str) -> bool {
42    std::path::Path::new(file).is_relative()
43}
44
45/// Dart's `_ElementDiagnosticableTreeNode`: an element's node, which answers its
46/// [`ElementFacts`] as its value.
47pub struct ElementDiagnosticsNode {
48    node: DiagnosticableTreeNode,
49    facts: ElementFacts,
50}
51
52impl ElementDiagnosticsNode {
53    pub(crate) fn new(node: DiagnosticableTreeNode, facts: ElementFacts) -> ElementDiagnosticsNode {
54        ElementDiagnosticsNode { node, facts }
55    }
56
57    /// Sets the node's style, as Dart's `toDiagnosticsNode(style:)` does.
58    pub fn with_style(self, style: DiagnosticsTreeStyle) -> ElementDiagnosticsNode {
59        ElementDiagnosticsNode {
60            node: self.node.with_style(style),
61            ..self
62        }
63    }
64}
65
66impl fmt::Display for ElementDiagnosticsNode {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        self.node.fmt(f)
69    }
70}
71
72impl DiagnosticsNode for ElementDiagnosticsNode {
73    fn name(&self) -> Option<&str> {
74        self.node.name()
75    }
76
77    fn to_description(&self, parent_configuration: Option<&TextTreeConfiguration>) -> String {
78        self.node.to_description(parent_configuration)
79    }
80
81    fn show_separator(&self) -> bool {
82        self.node.show_separator()
83    }
84
85    fn level(&self) -> DiagnosticLevel {
86        self.node.level()
87    }
88
89    fn show_name(&self) -> bool {
90        self.node.show_name()
91    }
92
93    fn line_prefix(&self) -> Option<&str> {
94        self.node.line_prefix()
95    }
96
97    fn empty_body_description(&self) -> Option<&str> {
98        self.node.empty_body_description()
99    }
100
101    fn value(&self) -> Option<&dyn Any> {
102        Some(&self.facts)
103    }
104
105    fn value_id(&self) -> Option<HandleId> {
106        self.node.value_id()
107    }
108
109    fn type_name(&self) -> &'static str {
110        "_ElementDiagnosticableTreeNode"
111    }
112
113    fn style(&self) -> Option<DiagnosticsTreeStyle> {
114        self.node.style()
115    }
116
117    fn allow_wrap(&self) -> bool {
118        self.node.allow_wrap()
119    }
120
121    fn allow_name_wrap(&self) -> bool {
122        self.node.allow_name_wrap()
123    }
124
125    fn allow_truncate(&self) -> bool {
126        self.node.allow_truncate()
127    }
128
129    fn get_properties(&self) -> Vec<Rc<dyn DiagnosticsNode>> {
130        self.node.get_properties()
131    }
132
133    fn get_children(&self) -> Vec<Rc<dyn DiagnosticsNode>> {
134        self.node.get_children()
135    }
136
137    /// Dart's `_ElementDiagnosticableTreeNode.toJsonMap`: the node's, with the widget's type and
138    /// whether the element is stateful.
139    fn to_json_map(&self, delegate: &dyn DiagnosticsSerializationDelegate) -> DiagnosticsJsonMap {
140        let mut json = base_json_map(self, delegate);
141        if let Some(widget_runtime_type) = self.facts.widget_runtime_type {
142            json.insert("widgetRuntimeType".to_owned(), widget_runtime_type.into());
143        }
144        json.insert("stateful".to_owned(), self.facts.stateful.into());
145        json
146    }
147
148    fn to_string_deep_with(
149        &self,
150        prefix_line_one: &str,
151        prefix_other_lines: Option<&str>,
152        parent_configuration: Option<&TextTreeConfiguration>,
153        min_level: DiagnosticLevel,
154        wrap_width: usize,
155    ) -> String {
156        self.node.to_string_deep_with(
157            prefix_line_one,
158            prefix_other_lines,
159            parent_configuration,
160            min_level,
161            wrap_width,
162        )
163    }
164}