Scope

Struct Scope 

Source
pub struct Scope<'d, 'w> { /* private fields */ }
Expand description

A Scope struct represents either a graph, digraph, subgraph or cluster. Its the workhorse of the DOT writing, and can be used to create new sub-scopes, add nodes, add edges, or adjust default attributes for any of the above.

The only way to construct a top level graph or digraph scope is to call the DotWriter::graph or DotWriter::digraph functions on a new DotWriter.

Implementations§

Source§

impl<'d, 'w> Scope<'d, 'w>

Source

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

Starts a new nested subgraph, returning another Scope for writing to that subgraph.

Source

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

Starts a new nested cluster subgraph, returning another Scope for writing to it. A cluster is a special case of a subgraph which groups its child nodes together. See the “Subgraphs and Clusters” section of the Graphviz documentation for more information.

Source

pub fn graph_attributes(&mut self) -> AttributesList<'_, 'w>

Returns a struct for writing the default attributes for all subgraphs from now on.

Source

pub fn edge_attributes(&mut self) -> AttributesList<'_, 'w>

Returns a struct for writing the default attributes for all edges from now on.

Source

pub fn node_attributes(&mut self) -> AttributesList<'_, 'w>

Returns a struct for writing the default attributes for all nodes from now on.

Source

pub fn node_auto(&mut self) -> Node<'_, 'w>

Creates a new node, with an automatic default id of the format node_x where x is an incerementing integer. You don’t have to declare nodes before using them in a call to Scope::edge, but you do have to declare them using this function if you want to set specifc attributes for this node (font etc).

The returned value can be used to get the automatically generated id, and also to set the attributes.

use dot_writer::{DotWriter, Color, Attributes};

let dot_string = DotWriter::write_string(|writer| {
  writer.set_pretty_print(false);
  let mut digraph = writer.digraph();
  let a = digraph.node_auto().id();
  let b = {
    let mut node = digraph.node_auto();
    node.set_color(Color::Red);
    node.id()
  };
  digraph.edge(a, b);
});
assert_eq!(
    dot_string,
    "digraph{node_0;node_1[color=red];node_0->node_1;}"
);
Source

pub fn node_named<S: Into<String>>(&mut self, id: S) -> Node<'_, 'w>

Creates a new node, with the specified id. You don’t have to declare nodes before using them in a call to Scope::edge, but you do have to declare them using this function if you want to set specific attributes for this node (font etc).

The returned value can be used to get the assigned name, and also to set the attributes.

use dot_writer::{DotWriter, Color, Attributes};

let dot_string = DotWriter::write_string(|writer| {
  writer.set_pretty_print(false);
  let mut digraph = writer.digraph();
  let a = digraph.node_named("alpha").id();
  let b = {
    let mut node = digraph.node_named("beta");
    node.set_color(Color::Red);
    node.id()
  };
  digraph.edge(a, b);
});
assert_eq!(
    dot_string,
    "digraph{alpha;beta[color=red];alpha->beta;}"
);
Source

pub fn edge<F, T>(&mut self, from_node_id: F, to_node_id: T) -> EdgeList<'_, 'w>
where F: AsRef<[u8]>, T: AsRef<[u8]>,

Add a new edge joining start_node_id and end_node_id nodes. Note that nodes do not need to be already defined by Scope::node_auto or by Scope::node_named (unless you want to set node-specific attributes). Arguments can be just strings, or you can use the Node::id of an already defined node:

use dot_writer::DotWriter;

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

pub fn edges<I, E>(&mut self, items: I) -> Option<EdgeList<'_, 'w>>
where I: IntoIterator<Item = E>, E: AsRef<[u8]>,

Add N-1 edges joining all node ids or subgraphs in the iterator, in the same manner as Scope::edge. The return value will be None if less than 2 items are passed in.

use dot_writer::DotWriter;

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

Trait Implementations§

Source§

impl<'d, 'w> Attributes for Scope<'d, 'w>

Source§

fn set(&mut self, name: &str, value: &str, quote: bool) -> &mut Self

Sets an attribute. See the Graphviz documentation for a full list of available names and values. Set the arguement quote to true if the value should be written in quotes ", to escape any special characters. Note that any quote in the string need to be escaped before calling. This function does NOT check that name or value are valid DOT strings.
Source§

fn set_font(&mut self, label: &str) -> &mut Self

Set the name of the font family for label text
Source§

fn set_html(&mut self, label: &str) -> &mut Self

Set arbitary html, useful for constructing more complex nodes
Source§

fn set_label(&mut self, label: &str) -> &mut Self

Set the display label for a graph, node or edge
Source§

fn set_head_label(&mut self, label: &str) -> &mut Self

Set the label to appear at the head of an edge
Source§

fn set_tail_label(&mut self, label: &str) -> &mut Self

Set the label to appear at the tail of an edge
Source§

fn set_color(&mut self, color: Color) -> &mut Self

Set the edge or line color
Source§

fn set_fill_color(&mut self, color: Color) -> &mut Self

Set the color to fill the area with
Source§

fn set_font_color(&mut self, color: Color) -> &mut Self

Set the color of the font used for text
Source§

fn set_background_color(&mut self, color: Color) -> &mut Self

Set the background color
Source§

fn set_shape(&mut self, shape: Shape) -> &mut Self

Set the shape of a graph, subgraph, cluster or node
Source§

fn set_style(&mut self, style: Style) -> &mut Self

Set the style
Source§

fn set_arrow_head(&mut self, arrow_type: ArrowType) -> &mut Self

Set type of arrow head for edge lines (the arrow at the destination)
Source§

fn set_arrow_tail(&mut self, arrow_type: ArrowType) -> &mut Self

Set type of arrow tail for edge lines (the arrow at the source)
Source§

fn set_rank(&mut self, rank: Rank) -> &mut Self

Set the relative rank, which affects layout
Source§

fn set_pen_width(&mut self, width: f32) -> &mut Self

Set the pen width for drawing lines
Source§

fn set_arrow_size(&mut self, size: f32) -> &mut Self

Set the arrow size
Source§

fn set_font_size(&mut self, size: f32) -> &mut Self

Set the font size
Source§

fn set_rank_direction(&mut self, rank_direction: RankDirection) -> &mut Self

Source§

impl<'d, 'w> Drop for Scope<'d, 'w>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'d, 'w> Freeze for Scope<'d, 'w>

§

impl<'d, 'w> !RefUnwindSafe for Scope<'d, 'w>

§

impl<'d, 'w> !Send for Scope<'d, 'w>

§

impl<'d, 'w> !Sync for Scope<'d, 'w>

§

impl<'d, 'w> Unpin for Scope<'d, 'w>

§

impl<'d, 'w> !UnwindSafe for Scope<'d, '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.