gitstack 5.3.0

Git history viewer with insights - Author stats, file heatmap, code ownership
Documentation
//! Graph cell definitions

/// Types of each cell in the graph
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphCell {
    /// Empty space
    Empty,
    /// Vertical line continuation │
    Vertical { color_idx: usize },
    /// Commit node ●
    Node { color_idx: usize },
    /// HEAD node ◉
    HeadNode { color_idx: usize },
    /// Merge node ◆
    MergeNode { color_idx: usize },
    /// Upper-right curve ╭ (branch from bottom to right)
    CurveUpRight { color_idx: usize },
    /// Upper-left curve ╮ (branch from bottom to left)
    CurveUpLeft { color_idx: usize },
    /// Lower-right curve ╰ (merge from top to right)
    CurveDownRight { color_idx: usize },
    /// Lower-left curve ╯ (merge from top to left)
    CurveDownLeft { color_idx: usize },
    /// Horizontal line ─
    Horizontal { color_idx: usize },
    /// Crossing ┼
    Cross { h_color: usize, v_color: usize },
    /// Tee right ├
    TeeRight { color_idx: usize },
    /// Tee left ┤
    TeeLeft { color_idx: usize },
    /// Tee up ┴
    TeeUp { color_idx: usize },
}

impl GraphCell {
    /// Convert cell to character
    pub fn to_char(&self) -> char {
        match self {
            GraphCell::Empty => ' ',
            GraphCell::Vertical { .. } => '',
            GraphCell::Node { .. } => '',
            GraphCell::HeadNode { .. } => '',
            GraphCell::MergeNode { .. } => '',
            GraphCell::CurveUpRight { .. } => '',
            GraphCell::CurveUpLeft { .. } => '',
            GraphCell::CurveDownRight { .. } => '',
            GraphCell::CurveDownLeft { .. } => '',
            GraphCell::Horizontal { .. } => '',
            GraphCell::Cross { .. } => '',
            GraphCell::TeeRight { .. } => '',
            GraphCell::TeeLeft { .. } => '',
            GraphCell::TeeUp { .. } => '',
        }
    }

    /// Get the color index of the cell (primary color)
    pub fn color_index(&self) -> Option<usize> {
        match self {
            GraphCell::Empty => None,
            GraphCell::Vertical { color_idx }
            | GraphCell::Node { color_idx }
            | GraphCell::HeadNode { color_idx }
            | GraphCell::MergeNode { color_idx }
            | GraphCell::CurveUpRight { color_idx }
            | GraphCell::CurveUpLeft { color_idx }
            | GraphCell::CurveDownRight { color_idx }
            | GraphCell::CurveDownLeft { color_idx }
            | GraphCell::Horizontal { color_idx }
            | GraphCell::TeeRight { color_idx }
            | GraphCell::TeeLeft { color_idx }
            | GraphCell::TeeUp { color_idx } => Some(*color_idx),
            GraphCell::Cross { v_color, .. } => Some(*v_color),
        }
    }
}

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

    #[test]
    fn test_cell_to_char_empty() {
        assert_eq!(GraphCell::Empty.to_char(), ' ');
    }

    #[test]
    fn test_cell_to_char_vertical() {
        assert_eq!(GraphCell::Vertical { color_idx: 0 }.to_char(), '');
    }

    #[test]
    fn test_cell_to_char_node() {
        assert_eq!(GraphCell::Node { color_idx: 0 }.to_char(), '');
    }

    #[test]
    fn test_cell_to_char_merge_node() {
        assert_eq!(GraphCell::MergeNode { color_idx: 0 }.to_char(), '');
    }

    #[test]
    fn test_cell_to_char_curves() {
        assert_eq!(GraphCell::CurveUpRight { color_idx: 0 }.to_char(), '');
        assert_eq!(GraphCell::CurveUpLeft { color_idx: 0 }.to_char(), '');
        assert_eq!(GraphCell::CurveDownRight { color_idx: 0 }.to_char(), '');
        assert_eq!(GraphCell::CurveDownLeft { color_idx: 0 }.to_char(), '');
    }

    #[test]
    fn test_cell_color_index() {
        assert_eq!(GraphCell::Empty.color_index(), None);
        assert_eq!(GraphCell::Node { color_idx: 3 }.color_index(), Some(3));
        assert_eq!(
            GraphCell::Cross {
                h_color: 1,
                v_color: 2
            }
            .color_index(),
            Some(2)
        );
    }
}