Skip to content

Commit

Permalink
Optional render config
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Mar 5, 2024
1 parent 8573936 commit deabfd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/hugr/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
{
Expand All @@ -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()
}

Expand All @@ -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()
}

Expand Down
19 changes: 16 additions & 3 deletions src/hugr/views/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: HugrView>(h: &H) -> Box<dyn FnMut(NodeIndex) -> NodeStyle + '_> {
Box::new(move |n| {
Expand Down Expand Up @@ -42,8 +52,7 @@ pub(super) fn port_style<H: HugrView>(h: &H) -> Box<dyn FnMut(PortIndex) -> Port
#[allow(clippy::type_complexity)]
pub(super) fn edge_style<H: HugrView>(
h: &H,
port_offsets_in_edges: bool,
type_labels_in_edges: bool,
config: RenderConfig,
) -> Box<
dyn FnMut(
<H::Portgraph<'_> as LinkView>::LinkEndpoint,
Expand Down Expand Up @@ -72,7 +81,11 @@ pub(super) fn edge_style<H: HugrView>(
// 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())
}
Expand Down

0 comments on commit deabfd7

Please sign in to comment.