diff --git a/src/hugr/views.rs b/src/hugr/views.rs index 9de439132..0a7d76636 100644 --- a/src/hugr/views.rs +++ b/src/hugr/views.rs @@ -2,7 +2,7 @@ pub mod descendants; pub mod petgraph; -mod render; +pub mod render; mod root_checked; pub mod sibling; pub mod sibling_subgraph; @@ -13,6 +13,7 @@ mod tests; use std::iter::Map; pub use self::petgraph::PetgraphWrapper; +use self::render::RenderConfig; pub use descendants::DescendantsGraph; pub use root_checked::RootChecked; pub use sibling::SiblingGraph; @@ -356,6 +357,23 @@ pub trait HugrView: sealed::HugrInternals { /// For a more detailed representation, use the [`HugrView::dot_string`] /// format instead. fn mermaid_string(&self) -> String + where + Self: Sized, + { + self.mermaid_string_with_config(RenderConfig { + port_offsets_in_edges: true, + type_labels_in_edges: true, + }) + } + + /// Return the mermaid representation of the underlying hierarchical graph. + /// + /// The hierarchy is represented using subgraphs. Edges are labelled with + /// their source and target ports. + /// + /// For a more detailed representation, use the [`HugrView::dot_string`] + /// format instead. + fn mermaid_string_with_config(&self, config: RenderConfig) -> String where Self: Sized, { @@ -365,7 +383,7 @@ pub trait HugrView: sealed::HugrInternals { .mermaid_format() .with_hierarchy(&hugr.hierarchy) .with_node_style(render::node_style(self)) - .with_edge_style(render::edge_style(self, true, true)) + .with_edge_style(render::edge_style(self, config)) .finish() } @@ -378,12 +396,13 @@ pub trait HugrView: sealed::HugrInternals { { let hugr = self.base_hugr(); let graph = self.portgraph(); + let config = RenderConfig::default(); graph .dot_format() .with_hierarchy(&hugr.hierarchy) .with_node_style(render::node_style(self)) .with_port_style(render::port_style(self)) - .with_edge_style(render::edge_style(self, false, false)) + .with_edge_style(render::edge_style(self, config)) .finish() } diff --git a/src/hugr/views/render.rs b/src/hugr/views/render.rs index 300ef5d07..aae034cb4 100644 --- a/src/hugr/views/render.rs +++ b/src/hugr/views/render.rs @@ -8,6 +8,16 @@ use crate::ops::OpName; use crate::types::EdgeKind; use crate::HugrView; +/// Configuration for rendering a HUGR graph. +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub struct RenderConfig { + /// Show port offsets in the graph edges. + pub port_offsets_in_edges: bool, + /// Show type labels on edges. + pub type_labels_in_edges: bool, +} + /// Formatter method to compute a node style. pub(super) fn node_style(h: &H) -> Box NodeStyle + '_> { Box::new(move |n| { @@ -42,8 +52,7 @@ pub(super) fn port_style(h: &H) -> Box Port #[allow(clippy::type_complexity)] pub(super) fn edge_style( h: &H, - port_offsets_in_edges: bool, - type_labels_in_edges: bool, + config: RenderConfig, ) -> Box< dyn FnMut( as LinkView>::LinkEndpoint, @@ -72,7 +81,11 @@ pub(super) fn edge_style( // Compute the label for the edge, given the setting flags. // // Only static and value edges have types to display. - let label = match (port_offsets_in_edges, type_labels_in_edges, port_kind) { + let label = match ( + config.port_offsets_in_edges, + config.type_labels_in_edges, + port_kind, + ) { (true, true, EdgeKind::Static(ty) | EdgeKind::Value(ty)) => { format!("{}:{}\n{ty}", src_offset.index(), tgt_offset.index()) }