Struct 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§

Source§

impl<'w> DotWriter<'w>

Source

pub fn set_pretty_print(&mut self, pretty_print: bool)

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 {}
    }
  }
}
Source

pub fn graph(&mut self) -> Scope<'_, 'w>

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;}"
);
Source

pub fn digraph(&mut self) -> Scope<'_, 'w>

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;}"
);
Source

pub fn write_string<F: Fn(&mut DotWriter<'_>)>(builder: F) -> String

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§

Source§

impl<'w, W: Write> From<&'w mut W> for DotWriter<'w>

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).

Source§

fn from(writer: &'w mut W) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'w> Freeze for DotWriter<'w>

§

impl<'w> !RefUnwindSafe for DotWriter<'w>

§

impl<'w> !Send for DotWriter<'w>

§

impl<'w> !Sync for DotWriter<'w>

§

impl<'w> Unpin for DotWriter<'w>

§

impl<'w> !UnwindSafe for DotWriter<'w>

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, 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.