use super::cell::GraphCell;
use crate::event::GitEvent;
#[derive(Debug, Clone)]
pub struct GraphRow {
pub event: Option<GitEvent>,
pub column: usize,
pub color_idx: usize,
pub cells: Vec<GraphCell>,
pub is_head: bool,
pub branch_names: Vec<String>,
}
impl GraphRow {
pub fn new(
event: Option<GitEvent>,
column: usize,
color_idx: usize,
cells: Vec<GraphCell>,
) -> Self {
Self {
event,
column,
color_idx,
cells,
is_head: false,
branch_names: Vec::new(),
}
}
pub fn with_head(mut self, is_head: bool) -> Self {
self.is_head = is_head;
self
}
pub fn with_branches(mut self, names: Vec<String>) -> Self {
self.branch_names = names;
self
}
}
#[derive(Debug, Clone)]
pub struct GraphLayout {
pub rows: Vec<GraphRow>,
pub max_column: usize,
}
impl GraphLayout {
pub fn empty() -> Self {
Self {
rows: Vec::new(),
max_column: 0,
}
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub fn get(&self, idx: usize) -> Option<&GraphRow> {
self.rows.get(idx)
}
pub fn iter(&self) -> impl Iterator<Item = &GraphRow> {
self.rows.iter()
}
}
impl Default for GraphLayout {
fn default() -> Self {
Self::empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph_row_new() {
let row = GraphRow::new(None, 0, 1, vec![GraphCell::Empty]);
assert!(row.event.is_none());
assert_eq!(row.column, 0);
assert_eq!(row.color_idx, 1);
assert!(!row.is_head);
}
#[test]
fn test_graph_row_with_head() {
let row = GraphRow::new(None, 0, 0, vec![]).with_head(true);
assert!(row.is_head);
}
#[test]
fn test_graph_row_with_branches() {
let row = GraphRow::new(None, 0, 0, vec![]).with_branches(vec!["main".to_string()]);
assert_eq!(row.branch_names, vec!["main"]);
}
#[test]
fn test_graph_layout_empty() {
let layout = GraphLayout::empty();
assert!(layout.is_empty());
assert_eq!(layout.len(), 0);
assert_eq!(layout.max_column, 0);
}
#[test]
fn test_graph_layout_get() {
let mut layout = GraphLayout::empty();
layout.rows.push(GraphRow::new(None, 0, 0, vec![]));
assert!(layout.get(0).is_some());
assert!(layout.get(1).is_none());
}
}