-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renames the `dot` module to `render`, and adds a mermaid formatter using the same style configuration structs. BREAKING CHANGE: Moved `portgraph::dot` to `portgraph::render`
- Loading branch information
Showing
4 changed files
with
566 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
//! This module contains rendering logic from portgraphs into graphviz and | ||
//! mermaid diagrams. | ||
mod dot; | ||
mod mermaid; | ||
|
||
use std::borrow::Cow; | ||
|
||
pub use dot::{DotFormat, DotFormatter}; | ||
pub use mermaid::{MermaidFormat, MermaidFormatter}; | ||
|
||
use self::mermaid::encode_label; | ||
|
||
/// Style of a rendered edge. | ||
/// | ||
/// Defaults to a box with no label. | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub enum NodeStyle { | ||
/// Ignore the node. No edges will be connected to it. | ||
Hidden, | ||
/// Draw a box with the label inside. | ||
Box(String), | ||
} | ||
|
||
impl NodeStyle { | ||
/// Show a node label with the default style. | ||
pub fn new(label: impl ToString) -> Self { | ||
Self::Box(label.to_string()) | ||
} | ||
} | ||
|
||
impl Default for NodeStyle { | ||
fn default() -> Self { | ||
Self::Box(String::new()) | ||
} | ||
} | ||
|
||
/// Style of an edge in a rendered graph. Defaults to a box with no label. | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub enum PortStyle { | ||
/// Do not draw a label. Edges will be connected to the node. | ||
Hidden, | ||
/// Just the port label. Optionally prepend the port index. | ||
Plain(String, bool), | ||
/// Draw a box around the label. Optionally prepend the port index. | ||
Boxed(String, bool), | ||
} | ||
|
||
impl PortStyle { | ||
/// Show a port label with the default style. | ||
pub fn new(label: impl ToString) -> Self { | ||
Self::Boxed(label.to_string(), true) | ||
} | ||
|
||
/// Just the port label. Optionally prepend the port index. | ||
pub fn text(label: impl ToString, show_offset: bool) -> Self { | ||
Self::Plain(label.to_string(), show_offset) | ||
} | ||
|
||
/// Draw a box around the label. Optionally prepend the port index. | ||
pub fn boxed(label: impl ToString, show_offset: bool) -> Self { | ||
Self::Boxed(label.to_string(), show_offset) | ||
} | ||
} | ||
|
||
impl Default for PortStyle { | ||
fn default() -> Self { | ||
Self::Boxed(String::new(), true) | ||
} | ||
} | ||
|
||
/// Style of an edge in a rendered graph. Defaults to [`EdgeStyle::Solid`]. | ||
#[derive(Clone, Debug, PartialEq, Eq, Default)] | ||
#[non_exhaustive] | ||
pub enum EdgeStyle { | ||
/// Normal line | ||
#[default] | ||
Solid, | ||
/// Dotted line | ||
Dotted, | ||
/// Dashed line | ||
Dashed, | ||
/// Edge style with a label | ||
Labelled(String, Box<EdgeStyle>), | ||
/// Custom style | ||
Custom(String), | ||
} | ||
|
||
impl EdgeStyle { | ||
/// Adds a label to the edge style. | ||
/// | ||
/// If the edge style already has a label, it will be replaced. | ||
pub fn with_label(self, label: impl ToString) -> Self { | ||
match self { | ||
Self::Labelled(_, e) => Self::Labelled(label.to_string(), e), | ||
_ => Self::Labelled(label.to_string(), Box::new(self)), | ||
} | ||
} | ||
|
||
/// Returns the base style of the edge, without labels. | ||
pub fn strip_label(&self) -> &Self { | ||
match self { | ||
Self::Labelled(_, e) => e.strip_label(), | ||
e => e, | ||
} | ||
} | ||
|
||
/// Get the style as a graphviz style string | ||
pub(super) fn as_dot_str(&self) -> &str { | ||
match self { | ||
Self::Solid => "", | ||
Self::Dotted => "dotted", | ||
Self::Dashed => "dashed", | ||
Self::Custom(s) => s, | ||
// Ignore edge labels. | ||
Self::Labelled(_lbl, e) => e.as_dot_str(), | ||
} | ||
} | ||
|
||
/// Get the style as a graphviz style string | ||
pub(super) fn as_mermaid_str(&self) -> Cow<'_, str> { | ||
match self { | ||
Self::Solid => "-->".into(), | ||
Self::Dotted => "-.->".into(), | ||
// Dashed links are not supported in mermaid, we use dots instead. | ||
Self::Dashed => "-.->".into(), | ||
Self::Custom(s) => s.into(), | ||
Self::Labelled(lbl, e) => { | ||
let lbl = encode_label("", lbl); | ||
match e.strip_label() { | ||
Self::Solid => format!("--{}-->", lbl).into(), | ||
Self::Dotted => format!("-.{}.->", lbl).into(), | ||
Self::Dashed => format!("-.{}.->", lbl).into(), | ||
Self::Custom(s) => s.into(), | ||
Self::Labelled(_, _) => { | ||
unreachable!("`strip_label` cannot return a `Labelled`") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.