Struct dot_writer::DotWriter

source ·
pub struct DotWriter<'w> { /* private fields */ }
Expand description

The entry point struct for writing a DOT graph. See the examples on the index for how to use it. This struct must live for the livetime of writing the graph, and also outlive the Write struct it points to, because it’s Drop trait will finish writing graph.

Implementations

If set to true, then output will have additional whitespace including newlines and indentation to make the output more human friendly. If false then this will be left out and the output will be more compact. Defaults to true. For example disabled:

use dot_writer::DotWriter;

let mut bytes = Vec::new();
let mut writer = DotWriter::from(&mut bytes);
writer.set_pretty_print(false);
writer.graph().subgraph().cluster().subgraph();
println!("{}", std::str::from_utf8(&bytes).unwrap());

Gives you:

graph {subgraph {subgraph cluster_0 {subgraph {}}}}

While enabled:

use dot_writer::DotWriter;

let mut bytes = Vec::new();
let mut writer = DotWriter::from(&mut bytes);
// writer.set_pretty_print(false); defaults to true anyway
writer.graph().subgraph().cluster().subgraph();
println!("{}", std::str::from_utf8(&bytes).unwrap());

Gives you:

graph {
  subgraph {
    subgraph cluster_0 {
      subgraph {}
    }
  }
}

Start a new undirection graph. This uses the edge operator -- automatically, which means edges should be rendered without any arrowheads by default.

use dot_writer::DotWriter;

let mut bytes = Vec::new();
let mut writer = DotWriter::from(&mut bytes);
writer.set_pretty_print(false);
writer.graph().edge("a", "b");
assert_eq!(
    std::str::from_utf8(&bytes).unwrap(),
    "graph{a--b;}"
);

Start a new directed graph. This uses the edge operator -> automatically, which means edges should be redered with an arrow head pointing to the head (end) node.

use dot_writer::DotWriter;

let mut bytes = Vec::new();
let mut writer = DotWriter::from(&mut bytes);
writer.set_pretty_print(false);
writer.digraph().edge("a", "b");
assert_eq!(
    std::str::from_utf8(&bytes).unwrap(),
    "digraph{a->b;}"
);

Uses a callback to write DOT to a String. This is useful if you just want to write your dot code to a string rather than a file or stdout, and want less boiler plate setup code. It’s used internally for unit testing, so the output is not pretty printed by default (but you can overwrite that by calling DotWriter::set_pretty_print from within the callback).

use dot_writer::DotWriter;

let output = DotWriter::write_string(|writer| {
    let mut graph = writer.graph();
    graph.edge("a", "b");
});
assert_eq!(output, "graph{a--b;}");

Trait Implementations

This is the only constructor, which requires a Write to borrow and write the DOT output to. Defaults to pretty printing the output (see DotWriter::set_pretty_print).

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.