cio 0.5.1

CIO provides two powerful procedural macros (`println!` and `input!`) that enhance console I/O operations in Rust, bringing Python-like convenience to Rust's type-safe environment.
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Advanced table formatting with hierarchical color system and intelligent structure detection.
//
// This module implements a sophisticated table rendering system that automatically
// analyzes JSON data structures and produces professional-grade formatted tables
// with Unicode box-drawing characters, hierarchical color coding, and intelligent
// layout optimization. It handles everything from simple key-value pairs to
// complex 3D nested data structures with automatic colspan detection.
//
// # Core Architecture
//
// ## Data Processing Pipeline
// The table formatter follows a comprehensive processing pipeline:
// 1. **JSON Serialization**: Convert input to serde_json::Value for uniform handling
// 2. **Structure Analysis**: Detect data patterns (1D, 2D, 3D, objects, arrays)
// 3. **Table Generation**: Build structured table representation with headers
// 4. **Layout Calculation**: Compute column widths, alignments, and spans
// 5. **Rendering**: Apply colors, borders, and formatting for final output
//
// ## Hierarchical Color System
// Professional color coding with semantic meaning:
// - **Level 1 Headers**: Bright Blue (94) + Bold + Italic - Primary categories
// - **Level 2 Headers**: Bright Cyan (96) + Bold + Italic - Secondary groupings  
// - **Level 3 Headers**: Bright Magenta (95) + Bold + Italic - Tertiary divisions
// - **Row Labels**: Bright White (97) + Bold + Italic - Only for true key columns
// - **Data Cells**: Standard White (37) - All data content for readability
//
// This hierarchy ensures visual clarity while maintaining professional appearance.
//
// # Format Specifier Integration
//
// ## Basic Table Format (`:t`)
// Automatic structure detection with intelligent formatting:
// - Simple objects → Key-value pairs without headers
// - Array of objects → Column-based table with automatic headers
// - Nested structures → Multi-level tables with appropriate spanning
//
// ## Custom Header Format (`:t(Col1, Col2, ...)`)
// User-defined column headers for specific use cases:
// - Overrides automatic header detection
// - Maintains data structure analysis for optimal layout
// - Supports arbitrary number of custom column names
//
// # Data Structure Support
//
// ## Simple Structures
// - **Key-Value Objects**: `{"name": "value"}` → Two-column layout
// - **1D Arrays**: `[1, 2, 3]` → Single-row table
// - **Primitive Values**: Automatic boxing in simple table format
//
// ## Complex Structures  
// - **2D Arrays**: `[[1,2], [3,4]]` → Matrix-style table
// - **Object Arrays**: `[{"a":1}, {"b":2}]` → Column-based tables
// - **Mixed Arrays**: Automatic content-based formatting decisions
//
// ## Advanced Structures
// - **3D Nested Objects**: Multi-level headers with intelligent colspan
// - **Heterogeneous Data**: Automatic structure normalization
// - **Array Expansion**: Complex nested arrays flattened to columns
//
// # Technical Implementation Details
//
// ## Structure Detection Algorithms
//
// ### Array Analysis
// The system uses sophisticated pattern matching:
// - **Homogeneity Detection**: Checks if all elements have same type
// - **Nesting Analysis**: Determines maximum depth and branching factor
// - **Content Classification**: Identifies primitives vs. complex structures
//
// ### Object Analysis  
// Multi-dimensional object handling:
// - **Dimension Detection**: 1D (flat), 2D (table), 3D (hierarchical)
// - **Key Consistency**: Ensures uniform column structure
// - **Value Type Analysis**: Determines appropriate formatting strategies
//
// ## Layout Calculation Engine
//
// ### Column Width Optimization
// Intelligent width calculation system:
// - **Content Analysis**: Measures actual visual width of all cells
// - **ANSI Stripping**: Removes color codes for accurate measurements
// - **Proportional Sizing**: Balances readability with space efficiency
// - **Minimum Width Guarantees**: Ensures headers and content both fit
//
// ### Alignment Intelligence
// Context-aware alignment decisions:
// - **Numeric Detection**: Right-aligns detected numbers
// - **Header Centering**: Centers all header content for professional look
// - **Text Left-Alignment**: Default for string content
// - **Mixed Column Handling**: Analyzes column content for optimal alignment
//
// ## Advanced Features
//
// ### Colspan Management
// Sophisticated spanning cell handling:
// - **Empty Cell Detection**: Identifies cells meant for spanning
// - **Span Calculation**: Determines appropriate span lengths
// - **Border Adaptation**: Adjusts box-drawing characters for spans
// - **Multi-Level Spanning**: Handles nested colspan scenarios
//
// ### Array Expansion System
// Automatic array-to-column conversion:
// - **Content Analysis**: Detects expandable array content
// - **Column Multiplication**: Creates appropriate number of sub-columns
// - **Data Distribution**: Spreads array elements across new columns
// - **Header Adaptation**: Adjusts headers for expanded structure
//
// ### Label Detection Intelligence
// Smart identification of row labels vs. data:
// - **Pattern Recognition**: Identifies typical label patterns
// - **Content Analysis**: Distinguishes labels from numeric data
// - **Context Awareness**: Uses table structure to inform decisions
// - **Multi-Level Detection**: Handles complex hierarchical labeling
//
// # Color System Implementation
//
// ## ANSI Code Management
// Efficient color handling:
// - **Constant Definitions**: Pre-compiled ANSI sequences for performance
// - **Reset Management**: Automatic color reset after each cell
// - **Nesting Safety**: Prevents color code conflicts in complex tables
// - **Fallback Handling**: Graceful degradation for unsupported terminals
//
// ## Hierarchical Application
// Context-sensitive color application:
// - **Level Detection**: Determines appropriate color based on table depth
// - **Semantic Coloring**: Colors carry meaning (headers vs. data vs. labels)
// - **Professional Palette**: Carefully chosen colors for readability
// - **Accessibility**: High contrast ratios for visual accessibility
//
// # Unicode Box Drawing System
//
// ## Character Set
// Complete box-drawing character support:
// - **Corners**: ┌ ┐ └ ┘ for table boundaries
// - **Intersections**: ┬ ┼ ┴ ├ ┤ for internal structure
// - **Extensions**: Advanced characters for complex spanning scenarios
// - **Consistency**: Uniform character usage throughout table
//
// ## Border Intelligence
// Adaptive border generation:
// - **Context Awareness**: Different borders for headers vs. data
// - **Span Adaptation**: Modifies borders for colspan scenarios  
// - **Nesting Support**: Handles nested table structures appropriately
// - **Clean Connections**: Ensures proper character connections at junctions
//
// # Performance Optimizations
//
// ## Memory Management
// Efficient resource utilization:
// - **Pre-allocation**: Estimates output size for minimal reallocations
// - **String Building**: Optimized string concatenation patterns
// - **Temporary Reduction**: Minimizes intermediate data structures
// - **Cache Efficiency**: Data structures optimized for CPU cache usage
//
// ## Algorithm Complexity
// Computational efficiency analysis:
// - **Width Calculation**: O(n·m) where n=rows, m=columns
// - **Structure Analysis**: O(d) where d=data structure depth
// - **Rendering**: O(n·m·w) where w=average cell width
// - **Overall**: Linear in table size with small constants
//
// ## String Processing
// Optimized text handling:
// - **ANSI Stripping**: Efficient regex-free ANSI code removal
// - **Unicode Handling**: Proper character counting for international text
// - **Escape Management**: Safe handling of special characters
// - **Buffer Reuse**: Minimizes string allocation overhead
//
// # Error Handling and Edge Cases
//
// ## Graceful Degradation
// Robust error handling strategy:
// - **Serialization Failures**: Falls back to Debug formatting
// - **Structure Anomalies**: Handles malformed data gracefully
// - **Empty Data**: Produces meaningful output for empty inputs
// - **Type Mismatches**: Automatic type coercion where possible
//
// ## Edge Case Management
// Comprehensive edge case coverage:
// - **Single Cell Tables**: Proper formatting for minimal data
// - **Extremely Wide Tables**: Handling of tables that exceed terminal width
// - **Deep Nesting**: Prevention of stack overflow in recursive structures
// - **Mixed Types**: Intelligent handling of heterogeneous data
//
// # Integration Points
//
// ## Macro System Integration
// Seamless integration with the macro system:
// - **Code Generation**: Produces efficient Rust code for table rendering
// - **Type Safety**: Maintains compile-time type checking
// - **Variable Handling**: Proper variable reference management
// - **Error Propagation**: Clean error handling through macro expansion
//
// ## JSON Compatibility
// Full serde_json integration:
// - **Automatic Serialization**: Any Serialize type supported
// - **Value Handling**: Complete JSON value type coverage
// - **Nested Structure**: Recursive handling of complex JSON
// - **Performance**: Minimal overhead for JSON processing
//
// ## Extension System
// Designed for extensibility:
// - **Custom Renderers**: Framework for specialized table types
// - **Format Hooks**: Integration points for custom formatting
// - **Color Themes**: Extensible color system for different themes
// - **Layout Plugins**: Modular layout algorithm system
//
// # Usage Examples and Patterns
//
// ## Simple Data
// Basic key-value pairs and simple arrays are formatted as clean, minimal tables
// without unnecessary headers or complex styling.
//
// ## Business Data
// Object arrays representing business data (users, products, etc.) automatically
// receive appropriate column headers and professional formatting.
//
// ## Scientific Data
// Multi-dimensional arrays and complex nested structures are rendered with
// hierarchical headers and appropriate mathematical-style formatting.
//
// ## Configuration Data
// Nested configuration objects are presented with clear hierarchical structure
// and color-coded levels for easy navigation.
//
// This comprehensive table formatting system ensures that any data structure
// can be presented in a professional, readable format while maintaining
// the performance and reliability expected in production systems.

fn format_table<T>(value: &T, headers: &[String], _header_color: &str) -> String
where
    T: std::fmt::Debug + serde::Serialize,
{
    let val = match serde_json::to_value(value) {
        Ok(v) => v,
        Err(_) => {
            let debug_str = format!("{:?}", value);
            return format!("┌─────────────────┐\n│ {:^15} │\n└─────────────────┘",
                          if debug_str.len() > 15 { &debug_str[..12] } else { &debug_str });
        }
    };
    render_sophisticated_table(&val, headers)
}
const RESET_CODE: &str = "\x1B[0m";
const DEFAULT_LEVEL1_COLOR: &str = "\x1B[94;1;3m";
const DEFAULT_LEVEL2_COLOR: &str = "\x1B[96;1;3m";
const DEFAULT_LEVEL3_COLOR: &str = "\x1B[95;1;3m";
const DEFAULT_KEY_COLOR: &str = "\x1B[97;1;3m";
const DATA_COLOR: &str = "\x1B[37m";
fn get_header_color_by_level(level: usize) -> &'static str {
    match level {
        0 => DEFAULT_LEVEL1_COLOR,
        1 => DEFAULT_LEVEL2_COLOR,
        2 => DEFAULT_LEVEL3_COLOR,
        _ => DEFAULT_LEVEL1_COLOR,
    }
}
fn is_first_column_key(col: usize, is_header: bool, n_headers: usize, table: &[Vec<String>]) -> bool {
    if col != 0 || is_header { return false; }
    let has_multiple_header_levels = n_headers > 1;
    let has_explicit_labels = table.len() > n_headers &&
                             table.iter().skip(n_headers).any(|row| {
                                 row.get(0).map_or(false, |first_cell| {
                                     first_cell.len() <= 3 && !first_cell.parse::<f64>().is_ok() ||
                                     ["attractions", "population", "x", "y", "z", "Français", "Mathématiques"].contains(&first_cell.as_str())
                                 })
                             });
    has_multiple_header_levels || has_explicit_labels
}
fn clean_ansi_for_width(s: &str) -> String {
    let mut result = String::new();
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\x1B' {
            while let Some(next_c) = chars.next() {
                if next_c == 'm' { break; }
            }
        } else {
            result.push(c);
        }
    }
    result
}
fn table_cell(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::Null => "".to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Array(a) => format!("[{}]", a.iter().map(table_cell).collect::<Vec<_>>().join(",")),
        serde_json::Value::Object(_) => "[object]".to_string(),
    }
}
fn expand_arrays_to_columns(table: Vec<Vec<String>>, n_headers: usize) -> Vec<Vec<String>> {
    if table.len() <= n_headers { return table; }
    let mut col_expansions = vec![1; table[0].len()];
    for row in table.iter().skip(n_headers) {
        for (i, c) in row.iter().enumerate().skip(1) {
            if c.starts_with('[') && c.ends_with(']') {
                if let Ok(serde_json::Value::Array(arr)) = serde_json::from_str::<serde_json::Value>(c) {
                    col_expansions[i] = col_expansions[i].max(arr.len());
                }
            } else if c.contains(", ") {
                col_expansions[i] = col_expansions[i].max(c.split(", ").count());
            }
        }
    }
    let mut new_table = vec![];
    for (idx, row) in table.iter().enumerate() {
        let mut new_row = vec![row[0].clone()];
        for (i, c) in row.iter().enumerate().skip(1) {
            let n = col_expansions[i];
            if idx < n_headers {
                new_row.push(c.clone());
                for _ in 1..n { new_row.push("".to_string()); }
            } else if c.starts_with('[') && c.ends_with(']') {
                if let Ok(serde_json::Value::Array(arr)) = serde_json::from_str::<serde_json::Value>(c) {
                    for j in 0..n {
                        new_row.push(arr.get(j).map(|x| table_cell(x)).unwrap_or("".to_string()));
                    }
                    continue;
                }
            } else if c.contains(", ") && n > 1 {
                let parts: Vec<_> = c.split(", ").collect();
                for j in 0..n { new_row.push(parts.get(j).unwrap_or(&"").to_string()); }
            } else {
                for j in 0..n { new_row.push(if j == 0 { c.clone() } else { "".to_string() }); }
            }
        }
        new_table.push(new_row);
    }
    new_table
}
fn table_col_widths(table: &[Vec<String>], equalize: bool) -> Vec<usize> {
    let cols = table.iter().map(|r| r.len()).max().unwrap_or(0);
    let mut widths = (0..cols)
        .map(|i| table.iter().filter_map(|r| r.get(i)).map(|v| clean_ansi_for_width(v).chars().count()).max().unwrap_or(0))
        .collect::<Vec<_>>();
    if equalize && widths.len() > 1 {
        let max = *widths[1..].iter().max().unwrap_or(&0);
        for w in &mut widths[1..] { *w = max; }
    }
    widths
}
fn center_content(s: &str, w: usize) -> String {
    let visual_len = clean_ansi_for_width(s).chars().count();
    if w <= visual_len { s.to_string() } else {
        let left = (w - visual_len) / 2;
        format!("{}{}{}", " ".repeat(left), s, " ".repeat(w - visual_len - left))
    }
}
fn calculate_colspans(header: &[String]) -> Vec<usize> {
    let mut spans = vec![1; header.len()];
    let mut i = 0;
    while i < header.len() {
        if !header[i].is_empty() {
            let mut span = 1;
            while i + span < header.len() && header[i + span].is_empty() { span += 1; }
            spans[i] = span;
            for j in 1..span { spans[i+j] = 0; }
            i += span;
        } else { i += 1; }
    }
    spans
}
fn calculate_cell_width(widths: &[usize], col: usize, span: usize) -> usize {
    widths[col..col+span].iter().sum::<usize>() + 3 * (span-1)
}
fn determine_alignments(table: &[Vec<String>], n_headers: usize) -> Vec<char> {
    (0..table[0].len()).map(|i| {
        if n_headers > 0 && table[..n_headers].iter().any(|r| !r[i].is_empty()) { 'c' }
        else if table.iter().skip(n_headers).all(|r| r.get(i).map_or(false, |s| clean_ansi_for_width(s).parse::<f64>().is_ok())) { 'r' }
        else { 'l' }
    }).collect()
}
fn format_table_row_sophisticated(row: &[String], widths: &[usize], aligns: &[char], is_header: bool, spans: Option<&[usize]>, header_level: usize, n_headers: usize, table: &[Vec<String>]) -> String {
    let mut result = String::new();
    let mut col = 0;
    let mut i = 0;
    let mut started = false;
    while i < row.len() {
        if let Some(sp) = spans { if sp[i] == 0 { col += 1; i += 1; continue; } }
        let cell_val = &row[i];
        let span = spans.map_or(1, |s| if s[i] > 0 { s[i] } else { 1 });
        let w = calculate_cell_width(widths, col, span);
        if cell_val.is_empty() {
            result.push_str(&" ".repeat(w + 3));
        } else {
            if !started { result.push('│'); started = true; }
            if is_header {
                let centered = center_content(cell_val, w);
                let colored = format!("{}{}{}", get_header_color_by_level(header_level), centered, RESET_CODE);
                result.push_str(&format!(" {} │", colored));
            } else if is_first_column_key(col, is_header, n_headers, table) {
                let colored = format!("{}{:<width$}{}", DEFAULT_KEY_COLOR, cell_val, RESET_CODE, width=w);
                result.push_str(&format!(" {} │", colored));
            } else if aligns[col] == 'r' || clean_ansi_for_width(cell_val).parse::<f64>().is_ok() {
                let colored = format!("{}{:>width$}{}", DATA_COLOR, cell_val, RESET_CODE, width=w);
                result.push_str(&format!(" {} │", colored));
            } else {
                let colored = format!("{}{:<width$}{}", DATA_COLOR, cell_val, RESET_CODE, width=w);
                result.push_str(&format!(" {} │", colored));
            }
        }
        col += span; i += span;
    }
    result.push('\n');
    result
}
fn format_separator_sophisticated(widths: &[usize], row: Option<&[String]>, prev_row: Option<&[String]>, left: &str, sep: &str, right: &str) -> String {
    let mut result = String::new();
    if let Some(row) = row {
        let fused = row.iter().any(|s| s.is_empty());
        let colspans = if fused { Some(calculate_colspans(row)) } else { None };
        let prev_colspans = prev_row.and_then(|pr| if pr.iter().any(|s| s.is_empty()) { Some(calculate_colspans(pr)) } else { None });
        let mut col = 0; let mut i = 0; let mut started = false;
        while i < row.len() {
            if let Some(ref sp) = colspans { if sp[i] == 0 { col += 1; i += 1; continue; } }
            let span = colspans.as_ref().map_or(1, |s| if s[i] > 0 { s[i] } else { 1 });
            let w = calculate_cell_width(widths, col, span);
            if row[i].is_empty() {
                result.push_str(&" ".repeat(w + 3));
            } else {
                if !started { result.push_str(left); started = true; }
                else {
                    let under = prev_colspans.as_ref().map_or(false, |sp| {
                        let mut prev_col = 0; let mut prev_i = 0;
                        while prev_i < sp.len() && prev_col < col {
                            if sp[prev_i] > 0 {
                                if sp[prev_i] > 1 && prev_col < col && col < prev_col + sp[prev_i] { return true; }
                                prev_col += sp[prev_i]; prev_i += sp[prev_i];
                            } else { prev_col += 1; prev_i += 1; }
                        }
                        false
                    });
                    result.push_str(if under { "┬" } else { sep });
                }
                result.push_str(&"─".repeat(w + 2));
            }
            col += span; i += span;
        }
        if started { result.push_str(right); }
        result.push('\n');
    } else {
        result.push_str(left);
        for (i, w) in widths.iter().enumerate() {
            result.push_str(&"─".repeat(*w + 2));
            if i != widths.len()-1 { result.push_str(sep); }
        }
        result.push_str(right);
        result.push('\n');
    }
    result
}
fn format_sophisticated_table(mut table: Vec<Vec<String>>, n_headers: usize, equalize_cols: bool, expand_arrays: bool) -> String {
    if table.is_empty() { return String::new(); }
    if expand_arrays { table = expand_arrays_to_columns(table, n_headers); }
    let widths = table_col_widths(&table, equalize_cols);
    let aligns = determine_alignments(&table, n_headers);
    let mut result = String::new();
    for (i, row) in table.iter().enumerate() {
        let is_header = i < n_headers;
        let prev_row = if i > 0 { Some(table[i-1].as_slice()) } else { None };
        let spans = if is_header && row.iter().any(|s| s.is_empty()) { Some(calculate_colspans(row)) } else { None };
        let header_level = if is_header { i } else { 0 };
        if i == 0 {
            result.push_str(&format_separator_sophisticated(&widths, Some(row), None, "┌", "┬", "┐"));
        } else if i == n_headers && n_headers > 0 {
            let has_complex_structure = table.iter().any(|r| r.iter().any(|s| s.is_empty())) || n_headers > 1;
            if has_complex_structure {
                result.push_str(&format_separator_sophisticated(&widths, Some(row), prev_row, "┌", "┼", "┤"));
            } else {
                result.push_str(&format_separator_sophisticated(&widths, Some(row), prev_row, "├", "┼", "┤"));
            }
        } else if i > 0 {
            result.push_str(&format_separator_sophisticated(&widths, Some(row), prev_row, "├", "┼", "┤"));
        }
        result.push_str(&format_table_row_sophisticated(row, &widths, &aligns, is_header, spans.as_deref(), header_level, n_headers, &table));
    }
    result.push_str(&format_separator_sophisticated(&widths, None, None, "└", "┴", "┘"));
    result
}
fn render_sophisticated_table(v: &serde_json::Value, custom_headers: &[String]) -> String {
    if let serde_json::Value::Array(a) = v {
        if a.iter().all(|x| !x.is_array() && !x.is_object()) {
            return format_sophisticated_table(vec![a.iter().map(table_cell).collect()], 0, false, false);
        }
        if a.iter().all(|x| matches!(x, serde_json::Value::Array(_)))
            && a.iter().all(|x| if let serde_json::Value::Array(r) = x { r.iter().all(|y| !y.is_array() && !y.is_object()) } else { false }) {
            let rows: Vec<Vec<String>> = a.iter().filter_map(|r| if let serde_json::Value::Array(vs) = r { Some(vs.iter().map(table_cell).collect()) } else { None }).collect();
            return format_sophisticated_table(rows, 0, false, false);
        }
        if a.iter().all(|x| matches!(x, serde_json::Value::Array(_))) &&
            a.iter().any(|x| if let serde_json::Value::Array(r) = x { r.iter().any(|y| matches!(y, serde_json::Value::Array(_))) } else { false }) {
            let mut rows = vec![];
            for v in a {
                if let serde_json::Value::Array(l2) = v {
                    for v2 in l2 {
                        if let serde_json::Value::Array(line) = v2 {
                            rows.push(line.iter().map(table_cell).collect());
                        }
                    }
                }
            }
            return format_sophisticated_table(rows, 0, false, false);
        }
        if a.iter().all(|x| x.is_object()) {
            let mut keys = std::collections::BTreeSet::new();
            for v in a {
                if let serde_json::Value::Object(m) = v { for k in m.keys() { keys.insert(k.clone()); } }
            }
            let keys: Vec<_> = keys.into_iter().collect();
            let has_label = a.iter().all(|v| v.get("").is_some());
            if has_label {
                let mut header = vec!["".to_string()];
                header.extend(a.iter().map(|v| v.get("").map(|x| table_cell(x)).unwrap_or_default()));
                let mut table = vec![header];
                for k in keys.iter().filter(|k| **k != "") {
                    let mut row = vec![k.clone()];
                    row.extend(a.iter().map(|v| if let serde_json::Value::Object(obj) = v {
                        obj.get(k).map(|x| table_cell(x)).unwrap_or_default()
                    } else { "".to_string() }));
                    table.push(row);
                }
                return format_sophisticated_table(table, 1, true, false);
            } else {
                let mut table = vec![if custom_headers.is_empty() { keys.clone() } else { custom_headers.to_vec() }];
                for v in a {
                    if let serde_json::Value::Object(obj) = v {
                        table.push(keys.iter().map(|k| obj.get(k).map(|x| table_cell(x)).unwrap_or_default()).collect());
                    }
                }
                return format_sophisticated_table(table, 1, true, false);
            }
        }
    }
    if let serde_json::Value::Object(m) = v {
        if m.values().all(|v| !v.is_object() && !v.is_array()) {
            if !custom_headers.is_empty() && custom_headers.len() >= 2 {
                let mut table = vec![vec![custom_headers[0].clone(), custom_headers[1].clone()]];
                let rows: Vec<_> = m.iter().map(|(k, v)| vec![k.clone(), table_cell(v)]).collect();
                table.extend(rows);
                return format_sophisticated_table(table, 1, false, false);
            } else {
                let rows: Vec<_> = m.iter().map(|(k, v)| vec![k.clone(), table_cell(v)]).collect();
                return format_sophisticated_table(rows, 0, false, false);
            }
        }
        if m.values().all(|v| v.is_array()) {
            let keys: Vec<_> = m.keys().cloned().collect();
            let max_len = m.values().filter_map(|v| if let serde_json::Value::Array(a) = v { Some(a.len()) } else { None }).max().unwrap_or(0);
            let mut table = vec![if custom_headers.is_empty() { keys.clone() } else { custom_headers.to_vec() }];
            for i in 0..max_len {
                let row = keys.iter().map(|k| {
                    if let Some(serde_json::Value::Array(a)) = m.get(k) {
                        a.get(i).map(|x| table_cell(x)).unwrap_or_default()
                    } else { "".to_string() }
                }).collect();
                table.push(row);
            }
            return format_sophisticated_table(table, 1, true, false);
        }
        if m.values().all(|v| v.is_object()) {
            let is_3d = m.values().any(|v| if let serde_json::Value::Object(obj) = v {
                obj.values().any(|x| matches!(x, serde_json::Value::Object(_)))
            } else { false });
            if is_3d {
                let outers: Vec<_> = m.keys().cloned().collect();
                let mut mids = vec![]; let mut leaves = std::collections::BTreeSet::new();
                for k in &outers {
                    if let Some(serde_json::Value::Object(obj)) = m.get(k) {
                        for mid in obj.keys() {
                            if !mids.contains(mid) { mids.push(mid.clone()); }
                            if let Some(serde_json::Value::Object(lf)) = obj.get(mid) {
                                for leaf in lf.keys() { leaves.insert(leaf.clone()); }
                            }
                        }
                    }
                }
                let leaves: Vec<_> = leaves.into_iter().collect();
                let mut h1 = vec!["".to_string()];
                for k in &outers {
                    if let Some(serde_json::Value::Object(obj)) = m.get(k) {
                        let colspan = obj.len();
                        h1.push(k.clone());
                        for _ in 1..colspan { h1.push("".to_string()); }
                    }
                }
                let mut h2 = vec!["".to_string()];
                for k in &outers {
                    if let Some(serde_json::Value::Object(obj)) = m.get(k) {
                        for mid in obj.keys() { h2.push(mid.clone()); }
                    }
                }
                let mut table = vec![h1, h2];
                for leaf in &leaves {
                    let mut row = vec![leaf.clone()];
                    for k in &outers {
                        if let Some(serde_json::Value::Object(obj)) = m.get(k) {
                            for mid in obj.keys() {
                                if let Some(serde_json::Value::Object(lf)) = obj.get(mid) {
                                    row.push(lf.get(leaf).map(|x| table_cell(x)).unwrap_or_default());
                                } else { row.push("".to_string()); }
                            }
                        }
                    }
                    table.push(row);
                }
                return format_sophisticated_table(table, 2, true, true);
            } else {
                let cols: Vec<_> = m.keys().cloned().collect();
                let mut rows_set = std::collections::BTreeSet::new();
                for v in m.values() {
                    if let serde_json::Value::Object(obj) = v {
                        for k in obj.keys() { rows_set.insert(k.clone()); }
                    }
                }
                let rows: Vec<_> = rows_set.into_iter().collect();
                let mut table = vec![{
                    let mut h = vec!["".to_string()];
                    h.extend(if custom_headers.is_empty() { cols.clone() } else { custom_headers.to_vec() });
                    h
                }];
                for r in &rows {
                    let mut row = vec![r.clone()];
                    for c in &cols {
                        if let Some(serde_json::Value::Object(obj)) = m.get(c) {
                            row.push(obj.get(r).map(|x| table_cell(x)).unwrap_or_default());
                        } else { row.push("".to_string()); }
                    }
                    table.push(row);
                }
                return format_sophisticated_table(table, 1, true, true);
            }
        }
    }
    format!("{}", v)
}