ascfix 0.7.1

Automatic ASCII diagram repair tool for Markdown files
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
//! Primitive types representing ASCII diagram elements.

/// Box drawing style for different box types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Part of public API for diagram rendering
pub enum BoxStyle {
    /// Single-line boxes: ─ │ ┌ ┐ └ ┘
    Single,
    /// Double-line boxes: ═ ║ ╔ ╗ ╚ ╝
    Double,
    /// Rounded-corner boxes: ─ │ ╭ ╮ ╰ ╯
    Rounded,
}

/// Box drawing characters for a specific style.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Part of public API for diagram rendering
pub struct BoxChars {
    /// Horizontal line character
    pub horizontal: char,
    /// Vertical line character
    pub vertical: char,
    /// Top-left corner
    pub top_left: char,
    /// Top-right corner
    pub top_right: char,
    /// Bottom-left corner
    pub bottom_left: char,
    /// Bottom-right corner
    pub bottom_right: char,
}

impl BoxStyle {
    /// Get the characters used for this box style.
    #[must_use]
    #[allow(dead_code)] // Reason: Part of public API for diagram rendering
    pub const fn chars(self) -> BoxChars {
        match self {
            Self::Single => BoxChars {
                horizontal: '',
                vertical: '',
                top_left: '',
                top_right: '',
                bottom_left: '',
                bottom_right: '',
            },
            Self::Double => BoxChars {
                horizontal: '',
                vertical: '',
                top_left: '',
                top_right: '',
                bottom_left: '',
                bottom_right: '',
            },
            Self::Rounded => BoxChars {
                horizontal: '',
                vertical: '',
                top_left: '',
                top_right: '',
                bottom_left: '',
                bottom_right: '',
            },
        }
    }

    /// Determine box style from a corner character.
    ///
    /// Returns the style if the character is a corner of a box,
    /// or None if the character is not a corner.
    #[must_use]
    #[allow(dead_code)] // Reason: Part of public API for diagram detection
    pub const fn from_corner(ch: char) -> Option<Self> {
        match ch {
            '' | '' | '' | '' => Some(Self::Single),
            '' | '' | '' | '' => Some(Self::Double),
            '' | '' | '' | '' => Some(Self::Rounded),
            _ => None,
        }
    }
}

/// A rectangular box defined by its border.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Box {
    /// Top-left corner (row, col)
    pub top_left: (usize, usize),
    /// Bottom-right corner (row, col)
    pub bottom_right: (usize, usize),
    /// Style of the box (single, double, or rounded lines)
    pub style: BoxStyle,
    /// Index of parent box (if nested inside another box)
    pub parent_idx: Option<usize>,
    /// Indices of child boxes (if this box contains other boxes)
    pub child_indices: Vec<usize>,
}

impl Box {
    /// Get the width of the box (number of columns).
    #[allow(dead_code)] // Reason: Used by normalization pipeline
    #[must_use]
    pub const fn width(&self) -> usize {
        self.bottom_right.1 - self.top_left.1 + 1
    }

    /// Get the height of the box (number of rows).
    #[allow(dead_code)] // Reason: Used by normalization pipeline
    #[must_use]
    pub const fn height(&self) -> usize {
        self.bottom_right.0 - self.top_left.0 + 1
    }

    /// Check if a position is inside the box interior (not on border).
    #[allow(dead_code)] // Reason: Used by normalization pipeline
    #[must_use]
    pub const fn contains_interior(&self, row: usize, col: usize) -> bool {
        row > self.top_left.0
            && row < self.bottom_right.0
            && col > self.top_left.1
            && col < self.bottom_right.1
    }

    /// Check if a position is on the box border.
    #[allow(dead_code)] // Reason: Used by normalization pipeline
    #[must_use]
    pub const fn contains_border(&self, row: usize, col: usize) -> bool {
        (row == self.top_left.0 || row == self.bottom_right.0)
            && col >= self.top_left.1
            && col <= self.bottom_right.1
            || (col == self.top_left.1 || col == self.bottom_right.1)
                && row >= self.top_left.0
                && row <= self.bottom_right.0
    }
}

/// Arrow type for different arrow styles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Part of public API for diagram rendering
pub enum ArrowType {
    /// Standard arrow: → ←
    Standard,
    /// Double arrow: ⇒ ⇐
    Double,
    /// Long arrow: ⟶ ⟵
    Long,
    /// Dashed arrow: · > <
    Dashed,
}

/// Arrow drawing characters for a specific style.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Part of public API for diagram rendering
pub struct ArrowChars {
    /// Line character
    pub line: char,
    /// Arrowhead pointing right
    pub arrowhead_right: char,
    /// Arrowhead pointing left
    pub arrowhead_left: char,
}

impl ArrowType {
    /// Get the characters used for this arrow type.
    #[must_use]
    #[allow(dead_code)] // Reason: Part of public API for diagram rendering
    pub const fn chars(self) -> ArrowChars {
        match self {
            Self::Standard => ArrowChars {
                line: '',
                arrowhead_right: '',
                arrowhead_left: '',
            },
            Self::Double => ArrowChars {
                line: '',
                arrowhead_right: '',
                arrowhead_left: '',
            },
            Self::Long => ArrowChars {
                line: '',
                arrowhead_right: '',
                arrowhead_left: '',
            },
            Self::Dashed => ArrowChars {
                line: '·',
                arrowhead_right: '>',
                arrowhead_left: '<',
            },
        }
    }

    /// Determine arrow type from an arrowhead character.
    ///
    /// Returns the type if the character is an arrowhead,
    /// or None if the character is not an arrowhead.
    #[must_use]
    #[allow(dead_code)] // Reason: Part of public API for diagram detection
    pub const fn from_char(ch: char) -> Option<Self> {
        match ch {
            '' | '' => Some(Self::Standard),
            '' | '' => Some(Self::Double),
            '' | '' => Some(Self::Long),
            '>' | '<' => Some(Self::Dashed),
            _ => None,
        }
    }
}

/// A horizontal arrow or connector.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct HorizontalArrow {
    /// Row position
    pub row: usize,
    /// Starting column
    pub start_col: usize,
    /// Ending column
    pub end_col: usize,
    /// Type of the arrow (standard, double, long, dashed)
    pub arrow_type: ArrowType,
    /// Direction: true if rightward (→), false if leftward (←)
    pub rightward: bool,
    /// Original arrow character to preserve (e.g., '→', '←')
    pub arrow_char: Option<char>,
}

/// A vertical arrow or connector.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct VerticalArrow {
    /// Column position
    pub col: usize,
    /// Starting row
    pub start_row: usize,
    /// Ending row
    pub end_row: usize,
    /// Type of the arrow (standard, double, long, dashed)
    pub arrow_type: ArrowType,
    /// Direction: true if downward (↓), false if upward (↑)
    pub downward: bool,
    /// Original arrow character to preserve (e.g., '↓', '↑')
    pub arrow_char: Option<char>,
}

/// A row of text content.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct TextRow {
    /// Row position
    pub row: usize,
    /// Starting column
    pub start_col: usize,
    /// Ending column (inclusive)
    pub end_col: usize,
    /// Text content
    pub content: String,
}

/// A segment of a connection line (either horizontal or vertical).
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by connection line detection and rendering
pub enum Segment {
    /// Horizontal segment: row and column range
    Horizontal {
        /// Row position
        row: usize,
        /// Starting column
        start_col: usize,
        /// Ending column
        end_col: usize,
    },
    /// Vertical segment: column and row range
    Vertical {
        /// Column position
        col: usize,
        /// Starting row
        start_row: usize,
        /// Ending row
        end_row: usize,
    },
}

impl Segment {
    /// Get the length of this segment (number of cells spanned).
    #[allow(dead_code)] // Reason: Used by normalization pipeline
    #[must_use]
    pub fn length(&self) -> usize {
        match self {
            Self::Horizontal {
                start_col, end_col, ..
            } => end_col - start_col + 1,
            Self::Vertical {
                start_row, end_row, ..
            } => end_row - start_row + 1,
        }
    }
}

/// An L-shaped or multi-segment connection line (e.g., between boxes).
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct ConnectionLine {
    /// Segments that make up this connection line
    pub segments: Vec<Segment>,
    /// Index of the box this line originates from (if any)
    pub from_box: Option<usize>,
    /// Index of the box this line connects to (if any)
    pub to_box: Option<usize>,
}

/// Single-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const SINGLE_LINE_HORIZ: char = '';
/// Single-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const SINGLE_LINE_VERT: char = '';
/// Single-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const SINGLE_LINE_CORNERS: &[char] = &['', '', '', ''];

/// Double-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const DOUBLE_LINE_HORIZ: char = '';
/// Double-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const DOUBLE_LINE_VERT: char = '';
/// Double-line box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const DOUBLE_LINE_CORNERS: &[char] = &['', '', '', ''];

/// Rounded box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const ROUNDED_HORIZ: char = '';
/// Rounded box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const ROUNDED_VERT: char = '';
/// Rounded box characters for character set constant.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const ROUNDED_CORNERS: &[char] = &['', '', '', ''];

/// Check if a character is a box character.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const fn is_box_char(ch: char) -> bool {
    matches!(
        ch,
        '' | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
    )
}

/// Check if a character is a box corner (any style).
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const fn is_box_corner(ch: char) -> bool {
    matches!(
        ch,
        '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | ''
    )
}

/// Check if a character is a double-line box corner.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const fn is_double_line_corner(ch: char) -> bool {
    matches!(ch, '' | '' | '' | '')
}

/// Check if a character is a rounded box corner.
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const fn is_rounded_corner(ch: char) -> bool {
    matches!(ch, '' | '' | '' | '')
}

/// Check if a character is a vertical arrow character (any style or direction).
#[allow(dead_code)] // Reason: Part of public API for diagram detection
const fn is_vertical_arrow_char(ch: char) -> bool {
    matches!(ch, '' | '' | '' | '' | '' | '')
}

/// What a label is attached to (for tracking during normalization).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by label detection and normalization
pub enum LabelAttachment {
    /// Label attached to a box (by index)
    Box(usize),
    /// Label attached to a horizontal arrow (by index)
    HorizontalArrow(usize),
    /// Label attached to a vertical arrow (by index)
    VerticalArrow(usize),
    /// Label attached to a connection line (by index)
    ConnectionLine(usize),
}

/// A label: text positioned near a primitive with tracked attachment.
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct Label {
    /// Row position of the label
    pub row: usize,
    /// Column position of the label
    pub col: usize,
    /// Text content of the label
    pub content: String,
    /// What this label is attached to
    pub attached_to: LabelAttachment,
    /// Offset from the attachment point (delta row, delta col)
    pub offset: (isize, isize),
}

/// Complete inventory of detected primitives in a diagram.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)] // Reason: Used by main processing pipeline
pub struct PrimitiveInventory {
    /// Detected boxes
    pub boxes: Vec<Box>,
    /// Detected horizontal arrows
    pub horizontal_arrows: Vec<HorizontalArrow>,
    /// Detected vertical arrows
    pub vertical_arrows: Vec<VerticalArrow>,
    /// Text rows (extracted from inside boxes)
    pub text_rows: Vec<TextRow>,
    /// Detected connection lines
    pub connection_lines: Vec<ConnectionLine>,
    /// Detected labels attached to primitives
    pub labels: Vec<Label>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_box_dimensions() {
        let b = Box {
            top_left: (0, 0),
            bottom_right: (3, 5),
            style: BoxStyle::Single,
            parent_idx: None,
            child_indices: Vec::new(),
        };
        assert_eq!(b.width(), 6);
        assert_eq!(b.height(), 4);
    }

    #[test]
    fn test_box_contains_interior() {
        let b = Box {
            top_left: (0, 0),
            bottom_right: (3, 5),
            style: BoxStyle::Single,
            parent_idx: None,
            child_indices: Vec::new(),
        };
        assert!(b.contains_interior(1, 1));
        assert!(b.contains_interior(2, 3));
        assert!(!b.contains_interior(0, 1)); // Top edge
        assert!(!b.contains_interior(3, 1)); // Bottom edge
        assert!(!b.contains_interior(1, 0)); // Left edge
    }

    #[test]
    fn test_box_contains_border() {
        let b = Box {
            top_left: (0, 0),
            bottom_right: (3, 5),
            style: BoxStyle::Single,
            parent_idx: None,
            child_indices: Vec::new(),
        };
        assert!(b.contains_border(0, 0)); // Top-left
        assert!(b.contains_border(0, 3)); // Top
        assert!(b.contains_border(3, 5)); // Bottom-right
        assert!(!b.contains_border(1, 1)); // Interior
    }

    #[test]
    fn test_horizontal_arrow() {
        let arr = HorizontalArrow {
            row: 5,
            start_col: 2,
            end_col: 8,
            arrow_type: ArrowType::Standard,
            rightward: true,
            arrow_char: Some(''),
        };
        assert_eq!(arr.row, 5);
        assert_eq!(arr.start_col, 2);
    }

    #[test]
    fn test_vertical_arrow() {
        let arr = VerticalArrow {
            col: 3,
            start_row: 1,
            end_row: 6,
            arrow_type: ArrowType::Standard,
            downward: true,
            arrow_char: None,
        };
        assert_eq!(arr.col, 3);
        assert_eq!(arr.start_row, 1);
    }

    #[test]
    fn test_text_row() {
        let tr = TextRow {
            row: 2,
            start_col: 5,
            end_col: 10,
            content: "Hello".to_string(),
        };
        assert_eq!(tr.content, "Hello");
    }

    #[test]
    fn test_is_box_char() {
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(!is_box_char('a'));
        assert!(!is_box_char(' '));
    }

    #[test]
    fn test_is_box_corner() {
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(!is_box_corner(''));
        assert!(!is_box_corner(''));
    }

    #[test]
    fn test_box_style_single_chars() {
        let chars = BoxStyle::Single.chars();
        // Single line: horizontal, vertical, top_left, top_right, bottom_left, bottom_right
        assert_eq!(chars.horizontal, '');
        assert_eq!(chars.vertical, '');
        assert_eq!(chars.top_left, '');
        assert_eq!(chars.top_right, '');
        assert_eq!(chars.bottom_left, '');
        assert_eq!(chars.bottom_right, '');
    }

    #[test]
    fn test_box_style_double_chars() {
        let chars = BoxStyle::Double.chars();
        // Double line: horizontal, vertical, top_left, top_right, bottom_left, bottom_right
        assert_eq!(chars.horizontal, '');
        assert_eq!(chars.vertical, '');
        assert_eq!(chars.top_left, '');
        assert_eq!(chars.top_right, '');
        assert_eq!(chars.bottom_left, '');
        assert_eq!(chars.bottom_right, '');
    }

    #[test]
    fn test_box_style_rounded_chars() {
        let chars = BoxStyle::Rounded.chars();
        // Rounded: horizontal, vertical, top_left, top_right, bottom_left, bottom_right
        assert_eq!(chars.horizontal, '');
        assert_eq!(chars.vertical, '');
        assert_eq!(chars.top_left, '');
        assert_eq!(chars.top_right, '');
        assert_eq!(chars.bottom_left, '');
        assert_eq!(chars.bottom_right, '');
    }

    #[test]
    fn test_is_box_char_double_line() {
        // Double-line characters should be recognized
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
    }

    #[test]
    fn test_is_box_char_rounded() {
        // Rounded characters should be recognized
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
        assert!(is_box_char(''));
    }

    #[test]
    fn test_is_box_corner_all_styles() {
        // Single line corners
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        // Double line corners
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        // Rounded corners
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
        assert!(is_box_corner(''));
    }

    #[test]
    fn test_is_double_line_corner() {
        assert!(is_double_line_corner(''));
        assert!(is_double_line_corner(''));
        assert!(is_double_line_corner(''));
        assert!(is_double_line_corner(''));
        assert!(!is_double_line_corner(''));
        assert!(!is_double_line_corner(''));
    }

    #[test]
    fn test_is_rounded_corner() {
        assert!(is_rounded_corner(''));
        assert!(is_rounded_corner(''));
        assert!(is_rounded_corner(''));
        assert!(is_rounded_corner(''));
        assert!(!is_rounded_corner(''));
        assert!(!is_rounded_corner(''));
    }

    #[test]
    fn test_box_style_from_single_corner() {
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Single));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Single));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Single));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Single));
    }

    #[test]
    fn test_box_style_from_double_corner() {
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Double));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Double));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Double));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Double));
    }

    #[test]
    fn test_box_style_from_rounded_corner() {
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Rounded));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Rounded));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Rounded));
        assert_eq!(BoxStyle::from_corner(''), Some(BoxStyle::Rounded));
    }

    #[test]
    fn test_box_style_from_non_corner() {
        assert_eq!(BoxStyle::from_corner(''), None);
        assert_eq!(BoxStyle::from_corner(''), None);
        assert_eq!(BoxStyle::from_corner('a'), None);
    }

    #[test]
    fn test_box_has_style_field() {
        let b = Box {
            top_left: (0, 0),
            bottom_right: (3, 5),
            style: BoxStyle::Single,
            parent_idx: None,
            child_indices: Vec::new(),
        };
        assert_eq!(b.style, BoxStyle::Single);
    }

    #[test]
    fn test_box_style_preserved() {
        let b = Box {
            top_left: (1, 2),
            bottom_right: (5, 8),
            style: BoxStyle::Double,
            parent_idx: None,
            child_indices: Vec::new(),
        };
        assert_eq!(b.style, BoxStyle::Double);
    }

    #[test]
    fn test_arrow_type_standard_chars() {
        let chars = ArrowType::Standard.chars();
        assert_eq!(chars.line, '');
        assert_eq!(chars.arrowhead_right, '');
        assert_eq!(chars.arrowhead_left, '');
    }

    #[test]
    fn test_arrow_type_double_chars() {
        let chars = ArrowType::Double.chars();
        assert_eq!(chars.line, '');
        assert_eq!(chars.arrowhead_right, '');
        assert_eq!(chars.arrowhead_left, '');
    }

    #[test]
    fn test_arrow_type_long_chars() {
        let chars = ArrowType::Long.chars();
        assert_eq!(chars.line, '');
        assert_eq!(chars.arrowhead_right, '');
        assert_eq!(chars.arrowhead_left, '');
    }

    #[test]
    fn test_arrow_type_dashed_chars() {
        let chars = ArrowType::Dashed.chars();
        assert_eq!(chars.line, '·');
        assert_eq!(chars.arrowhead_right, '>');
        assert_eq!(chars.arrowhead_left, '<');
    }

    #[test]
    fn test_arrow_type_from_char() {
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Standard));
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Standard));
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Double));
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Double));
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Long));
        assert_eq!(ArrowType::from_char(''), Some(ArrowType::Long));
        assert_eq!(ArrowType::from_char('a'), None);
    }

    #[test]
    fn test_arrow_type_is_vertical_arrow_char() {
        assert!(is_vertical_arrow_char(''));
        assert!(is_vertical_arrow_char(''));
        assert!(is_vertical_arrow_char(''));
        assert!(is_vertical_arrow_char(''));
        assert!(is_vertical_arrow_char(''));
        assert!(is_vertical_arrow_char(''));
        assert!(!is_vertical_arrow_char(''));
        assert!(!is_vertical_arrow_char('a'));
    }

    #[test]
    fn test_horizontal_arrow_has_type_field() {
        let arr = HorizontalArrow {
            row: 5,
            start_col: 2,
            end_col: 8,
            arrow_type: ArrowType::Standard,
            rightward: true,
            arrow_char: None,
        };
        assert_eq!(arr.arrow_type, ArrowType::Standard);
        assert!(arr.rightward);
    }

    #[test]
    fn test_horizontal_arrow_leftward() {
        let arr = HorizontalArrow {
            row: 5,
            start_col: 8,
            end_col: 2,
            arrow_type: ArrowType::Double,
            rightward: false,
            arrow_char: Some(''),
        };
        assert_eq!(arr.arrow_type, ArrowType::Double);
        assert!(!arr.rightward);
    }

    #[test]
    fn test_vertical_arrow_has_type_field() {
        let arr = VerticalArrow {
            col: 3,
            start_row: 1,
            end_row: 6,
            arrow_type: ArrowType::Long,
            downward: true,
            arrow_char: Some(''),
        };
        assert_eq!(arr.arrow_type, ArrowType::Long);
        assert!(arr.downward);
    }

    #[test]
    fn test_vertical_arrow_upward() {
        let arr = VerticalArrow {
            col: 3,
            start_row: 6,
            end_row: 1,
            arrow_type: ArrowType::Dashed,
            downward: false,
            arrow_char: Some(''),
        };
        assert_eq!(arr.arrow_type, ArrowType::Dashed);
        assert!(!arr.downward);
    }

    #[test]
    fn test_segment_horizontal() {
        let seg = Segment::Horizontal {
            row: 5,
            start_col: 2,
            end_col: 8,
        };
        match seg {
            Segment::Horizontal {
                row,
                start_col,
                end_col,
            } => {
                assert_eq!(row, 5);
                assert_eq!(start_col, 2);
                assert_eq!(end_col, 8);
            }
            Segment::Vertical { .. } => panic!("Expected Horizontal segment"),
        }
    }

    #[test]
    fn test_segment_vertical() {
        let seg = Segment::Vertical {
            col: 3,
            start_row: 1,
            end_row: 6,
        };
        match seg {
            Segment::Vertical {
                col,
                start_row,
                end_row,
            } => {
                assert_eq!(col, 3);
                assert_eq!(start_row, 1);
                assert_eq!(end_row, 6);
            }
            Segment::Horizontal { .. } => panic!("Expected Vertical segment"),
        }
    }

    #[test]
    fn test_connection_line_basic() {
        let conn = ConnectionLine {
            segments: vec![
                Segment::Horizontal {
                    row: 2,
                    start_col: 0,
                    end_col: 5,
                },
                Segment::Vertical {
                    col: 5,
                    start_row: 2,
                    end_row: 5,
                },
            ],
            from_box: Some(0),
            to_box: Some(1),
        };
        assert_eq!(conn.segments.len(), 2);
        assert_eq!(conn.from_box, Some(0));
        assert_eq!(conn.to_box, Some(1));
    }

    #[test]
    fn test_connection_line_unattached() {
        let conn = ConnectionLine {
            segments: vec![Segment::Horizontal {
                row: 3,
                start_col: 0,
                end_col: 10,
            }],
            from_box: None,
            to_box: None,
        };
        assert_eq!(conn.segments.len(), 1);
        assert!(conn.from_box.is_none());
        assert!(conn.to_box.is_none());
    }

    #[test]
    fn test_connection_line_single_segment() {
        let conn = ConnectionLine {
            segments: vec![Segment::Vertical {
                col: 2,
                start_row: 0,
                end_row: 4,
            }],
            from_box: Some(0),
            to_box: None,
        };
        assert_eq!(conn.segments.len(), 1);
        assert_eq!(conn.from_box, Some(0));
    }

    #[test]
    fn test_label_attachment_box() {
        let attachment = LabelAttachment::Box(0);
        assert_eq!(attachment, LabelAttachment::Box(0));
    }

    #[test]
    fn test_label_attachment_horizontal_arrow() {
        let attachment = LabelAttachment::HorizontalArrow(2);
        assert_eq!(attachment, LabelAttachment::HorizontalArrow(2));
    }

    #[test]
    fn test_label_attachment_vertical_arrow() {
        let attachment = LabelAttachment::VerticalArrow(1);
        assert_eq!(attachment, LabelAttachment::VerticalArrow(1));
    }

    #[test]
    fn test_label_attachment_connection_line() {
        let attachment = LabelAttachment::ConnectionLine(3);
        assert_eq!(attachment, LabelAttachment::ConnectionLine(3));
    }

    #[test]
    fn test_label_basic() {
        let label = Label {
            row: 5,
            col: 10,
            content: "Input".to_string(),
            attached_to: LabelAttachment::Box(0),
            offset: (-1, 2),
        };
        assert_eq!(label.row, 5);
        assert_eq!(label.col, 10);
        assert_eq!(label.content, "Input");
        assert_eq!(label.offset, (-1, 2));
    }

    #[test]
    fn test_label_with_arrow_attachment() {
        let label = Label {
            row: 3,
            col: 8,
            content: "Process".to_string(),
            attached_to: LabelAttachment::HorizontalArrow(1),
            offset: (0, -3),
        };
        assert_eq!(label.attached_to, LabelAttachment::HorizontalArrow(1));
        assert_eq!(label.offset, (0, -3));
    }

    #[test]
    fn test_label_negative_offsets() {
        let label = Label {
            row: 2,
            col: 4,
            content: "Start".to_string(),
            attached_to: LabelAttachment::Box(1),
            offset: (-2, -1),
        };
        assert!(label.offset.0 < 0);
        assert!(label.offset.1 < 0);
    }

    #[test]
    fn test_label_large_offset() {
        let label = Label {
            row: 10,
            col: 20,
            content: "Far away".to_string(),
            attached_to: LabelAttachment::ConnectionLine(2),
            offset: (5, 8),
        };
        assert_eq!(label.offset, (5, 8));
    }
}