#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphCell {
Empty,
Vertical { color_idx: usize },
Node { color_idx: usize },
HeadNode { color_idx: usize },
MergeNode { color_idx: usize },
CurveUpRight { color_idx: usize },
CurveUpLeft { color_idx: usize },
CurveDownRight { color_idx: usize },
CurveDownLeft { color_idx: usize },
Horizontal { color_idx: usize },
Cross { h_color: usize, v_color: usize },
TeeRight { color_idx: usize },
TeeLeft { color_idx: usize },
TeeUp { color_idx: usize },
}
impl GraphCell {
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 { .. } => '┴',
}
}
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)
);
}
}