Dot

Struct Dot 

Source
pub struct Dot<'a, G>{ /* private fields */ }
Expand description

Dot implements output to graphviz .dot format for a graph.

Formatting and options are rather simple, this is mostly intended for debugging. Exact output may change.

§Examples

use petgraph::Graph;
use petgraph::dot::{Dot, Config};

let mut graph = Graph::<_, ()>::new();
graph.add_node("A");
graph.add_node("B");
graph.add_node("C");
graph.add_node("D");
graph.extend_with_edges(&[
    (0, 1), (0, 2), (0, 3),
    (1, 2), (1, 3),
    (2, 3),
]);

println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));

// In this case the output looks like this:
//
// digraph {
//     0 [label="\"A\""]
//     1 [label="\"B\""]
//     2 [label="\"C\""]
//     3 [label="\"D\""]
//     0 -> 1 [ ]
//     0 -> 2 [ ]
//     0 -> 3 [ ]
//     1 -> 2 [ ]
//     1 -> 3 [ ]
//     2 -> 3 [ ]
// }

// If you need multiple config options, just list them all in the slice.

Implementations§

Source§

impl<'a, G> Dot<'a, G>

Source

pub fn new(graph: G) -> Self

Create a Dot formatting wrapper with default configuration.

Source

pub fn with_config(graph: G, config: &'a [Config]) -> Self

Create a Dot formatting wrapper with custom configuration.

Source

pub fn with_attr_getters( graph: G, config: &'a [Config], get_edge_attributes: &'a dyn Fn(G, G::EdgeRef) -> String, get_node_attributes: &'a dyn Fn(G, G::NodeRef) -> String, ) -> Self

Create a Dot that uses the given functions to generate edge and node attributes.

NOTE: Config::EdgeNoLabel and Config::NodeNoLabel should be set if you intend to generate your own label attributes. The getter functions should return an attribute list as a String. For example, if you want to calculate a label for a node, then return "label = \"your label here\"". Each function should take as arguments the graph and that graph’s EdgeRef or NodeRef, respectively. Check the documentation for the graph type to see how it implements IntoNodeReferences. The Graphviz docs list the available attributes.

Note that some attribute values, such as labels, should be strings and must be quoted. These can be written using escapes ("label = \"foo\"") or raw strings (r#"label = "foo""#).

For example, using a Graph<&str, &str> where we want the node labels to be the nodes’ weights shortened to 4 characters, and all the edges are colored blue with no labels:

use petgraph::Graph;
use petgraph::dot::{Config, Dot};

let mut deps = Graph::<&str, &str>::new();
let pg = deps.add_node("petgraph");
let fb = deps.add_node("fixedbitset");
let qc = deps.add_node("quickcheck");
let rand = deps.add_node("rand");
let libc = deps.add_node("libc");
deps.extend_with_edges(&[(pg, fb), (pg, qc), (qc, rand), (rand, libc), (qc, libc)]);

println!(
    "{:?}",
    Dot::with_attr_getters(
        &deps,
        &[Config::EdgeNoLabel, Config::NodeNoLabel],
        &|_, _| "color = blue".to_string(),
        &|_, (_, s)| format!(r#"label = "{}""#, s.chars().take(4).collect::<String>()),
    )
);
// This outputs:
// digraph {
//     0 [ label = "petg"]
//     1 [ label = "fixe"]
//     2 [ label = "quic"]
//     3 [ label = "rand"]
//     4 [ label = "libc"]
//     0 -> 1 [ color = blue]
//     0 -> 2 [ color = blue]
//     2 -> 3 [ color = blue]
//     3 -> 4 [ color = blue]
//     2 -> 4 [ color = blue]
// }
Source§

impl<G> Dot<'_, G>

A low-level function allows specifying fmt functions for nodes and edges separately.

Source

pub fn graph_fmt<NF, EF>( &self, f: &mut Formatter<'_>, node_fmt: NF, edge_fmt: EF, ) -> Result
where NF: Fn(&G::NodeWeight, &mut Formatter<'_>) -> Result, EF: Fn(&G::EdgeWeight, &mut Formatter<'_>) -> Result,

Trait Implementations§

Source§

impl<G> Debug for Dot<'_, G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<G> Display for Dot<'_, G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<G> LowerHex for Dot<'_, G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<G> UpperHex for Dot<'_, G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, G> Freeze for Dot<'a, G>
where G: Freeze,

§

impl<'a, G> !RefUnwindSafe for Dot<'a, G>

§

impl<'a, G> !Send for Dot<'a, G>

§

impl<'a, G> !Sync for Dot<'a, G>

§

impl<'a, G> Unpin for Dot<'a, G>
where G: Unpin,

§

impl<'a, G> !UnwindSafe for Dot<'a, G>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.