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
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0
//! Traits and implementations for rendering collections of nodes.
//!
//! This module provides the [`Nodes`] and [`NodesWithState`] traits that allow
//! collections of [`Bluegum`] items to be rendered as groups of child nodes.
use ;
/// A trait for collections of items that can be rendered as multiple tree
/// nodes.
///
/// The `Nodes` trait allows collections like `Vec<T>`, `&[T]`, and `Box<T>` to
/// be used with [`Builder::add_nodes`] to add multiple child nodes at once.
/// This is more convenient than manually iterating and calling
/// [`Builder::add_node`] for each item.
///
/// # Examples
///
/// ```rust
/// use bluegum::{Bluegum, Builder};
///
/// struct Parent {
/// children: Vec<String>,
/// }
///
/// impl Bluegum for Parent {
/// fn node(&self, b: &mut Builder) {
/// b.name("Parent")
/// .add_nodes("children", &self.children); // Uses Nodes trait
/// }
/// }
/// ```
///
/// # Automatic Implementations
///
/// This trait is automatically implemented for:
/// - `&Vec<T>` where `T: Bluegum`
/// - `&[T]` where `T: Bluegum`
/// - `&Box<T>` where `T: Bluegum`
/// Implementation of [`Nodes`] for boxed items.
///
/// This allows a single boxed item to be treated as a collection of one node.
/// Implementation of [`Nodes`] for vectors.
///
/// This allows `Vec<T>` to be used directly with [`Builder::add_nodes`].
/// Implementation of [`Nodes`] for slices.
///
/// This allows `&[T]` to be used directly with [`Builder::add_nodes`].
/// A stateful variant of [`Nodes`] for collections that need external context.
///
/// This trait is similar to [`Nodes`] but allows the rendering process to
/// access external state, such as symbol tables or type information. It's used
/// with [`Builder::add_nodes_with_state`].
///
/// # Examples
///
/// ```rust
/// use bluegum::{
/// Bluegum,
/// BluegumWithState,
/// Builder,
/// };
///
/// struct SymbolTable {
/// names: Vec<String>,
/// }
///
/// struct Variable {
/// name_id: usize,
/// }
///
/// impl Bluegum for Variable {
/// fn node(&self, b: &mut Builder) {
/// b.name("Variable").field("name_id", &self.name_id);
/// }
/// }
///
/// impl BluegumWithState<SymbolTable> for Variable {
/// fn node_with_state(&self, b: &mut Builder, symbols: &SymbolTable) {
/// b.name("Variable")
/// .field("name", &symbols.names[self.name_id]);
/// }
/// }
///
/// struct Block {
/// variables: Vec<Variable>,
/// }
///
/// impl Bluegum for Block {
/// fn node(&self, b: &mut Builder) {
/// b.name("Block").add_nodes("variables", &self.variables);
/// }
/// }
///
/// impl BluegumWithState<SymbolTable> for Block {
/// fn node_with_state(&self, b: &mut Builder, symbols: &SymbolTable) {
/// b.name("Block").add_nodes_with_state(
/// symbols,
/// "variables",
/// &self.variables,
/// );
/// }
/// }
/// ```
///
/// # Automatic Implementations
///
/// This trait is automatically implemented for:
/// - `&Vec<T>` where `T: BluegumWithState<State>`
/// - `&[T]` where `T: BluegumWithState<State>`
/// - `&Box<T>` where `T: BluegumWithState<State>`
/// Implementation of [`NodesWithState`] for boxed items.
///
/// This allows a single boxed item to be treated as a stateful collection of
/// one node.
/// Implementation of [`NodesWithState`] for vectors.
///
/// This allows `Vec<T>` to be used directly with
/// [`Builder::add_nodes_with_state`].
/// Implementation of [`NodesWithState`] for slices.
///
/// This allows `&[T]` to be used directly with
/// [`Builder::add_nodes_with_state`].