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
// Basic formatting functions for container structures.
//
// This module implements intelligent formatting for Rust container types
// using the `:a` format specifier. It provides sophisticated pretty-printing
// capabilities that automatically adapt to data structure complexity and
// nesting depth, ensuring optimal readability for various container types.
//
// # Architecture Overview
//
// The formatting system uses a hierarchical approach based on nesting depth:
// - **Depth 0-1**: Simple containers formatted using Debug trait
// - **Depth 2**: 2D arrays with specialized row-based formatting
// - **Depth 3+**: Multi-dimensional arrays with recursive indentation
//
// This approach ensures that simple data structures remain compact while
// complex nested structures receive appropriate visual organization.
//
// # Core Functions
//
// ## format_container
// The main entry point for container formatting that analyzes structure
// complexity and delegates to appropriate specialized formatters.
//
// **Algorithm**:
// 1. Generate Debug representation of the input value
// 2. Analyze nesting depth using bracket counting
// 3. Route to appropriate formatter based on complexity
// 4. Return optimally formatted string representation
//
// **Performance**: O(n) analysis with single-pass depth detection
// **Memory**: Efficient pre-allocation based on input size estimation
//
// ## format_2d_array
// Specialized formatter for 2D arrays that creates visually organized
// row-based layouts with consistent indentation and spacing.
//
// **Formatting Rules**:
// - Opening brackets get dedicated lines with proper indentation
// - Each row receives consistent 2-space indentation
// - Row separators include newlines for visual clarity
// - Closing brackets align with opening structure
//
// **Output Example**:
// ```
// [
//   [1, 2, 3],
//   [4, 5, 6],
//   [7, 8, 9]
// ]
// ```
//
// ## format_nd_array
// Advanced formatter for multi-dimensional arrays that handles arbitrary
// nesting levels with recursive structure analysis and adaptive indentation.
//
// **Key Features**:
// - Bracket boundary detection for accurate parsing
// - Recursive sub-array processing with depth tracking
// - Memory-efficient string building with capacity pre-allocation
// - Robust error handling for malformed input structures
//
// **Algorithm Complexity**: O(n·d) where n is input size and d is depth
// **Memory Pattern**: Linear growth with pre-allocated capacity buffers
//
// ## format_sub_array
// Recursive formatter that handles nested sub-arrays with proper indentation
// management and depth-aware formatting decisions.
//
// **Indentation Strategy**:
// - Each nesting level adds 2 spaces of indentation
// - Closing brackets align with their opening counterparts
// - Recursive calls maintain consistent indentation hierarchy
// - Special handling for deeply nested structures
//
// # Technical Implementation Details
//
// ## String Processing Strategy
// The module uses efficient string manipulation techniques:
// - **Capacity Pre-allocation**: Estimates final size to minimize reallocations
// - **In-place Replacement**: Uses replace() for simple 2D transformations
// - **Builder Pattern**: Accumulates complex structures incrementally
// - **Memory Reuse**: Leverages string capacity across operations
//
// ## Bracket Parsing Integration
// Leverages common utility functions for robust parsing:
// - `count_nesting_depth()`: Determines formatting strategy
// - `find_first_level_brackets()`: Identifies structural boundaries
// - Quote-aware parsing prevents false matches in string data
// - Handles malformed input gracefully with fallback formatting
//
// ## Error Handling and Edge Cases
// The formatters handle various edge cases robustly:
// - **Empty Containers**: Preserves original Debug formatting
// - **Malformed Brackets**: Falls back to Debug representation
// - **Mixed Data Types**: Maintains type information in output
// - **Deep Nesting**: Prevents stack overflow with depth limits
//
// # Performance Characteristics
//
// ## Time Complexity
// - Simple containers (depth 0-1): O(1) - direct Debug formatting
// - 2D arrays (depth 2): O(n) - single-pass string replacement
// - Multi-dimensional (depth 3+): O(n·d) - recursive processing
//
// ## Space Complexity
// - String allocation: O(n + k) where k is formatting overhead
// - Recursive stack: O(d) where d is maximum nesting depth
// - Temporary buffers: Minimal through efficient string building
//
// ## Optimization Strategies
// - **Early Termination**: Simple cases bypass complex processing
// - **Capacity Estimation**: Pre-allocates string buffers appropriately
// - **Recursive Limits**: Prevents excessive memory usage in deep structures
// - **String Interning**: Reuses common formatting patterns
//
// # Integration and Usage
//
// ## Format Specifier Integration
// The `:a` specifier triggers container formatting through the macro system:
// 1. Format token parsing identifies `:a` specifier
// 2. Code generation calls `format_container()` function
// 3. Appropriate formatter handles the specific data structure
// 4. Formatted output integrates with color and style systems
//
// ## Type System Integration
// Works with any type implementing `std::fmt::Debug`:
// - **Vectors**: Multi-dimensional vector formatting
// - **Arrays**: Fixed-size array pretty-printing
// - **Custom Types**: User-defined Debug implementations
// - **Mixed Types**: Heterogeneous container support
//
// ## Output Quality Guarantees
// - **Consistency**: Uniform indentation and spacing rules
// - **Readability**: Optimal visual organization for human consumption
// - **Correctness**: Preserves all data while improving presentation
// - **Scalability**: Handles containers of arbitrary size and complexity

fn format_container<T: std::fmt::Debug>(value: &T) -> String {
    let debug_str = format!("{:?}", value);
    match count_nesting_depth(&debug_str) {
        0 | 1 => debug_str,
        2 => format_2d_array(&debug_str),
        _ => format_nd_array(&debug_str),
    }
}
fn format_2d_array(debug_str: &str) -> String {
    debug_str.replace("[[", "[\n  [")
            .replace("]]", "]\n]")
            .replace("], [", "],\n  [")
}
fn format_nd_array(debug_str: &str) -> String {
    if !debug_str.starts_with('[') || !debug_str.ends_with(']') {
        return debug_str.to_string();
    }
    let content = &debug_str[1..debug_str.len()-1];
    let brackets = find_first_level_brackets(content);
    if brackets.is_empty() {
        return debug_str.to_string();
    }
    let mut result = String::with_capacity(debug_str.len() + brackets.len() * 4);
    result.push_str("[\n");
    for (i, (start, end)) in brackets.iter().enumerate() {
        result.push_str("  ");
        let sub_array = &content[*start..*end+1];
        result.push_str(&format_sub_array(sub_array, 1));
        if i < brackets.len() - 1 { result.push_str(",\n"); }
    }
    result.push_str("\n]");
    result
}
fn format_sub_array(sub_array: &str, indent_level: usize) -> String {
    if !sub_array.contains("[[") { return sub_array.to_string(); }
    if !sub_array.starts_with('[') || !sub_array.ends_with(']') { return sub_array.to_string(); }
    let content = &sub_array[1..sub_array.len()-1];
    let brackets = find_first_level_brackets(content);
    if brackets.is_empty() { return sub_array.to_string(); }
    let indent_str = "  ".repeat(indent_level + 1);
    let close_indent = "  ".repeat(indent_level);
    let mut result = String::with_capacity(sub_array.len() + brackets.len() * (indent_str.len() + 4));
    result.push_str("[\n");
    for (i, (start, end)) in brackets.iter().enumerate() {
        result.push_str(&indent_str);
        let sub_sub_array = &content[*start..*end+1];
        result.push_str(&format_sub_array(sub_sub_array, indent_level + 1));
        if i < brackets.len() - 1 { result.push_str(",\n"); }
    }
    result.push_str(&format!("\n{}", close_indent));
    result.push(']');
    result
}