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
use super::super::Bounds;
use crate::architecture_metrics::architecture_svg_group_bbox_padding_px;
pub(super) fn is_arch_dir_x(dir: char) -> bool {
matches!(dir, 'L' | 'R')
}
pub(super) fn is_arch_dir_y(dir: char) -> bool {
matches!(dir, 'T' | 'B')
}
pub(super) fn arrow_shift(dir: char, orig: f64, arrow_size: f64) -> f64 {
// Mermaid@11.12.2 `ArchitectureDirectionArrowShift`.
match dir {
'L' | 'T' => orig - arrow_size + 2.0,
'R' | 'B' => orig - 2.0,
_ => orig,
}
}
pub(super) fn extend_bounds(bounds: &mut Option<Bounds>, other: Bounds) {
let b = bounds.get_or_insert(other.clone());
b.min_x = b.min_x.min(other.min_x);
b.min_y = b.min_y.min(other.min_y);
b.max_x = b.max_x.max(other.max_x);
b.max_y = b.max_y.max(other.max_y);
}
pub(super) fn bounds_from_rect(x: f64, y: f64, w: f64, h: f64) -> Bounds {
Bounds {
min_x: x,
min_y: y,
max_x: x + w,
max_y: y + h,
}
}
#[derive(Clone, Copy)]
pub(super) struct GroupRect<'a> {
pub(super) id: &'a str,
pub(super) x: f64,
pub(super) y: f64,
pub(super) w: f64,
pub(super) h: f64,
pub(super) icon: Option<&'a str>,
pub(super) title: Option<&'a str>,
}
pub(super) struct GroupRectComputer<'a> {
icon_size_px: f64,
padding_px: f64,
services_in_group: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
junctions_in_group: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
child_groups: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
service_bounds: &'a rustc_hash::FxHashMap<&'a str, Bounds>,
junction_bounds: &'a rustc_hash::FxHashMap<&'a str, Bounds>,
group_rects: rustc_hash::FxHashMap<&'a str, Bounds>,
visiting: rustc_hash::FxHashSet<&'a str>,
}
impl<'a> GroupRectComputer<'a> {
pub(super) fn new(
icon_size_px: f64,
padding_px: f64,
services_in_group: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
junctions_in_group: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
child_groups: &'a rustc_hash::FxHashMap<&'a str, Vec<&'a str>>,
service_bounds: &'a rustc_hash::FxHashMap<&'a str, Bounds>,
junction_bounds: &'a rustc_hash::FxHashMap<&'a str, Bounds>,
) -> Self {
Self {
icon_size_px,
padding_px,
services_in_group,
junctions_in_group,
child_groups,
service_bounds,
junction_bounds,
group_rects: rustc_hash::FxHashMap::default(),
visiting: rustc_hash::FxHashSet::default(),
}
}
pub(super) fn get(&self, group_id: &str) -> Option<&Bounds> {
self.group_rects.get(group_id)
}
pub(super) fn compute(&mut self, group_id: &'a str) -> Option<Bounds> {
let debug_group = std::env::var("MERMAN_ARCH_DEBUG_GROUP_RECT")
.ok()
.filter(|value| !value.is_empty());
enum Step<'a> {
Enter(&'a str),
Exit(&'a str),
}
let mut stack = vec![Step::Enter(group_id)];
while let Some(step) = stack.pop() {
match step {
Step::Enter(id) => {
if self.group_rects.contains_key(id) {
continue;
}
if !self.visiting.insert(id) {
continue;
}
stack.push(Step::Exit(id));
if let Some(children) = self.child_groups.get(id) {
for child in children.iter().rev() {
if !self.group_rects.contains_key(child)
&& !self.visiting.contains(child)
{
stack.push(Step::Enter(child));
}
}
}
}
Step::Exit(id) => {
let debug_this_group = debug_group.as_deref() == Some(id);
let mut content: Option<Bounds> = None;
if let Some(svcs) = self.services_in_group.get(id) {
for svc_id in svcs {
if let Some(b) = self.service_bounds.get(svc_id) {
if debug_this_group {
eprintln!(
"[arch-group-rect] group={} service={} bounds=({}, {})-({}, {})",
id, svc_id, b.min_x, b.min_y, b.max_x, b.max_y
);
}
let mut tmp = content;
extend_bounds(&mut tmp, b.clone());
content = tmp;
}
}
}
if let Some(junctions) = self.junctions_in_group.get(id) {
for junction_id in junctions {
if let Some(b) = self.junction_bounds.get(junction_id) {
if debug_this_group {
eprintln!(
"[arch-group-rect] group={} junction={} bounds=({}, {})-({}, {})",
id, junction_id, b.min_x, b.min_y, b.max_x, b.max_y
);
}
let mut tmp = content;
extend_bounds(&mut tmp, b.clone());
content = tmp;
}
}
}
if let Some(children) = self.child_groups.get(id) {
// Empirical correction for nested compounds:
//
// Mermaid draws group rects from Cytoscape `node.boundingBox()` values.
// When groups nest, Cytoscape's compound bounds update uses a children
// bounding box that is not a perfect "union of already-padded child group
// rects" in SVG space; treating child group rects as fully-inclusive
// inputs makes parent groups slightly too large in parity-root viewBox
// comparisons (notably in deep group chains).
//
// Approximate this by shrinking child group bounds by half the group
// border width (2px / 2 == 1px) before unioning them into the parent's
// content bounds.
let child_group_inset = 1.0;
for child in children {
let Some(b) = self.group_rects.get(child).cloned() else {
continue;
};
if debug_this_group {
eprintln!(
"[arch-group-rect] group={} child-group={} raw=({}, {})-({}, {})",
id, child, b.min_x, b.min_y, b.max_x, b.max_y
);
}
let b = if (b.max_x - b.min_x) > 2.0 * child_group_inset
&& (b.max_y - b.min_y) > 2.0 * child_group_inset
{
Bounds {
min_x: b.min_x + child_group_inset,
min_y: b.min_y + child_group_inset,
max_x: b.max_x - child_group_inset,
max_y: b.max_y - child_group_inset,
}
} else {
b
};
if debug_this_group {
eprintln!(
"[arch-group-rect] group={} child-group={} inset=({}, {})-({}, {})",
id, child, b.min_x, b.min_y, b.max_x, b.max_y
);
}
let mut tmp = content;
extend_bounds(&mut tmp, b);
content = tmp;
}
}
// Upstream Mermaid draws group rectangles from Cytoscape `node.boundingBox()`
// values and then offsets them by `halfIconSize`. This renderer-side
// approximation is intentionally named separately from manatee's
// relocation/element-bbox policy; they are different Cytoscape phases and
// should not silently share a generic compound-padding helper.
let pad = architecture_svg_group_bbox_padding_px(self.padding_px);
if debug_this_group {
if let Some(content_bounds) = &content {
eprintln!(
"[arch-group-rect] group={} content=({}, {})-({}, {}) pad={}",
id,
content_bounds.min_x,
content_bounds.min_y,
content_bounds.max_x,
content_bounds.max_y,
pad
);
} else {
eprintln!("[arch-group-rect] group={} content=<empty> pad={}", id, pad);
}
}
let b = if let Some(content) = content {
Bounds {
min_x: content.min_x - pad,
min_y: content.min_y - pad,
max_x: content.max_x + pad,
max_y: content.max_y + pad,
}
} else {
// Empty group: match Mermaid's "no children" fallback sizing behavior.
Bounds {
min_x: 0.0,
min_y: 0.0,
max_x: self.icon_size_px.max(1.0),
max_y: self.icon_size_px.max(1.0),
}
};
if debug_this_group {
eprintln!(
"[arch-group-rect] group={} final=({}, {})-({}, {})",
id, b.min_x, b.min_y, b.max_x, b.max_y
);
}
self.group_rects.insert(id, b);
self.visiting.remove(id);
}
};
}
self.group_rects.get(group_id).cloned()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn group_rect_computer_handles_deep_child_group_chain_with_small_stack() {
const DEPTH: usize = 2048;
let handle = std::thread::Builder::new()
.name("architecture-group-rect-deep-chain".to_string())
.stack_size(64 * 1024)
.spawn(|| {
let group_ids = (0..DEPTH).map(|i| format!("g{i}")).collect::<Vec<_>>();
let mut services_in_group = rustc_hash::FxHashMap::<&str, Vec<&str>>::default();
let junctions_in_group = rustc_hash::FxHashMap::<&str, Vec<&str>>::default();
let mut child_groups = rustc_hash::FxHashMap::<&str, Vec<&str>>::default();
let mut service_bounds = rustc_hash::FxHashMap::<&str, Bounds>::default();
let junction_bounds = rustc_hash::FxHashMap::<&str, Bounds>::default();
for pair in group_ids.windows(2) {
child_groups.insert(pair[0].as_str(), vec![pair[1].as_str()]);
}
services_in_group.insert(group_ids[DEPTH - 1].as_str(), vec!["leaf"]);
service_bounds.insert(
"leaf",
Bounds {
min_x: 0.0,
min_y: 0.0,
max_x: 80.0,
max_y: 80.0,
},
);
let mut computer = GroupRectComputer::new(
80.0,
40.0,
&services_in_group,
&junctions_in_group,
&child_groups,
&service_bounds,
&junction_bounds,
);
let root = computer
.compute(group_ids[0].as_str())
.expect("root bounds");
assert!(root.max_x > root.min_x);
assert!(computer.get(group_ids[DEPTH - 1].as_str()).is_some());
})
.expect("spawn architecture group rect deep-chain test");
handle
.join()
.expect("group rect deep-chain compute should not overflow");
}
}