#[cfg(feature = "serialize")]
use serde::Serialize;
pub use crate::render::Ancestor;
pub use crate::render::LinkLine;
pub use crate::render::NodeLine;
pub use crate::render::PadLine;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct GraphRowShapeStyle {
pub dash_style: DashStyle,
pub link_line_placement: LinkLinePlacement,
}
impl Default for GraphRowShapeStyle {
fn default() -> Self {
Self {
dash_style: DashStyle::Persistent,
link_line_placement: LinkLinePlacement::Top,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub enum DashStyle {
Persistent,
Once,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub enum LinkLinePlacement {
#[default]
Top,
Bottom,
}
pub type GraphRowShapeOptions = crate::OutputRendererOptions;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct GraphRowShape<N> {
pub node: N,
pub merge: bool,
pub separator_line: bool,
pub style: GraphRowShapeStyle,
pub is_head: bool,
pub is_root: bool,
pub node_column: usize,
pub parent_columns: Vec<usize>,
pub node_line: Vec<NodeLine>,
pub link_line: Option<Vec<LinkLine>>,
pub link_line_from_node: Option<Vec<LinkLine>>,
pub term_line: Option<Vec<bool>>,
pub ancestry_line: Vec<PadLine>,
}
impl<N> GraphRowShape<N> {
pub fn has_indirect_ancestor(&self) -> bool {
self.ancestry_line
.iter()
.any(|line| *line == PadLine::Ancestor)
}
pub fn pre_node_line(&self) -> Vec<PadLine> {
self.node_line
.iter()
.map(|line| node_to_pad_line(*line, self.is_head))
.collect()
}
pub fn post_node_line(&self) -> Vec<PadLine> {
self.node_line
.iter()
.map(|line| node_to_pad_line(*line, self.is_root))
.collect()
}
pub fn post_ancestry_line(&self) -> Vec<PadLine> {
self.ancestry_line
.iter()
.map(|line| match line {
PadLine::Ancestor => PadLine::Parent,
other => *other,
})
.collect()
}
}
fn node_to_pad_line(line: NodeLine, use_blank_for_node: bool) -> PadLine {
match line {
NodeLine::Blank => PadLine::Blank,
NodeLine::Ancestor => PadLine::Ancestor,
NodeLine::Parent => PadLine::Parent,
NodeLine::Node => {
if use_blank_for_node {
PadLine::Blank
} else {
PadLine::Parent
}
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrefixLine {
pub parts: Vec<PrefixLinePart>,
pub kind: PrefixLineKind,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PrefixLinePart {
Text(String),
NodeGlyph,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub enum PrefixLineKind {
Separator,
PreNode,
Node,
PostNode,
Link,
Term,
Ancestry,
PostAncestry,
}
impl PrefixLineKind {
pub fn is_repeatable(self) -> bool {
matches!(
self,
PrefixLineKind::PreNode | PrefixLineKind::PostNode | PrefixLineKind::PostAncestry
)
}
}
pub trait PrefixLineRenderer<N> {
fn next_prefix_lines(&mut self, row_shape: &GraphRowShape<N>) -> Vec<PrefixLine>;
}