a1_notation/range_or_cell/
display.rs

1use super::RangeOrCell;
2use std::fmt;
3
4impl fmt::Display for RangeOrCell {
5    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6        match self {
7            Self::Cell(p) => write!(f, "{p}"),
8            Self::ColumnRange { from, to } => write!(f, "{from}:{to}"),
9            Self::NonContiguous(range_or_cells) => {
10                let joined_range_or_cells = range_or_cells
11                    .iter()
12                    .map(|r| r.to_string())
13                    .collect::<Vec<_>>()
14                    .join(", ");
15
16                write!(f, "{joined_range_or_cells}")
17            }
18            Self::Range { from, to } => write!(f, "{from}:{to}"),
19            Self::RowRange { from, to } => write!(f, "{from}:{to}"),
20        }
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use crate::*;
27
28    #[test]
29    fn display_cell() {
30        assert_eq!(RangeOrCell::Cell(Address::new(0, 0)).to_string(), "A1");
31
32        assert_eq!(
33            RangeOrCell::Cell(Address::new(100, 100)).to_string(),
34            "CW101"
35        );
36    }
37
38    #[test]
39    fn display_column_range() {
40        assert_eq!(
41            RangeOrCell::ColumnRange {
42                from: Column::new(0),
43                to: Column::new(10)
44            }
45            .to_string(),
46            "A:K"
47        );
48
49        assert_eq!(
50            RangeOrCell::ColumnRange {
51                from: Column::new(5),
52                to: Column::new(5)
53            }
54            .to_string(),
55            "F:F"
56        );
57    }
58
59    #[test]
60    fn display_non_contiguous() {
61        assert_eq!(
62            RangeOrCell::NonContiguous(vec![
63                RangeOrCell::Cell(Address::new(0, 0)),
64                RangeOrCell::ColumnRange {
65                    from: Column::new(0),
66                    to: Column::new(10)
67                },
68                RangeOrCell::Range {
69                    from: Address::new(0, 0),
70                    to: Address::new(10, 10)
71                },
72            ])
73            .to_string(),
74            "A1, A:K, A1:K11"
75        );
76    }
77
78    #[test]
79    fn display_range() {
80        assert_eq!(
81            RangeOrCell::Range {
82                from: Address::new(0, 0),
83                to: Address::new(10, 10)
84            }
85            .to_string(),
86            "A1:K11"
87        );
88    }
89
90    #[test]
91    fn display_row_range() {
92        assert_eq!(
93            RangeOrCell::RowRange {
94                from: Row::new(0),
95                to: Row::new(10)
96            }
97            .to_string(),
98            "1:11"
99        );
100
101        assert_eq!(
102            RangeOrCell::RowRange {
103                from: Row::new(5),
104                to: Row::new(5)
105            }
106            .to_string(),
107            "6:6"
108        );
109    }
110}