waft_widgets/framework/
element_node.rs1use 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#[derive(Clone, Debug, PartialEq, Eq)]
19pub struct ElementFacts {
20 pub widget_runtime_type: Option<&'static str>,
23 pub stateful: bool,
25 pub creation_location: Option<&'static Location<'static>>,
27}
28
29impl ElementFacts {
30 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
39pub(crate) fn is_local_file(file: &str) -> bool {
42 std::path::Path::new(file).is_relative()
43}
44
45pub 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 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 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}