Skip to main content

oxiui_table/
align.rs

1//! Per-column cell alignment.
2
3use crate::Cell;
4
5/// Horizontal alignment of a cell's content within its column.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
7pub enum CellAlign {
8    /// Align content to the left (default for text).
9    #[default]
10    Left,
11    /// Centre content horizontally.
12    Center,
13    /// Align content to the right (default for numeric cells).
14    Right,
15}
16
17impl CellAlign {
18    /// The conventional default alignment for a given cell: numbers are
19    /// right-aligned, booleans centred, everything else left-aligned.
20    pub fn default_for(cell: &Cell) -> CellAlign {
21        match cell {
22            Cell::Int(_) | Cell::Float(_) => CellAlign::Right,
23            Cell::Bool(_) => CellAlign::Center,
24            _ => CellAlign::Left,
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn numeric_defaults_right() {
35        assert_eq!(CellAlign::default_for(&Cell::Int(1)), CellAlign::Right);
36        assert_eq!(CellAlign::default_for(&Cell::Float(1.5)), CellAlign::Right);
37    }
38
39    #[test]
40    fn bool_defaults_center() {
41        assert_eq!(CellAlign::default_for(&Cell::Bool(true)), CellAlign::Center);
42    }
43
44    #[test]
45    fn text_and_empty_default_left() {
46        assert_eq!(CellAlign::default_for(&Cell::from("x")), CellAlign::Left);
47        assert_eq!(CellAlign::default_for(&Cell::Empty), CellAlign::Left);
48        assert_eq!(CellAlign::default(), CellAlign::Left);
49    }
50}