Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node_{width,height}{s,_default} and fixed_size #37

Merged
merged 8 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## unreleased

* Support `node_width_default` in `GraphvizAttrs`.
* Support `node_widths` in `GraphvizAttrs`.
* Support `node_height_default` in `GraphvizAttrs`.
* Support `node_heights` in `GraphvizAttrs`.
* Support `fixed_size` in `GraphvizAttrs`.


## 0.8.1 (2024-09-27)

* Support inline images ([#33]).
Expand Down
148 changes: 147 additions & 1 deletion crate/model/src/common/graphviz_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

pub use self::{
edge_constraints::EdgeConstraints, edge_dir::EdgeDir, edge_dirs::EdgeDirs,
edge_minlens::EdgeMinlens, pack_mode::PackMode, pack_mode_flag::PackModeFlag, splines::Splines,
edge_minlens::EdgeMinlens, fixed_size::FixedSize, node_heights::NodeHeights,
node_widths::NodeWidths, pack_mode::PackMode, pack_mode_flag::PackModeFlag, splines::Splines,
};

mod edge_constraints;
mod edge_dir;
mod edge_dirs;
mod edge_minlens;
mod fixed_size;
mod node_heights;
mod node_widths;
mod pack_mode;
mod pack_mode_flag;
mod splines;
Expand Down Expand Up @@ -65,6 +69,40 @@
///
/// [`minlen`]: https://graphviz.org/docs/attrs/minlen/
pub edge_minlens: EdgeMinlens,
/// Minimum / initial [`width`] for nodes, defaults to `0.3`.
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub node_width_default: f64,
/// Each node's [`width`].
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub node_widths: NodeWidths,
/// Minimum / initial [`height`] for nodes, defaults to `0.1`.
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub node_height_default: f64,
/// Each node's [`height`].
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub node_heights: NodeHeights,
/// Whether a node's `width` and `height` are fixed dimensions.
///
/// See [`fixedsize`].
///
/// [`fixedsize`]: https://graphviz.org/docs/attrs/fixedsize/
pub fixed_size: FixedSize,
/// How closely to pack together graph components.
pub pack_mode: PackMode,
}
Expand Down Expand Up @@ -159,6 +197,60 @@
self
}

/// Sets the minimum / initial [`width`] for nodes, defaults to `0.3`.
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub fn with_node_width_default(mut self, node_width_default: f64) -> Self {
self.node_width_default = node_width_default;
self
}

Check warning on line 209 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L206-L209

Added lines #L206 - L209 were not covered by tests

/// Sets each node's [`width`].
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub fn with_node_widths(mut self, node_widths: NodeWidths) -> Self {
self.node_widths = node_widths;
self
}

Check warning on line 220 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L217-L220

Added lines #L217 - L220 were not covered by tests

/// Sets the minimum / initial [`height`] for nodes, defaults to `0.1`.
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub fn with_node_height_default(mut self, node_height_default: f64) -> Self {
self.node_height_default = node_height_default;
self
}

Check warning on line 231 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L228-L231

Added lines #L228 - L231 were not covered by tests

/// Sets each node's [`height`].
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub fn with_node_heights(mut self, node_heights: NodeHeights) -> Self {
self.node_heights = node_heights;
self
}

Check warning on line 242 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L239-L242

Added lines #L239 - L242 were not covered by tests

/// Sets whether a node's `width` and `height` are fixed dimensions.
///
/// See [`fixedsize`].
///
/// [`fixedsize`]: https://graphviz.org/docs/attrs/fixedsize/
pub fn with_fixed_size(mut self, fixed_size: FixedSize) -> Self {
self.fixed_size = fixed_size;
self
}

Check warning on line 252 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L249-L252

Added lines #L249 - L252 were not covered by tests

/// Returns the minimum space between two adjacent nodes in the same rank,
/// in inches. Also controls the spacing between multiple edges between
/// the same pair of nodes.
Expand Down Expand Up @@ -230,6 +322,55 @@
pub fn edge_minlens(&self) -> &EdgeMinlens {
&self.edge_minlens
}

/// Returns the minimum / initial [`width`] for nodes.
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub fn node_width_default(&self) -> f64 {
self.node_width_default
}

Check warning on line 334 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L332-L334

Added lines #L332 - L334 were not covered by tests

/// Returns each node's [`width`].
///
/// If `fixedsize` is true, this will be the exact / maximum width for
/// nodes.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
pub fn node_widths(&self) -> &NodeWidths {
&self.node_widths
}

Check warning on line 344 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L342-L344

Added lines #L342 - L344 were not covered by tests

/// Returns the minimum / initial [`height`] for nodes.
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub fn node_height_default(&self) -> f64 {
self.node_height_default
}

Check warning on line 354 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L352-L354

Added lines #L352 - L354 were not covered by tests

/// Returns each node's [`height`].
///
/// If `fixedsize` is true, this will be the exact / maximum height for
/// nodes.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
pub fn node_heights(&self) -> &NodeHeights {
&self.node_heights
}

Check warning on line 364 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L362-L364

Added lines #L362 - L364 were not covered by tests

/// Returns whether a node's `width` and `height` are fixed dimensions.
///
/// See [`fixedsize`].
///
/// [`fixedsize`]: https://graphviz.org/docs/attrs/fixedsize/
pub fn fixed_size(&self) -> FixedSize {
self.fixed_size
}

Check warning on line 373 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L371-L373

Added lines #L371 - L373 were not covered by tests
}

impl Default for GraphvizAttrs {
Expand All @@ -244,6 +385,11 @@
edge_dirs: EdgeDirs::default(),
edge_minlen_default: 2,
edge_minlens: EdgeMinlens::default(),
node_width_default: 0.3,
node_widths: NodeWidths::default(),
node_height_default: 0.1,
node_heights: NodeHeights::default(),
fixed_size: FixedSize::default(),

Check warning on line 392 in crate/model/src/common/graphviz_attrs.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs.rs#L388-L392

Added lines #L388 - L392 were not covered by tests
pack_mode: PackMode::default(),
}
}
Expand Down
33 changes: 33 additions & 0 deletions crate/model/src/common/graphviz_attrs/fixed_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::fmt;

use serde::{Deserialize, Serialize};

/// Whether a node's `width` and `height` are fixed dimensions.
///
/// See [`fixedsize`].
///
/// [`fixedsize`]: https://graphviz.org/docs/attrs/fixedsize/
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]

Check warning on line 10 in crate/model/src/common/graphviz_attrs/fixed_size.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/fixed_size.rs#L10

Added line #L10 was not covered by tests
#[serde(rename_all = "snake_case")]
pub enum FixedSize {
/// Nodes are not fixed size, and `width`/`height` indicate their minimum
/// dimensions.
#[default]
False,
/// Nodes are fixed size, and `width`/`height` indicate their maximum
/// dimensions.
True,
/// `width` and `height` determine the dimensions of the node's shape, but
/// not its label.
Shape,
}

impl fmt::Display for FixedSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FixedSize::False => write!(f, "false"),
FixedSize::True => write!(f, "true"),
FixedSize::Shape => write!(f, "shape"),

Check warning on line 30 in crate/model/src/common/graphviz_attrs/fixed_size.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/fixed_size.rs#L26-L30

Added lines #L26 - L30 were not covered by tests
}
}

Check warning on line 32 in crate/model/src/common/graphviz_attrs/fixed_size.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/fixed_size.rs#L32

Added line #L32 was not covered by tests
}
59 changes: 59 additions & 0 deletions crate/model/src/common/graphviz_attrs/node_heights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::ops::{Deref, DerefMut};

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::common::NodeId;

/// GraphViz node height. `IndexMap<NodeId, f64>` newtype.
///
/// This is only used for GraphViz dot graphs, which sets the [`height`]
/// attribute for the node.
///
/// [`height`]: https://graphviz.org/docs/attrs/height/
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]

Check warning on line 14 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L14

Added line #L14 was not covered by tests
pub struct NodeHeights(IndexMap<NodeId, f64>);

impl NodeHeights {
/// Returns a new `NodeHeights` map.
pub fn new() -> Self {
Self::default()
}

Check warning on line 21 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L19-L21

Added lines #L19 - L21 were not covered by tests

/// Returns a new `NodeHeights` map with the given preallocated
/// capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self(IndexMap::with_capacity(capacity))
}

Check warning on line 27 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L25-L27

Added lines #L25 - L27 were not covered by tests

/// Returns the underlying map.
pub fn into_inner(self) -> IndexMap<NodeId, f64> {
self.0
}

Check warning on line 32 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

impl Deref for NodeHeights {
type Target = IndexMap<NodeId, f64>;

fn deref(&self) -> &Self::Target {
&self.0
}

Check warning on line 40 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L38-L40

Added lines #L38 - L40 were not covered by tests
}

impl DerefMut for NodeHeights {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}

Check warning on line 46 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
}

impl From<IndexMap<NodeId, f64>> for NodeHeights {
fn from(inner: IndexMap<NodeId, f64>) -> Self {
Self(inner)
}

Check warning on line 52 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L50-L52

Added lines #L50 - L52 were not covered by tests
}

impl FromIterator<(NodeId, f64)> for NodeHeights {
fn from_iter<I: IntoIterator<Item = (NodeId, f64)>>(iter: I) -> Self {
Self(IndexMap::from_iter(iter))
}

Check warning on line 58 in crate/model/src/common/graphviz_attrs/node_heights.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_heights.rs#L56-L58

Added lines #L56 - L58 were not covered by tests
}
59 changes: 59 additions & 0 deletions crate/model/src/common/graphviz_attrs/node_widths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::ops::{Deref, DerefMut};

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

use crate::common::NodeId;

/// GraphViz node width. `IndexMap<NodeId, f64>` newtype.
///
/// This is only used for GraphViz dot graphs, which sets the [`width`]
/// attribute for the node.
///
/// [`width`]: https://graphviz.org/docs/attrs/width/
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]

Check warning on line 14 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L14

Added line #L14 was not covered by tests
pub struct NodeWidths(IndexMap<NodeId, f64>);

impl NodeWidths {
/// Returns a new `NodeWidths` map.
pub fn new() -> Self {
Self::default()
}

Check warning on line 21 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L19-L21

Added lines #L19 - L21 were not covered by tests

/// Returns a new `NodeWidths` map with the given preallocated
/// capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self(IndexMap::with_capacity(capacity))
}

Check warning on line 27 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L25-L27

Added lines #L25 - L27 were not covered by tests

/// Returns the underlying map.
pub fn into_inner(self) -> IndexMap<NodeId, f64> {
self.0
}

Check warning on line 32 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

impl Deref for NodeWidths {
type Target = IndexMap<NodeId, f64>;

fn deref(&self) -> &Self::Target {
&self.0
}

Check warning on line 40 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L38-L40

Added lines #L38 - L40 were not covered by tests
}

impl DerefMut for NodeWidths {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}

Check warning on line 46 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L44-L46

Added lines #L44 - L46 were not covered by tests
}

impl From<IndexMap<NodeId, f64>> for NodeWidths {
fn from(inner: IndexMap<NodeId, f64>) -> Self {
Self(inner)
}

Check warning on line 52 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L50-L52

Added lines #L50 - L52 were not covered by tests
}

impl FromIterator<(NodeId, f64)> for NodeWidths {
fn from_iter<I: IntoIterator<Item = (NodeId, f64)>>(iter: I) -> Self {
Self(IndexMap::from_iter(iter))
}

Check warning on line 58 in crate/model/src/common/graphviz_attrs/node_widths.rs

View check run for this annotation

Codecov / codecov/patch

crate/model/src/common/graphviz_attrs/node_widths.rs#L56-L58

Added lines #L56 - L58 were not covered by tests
}
Loading