Graph

Struct Graph 

Source
pub struct Graph { /* private fields */ }
Expand description

A struct for building a graph

This is the main tool end users will want to use to generate graph visualizations.

§Example

use vizz::Graph;
use std::fs::File;

struct MyStruct<'a> {
    my_u8: u8,
    my_string: String,
    my_ref: &'a String,
}

// create some data
let unowned_string = String::from("this is referenced!");
let my_struct = MyStruct {
    my_u8: 42,
    my_string: "this is owned!".into(),
    my_ref: &unowned_string,
};

// create file
let mut dot_file = File::create("/tmp/my_struct.dot").unwrap();

// create graph
Graph::new()
    .set_id("my_test_graph")
    .add_node(&my_struct)
    .add_node(&unowned_string)
    .write_to(&mut dot_file)
    .unwrap();

Implementations§

Source§

impl Graph

Source

pub fn new() -> Graph

Create a new graph

Examples found in repository?
examples/my_tuple_struct_derived.rs (line 18)
10pub fn main() -> Result<(), Box<dyn Error>> {
11    // create some data
12    let my_struct = MyStruct(45, 42_000_000_000, String::from("this is my tuple struct"));
13
14    // create file
15    let mut dot_file = File::create("my_struct.dot")?;
16
17    // create graph
18    Graph::new().add_node(&my_struct).write_to(&mut dot_file)?;
19
20    Ok(())
21}
More examples
Hide additional examples
examples/my_struct_derive.rs (line 29)
16pub fn main() -> Result<(), Box<dyn Error>> {
17    // create some data
18    let unowned_string = String::from("yabadabadoo!");
19    let my_struct = MyStruct {
20        my_u8: 42,
21        my_string: "HELLO WORLD".into(),
22        my_ref: &unowned_string,
23    };
24
25    // create file
26    let mut dot_file = File::create("my_struct.dot")?;
27
28    // create graph
29    Graph::new()
30        .add_node(&my_struct)
31        .add_node(&unowned_string)
32        .write_to(&mut dot_file)?;
33
34    Ok(())
35}
examples/my_struct_manual.rs (line 41)
28pub fn main() -> Result<(), Box<dyn Error>> {
29    // create some data
30    let unowned_string = String::from("yabadabadoo!");
31    let my_struct = MyStruct {
32        my_u8: 42,
33        my_string: "HELLO WORLD".into(),
34        my_ref: &unowned_string,
35    };
36
37    // create file
38    let mut dot_file = File::create("my_struct.dot")?;
39
40    // create graph
41    Graph::new()
42        .add_node(&my_struct)
43        .add_node(&unowned_string)
44        .write_to(&mut dot_file)?;
45
46    Ok(())
47}
examples/my_enum_manual.rs (line 59)
45pub fn main() -> Result<(), Box<dyn std::error::Error>> {
46    // create some values
47    let plain_enum = MyEnum::Plain;
48    let enum_with_u8_and_string = MyEnum::WithU8AndString(8, String::from("hey"));
49    let enum_with_u8 = MyEnum::WithU8(8);
50    let enum_with_named_fields = MyEnum::WithStruct {
51        my_u8: 8,
52        my_string: String::from("hey"),
53    };
54
55    // create file
56    let mut dot_file = File::create("my_enum.dot")?;
57
58    // create graph
59    Graph::new()
60        .set_id("my_enum_visualization")
61        .add_node(&plain_enum)
62        .add_node(&enum_with_named_fields)
63        .add_node(&enum_with_u8)
64        .add_node(&enum_with_u8_and_string)
65        .write_to(&mut dot_file)?;
66
67    Ok(())
68}
examples/my_enum_derived.rs (line 33)
19pub fn main() -> Result<(), Box<dyn std::error::Error>> {
20    // create some values
21    let plain_enum = MyEnum::Plain;
22    let enum_with_u8_and_string = MyEnum::WithU8AndString(6, String::from("hey"));
23    let enum_with_u8 = MyEnum::WithU8(10);
24    let enum_with_named_fields = MyEnum::WithStruct {
25        my_u8: 8,
26        my_string: String::from("hey hey mic check 1 2 3"),
27    };
28
29    // create file
30    let mut dot_file = File::create("my_enum.dot")?;
31
32    // create graph
33    Graph::new()
34        .set_id("my_enum_visualization")
35        .add_node(&plain_enum)
36        .add_node(&enum_with_named_fields)
37        .add_node(&enum_with_u8)
38        .add_node(&enum_with_u8_and_string)
39        .write_to(&mut dot_file)?;
40
41    Ok(())
42}
Source

pub fn set_id(self, new_id: impl Into<String>) -> Graph

Set the ID for the graph

See the DOT language grammar ID for more info: https://graphviz.org/doc/info/lang.html

Examples found in repository?
examples/my_enum_manual.rs (line 60)
45pub fn main() -> Result<(), Box<dyn std::error::Error>> {
46    // create some values
47    let plain_enum = MyEnum::Plain;
48    let enum_with_u8_and_string = MyEnum::WithU8AndString(8, String::from("hey"));
49    let enum_with_u8 = MyEnum::WithU8(8);
50    let enum_with_named_fields = MyEnum::WithStruct {
51        my_u8: 8,
52        my_string: String::from("hey"),
53    };
54
55    // create file
56    let mut dot_file = File::create("my_enum.dot")?;
57
58    // create graph
59    Graph::new()
60        .set_id("my_enum_visualization")
61        .add_node(&plain_enum)
62        .add_node(&enum_with_named_fields)
63        .add_node(&enum_with_u8)
64        .add_node(&enum_with_u8_and_string)
65        .write_to(&mut dot_file)?;
66
67    Ok(())
68}
More examples
Hide additional examples
examples/my_enum_derived.rs (line 34)
19pub fn main() -> Result<(), Box<dyn std::error::Error>> {
20    // create some values
21    let plain_enum = MyEnum::Plain;
22    let enum_with_u8_and_string = MyEnum::WithU8AndString(6, String::from("hey"));
23    let enum_with_u8 = MyEnum::WithU8(10);
24    let enum_with_named_fields = MyEnum::WithStruct {
25        my_u8: 8,
26        my_string: String::from("hey hey mic check 1 2 3"),
27    };
28
29    // create file
30    let mut dot_file = File::create("my_enum.dot")?;
31
32    // create graph
33    Graph::new()
34        .set_id("my_enum_visualization")
35        .add_node(&plain_enum)
36        .add_node(&enum_with_named_fields)
37        .add_node(&enum_with_u8)
38        .add_node(&enum_with_u8_and_string)
39        .write_to(&mut dot_file)?;
40
41    Ok(())
42}
Source

pub fn add_node<V>(self, node: &V) -> Graph
where V: Visualize,

Add a data structure that implements Visualize to the Graph

Examples found in repository?
examples/my_tuple_struct_derived.rs (line 18)
10pub fn main() -> Result<(), Box<dyn Error>> {
11    // create some data
12    let my_struct = MyStruct(45, 42_000_000_000, String::from("this is my tuple struct"));
13
14    // create file
15    let mut dot_file = File::create("my_struct.dot")?;
16
17    // create graph
18    Graph::new().add_node(&my_struct).write_to(&mut dot_file)?;
19
20    Ok(())
21}
More examples
Hide additional examples
examples/my_struct_derive.rs (line 30)
16pub fn main() -> Result<(), Box<dyn Error>> {
17    // create some data
18    let unowned_string = String::from("yabadabadoo!");
19    let my_struct = MyStruct {
20        my_u8: 42,
21        my_string: "HELLO WORLD".into(),
22        my_ref: &unowned_string,
23    };
24
25    // create file
26    let mut dot_file = File::create("my_struct.dot")?;
27
28    // create graph
29    Graph::new()
30        .add_node(&my_struct)
31        .add_node(&unowned_string)
32        .write_to(&mut dot_file)?;
33
34    Ok(())
35}
examples/my_struct_manual.rs (line 42)
28pub fn main() -> Result<(), Box<dyn Error>> {
29    // create some data
30    let unowned_string = String::from("yabadabadoo!");
31    let my_struct = MyStruct {
32        my_u8: 42,
33        my_string: "HELLO WORLD".into(),
34        my_ref: &unowned_string,
35    };
36
37    // create file
38    let mut dot_file = File::create("my_struct.dot")?;
39
40    // create graph
41    Graph::new()
42        .add_node(&my_struct)
43        .add_node(&unowned_string)
44        .write_to(&mut dot_file)?;
45
46    Ok(())
47}
examples/my_enum_manual.rs (line 61)
45pub fn main() -> Result<(), Box<dyn std::error::Error>> {
46    // create some values
47    let plain_enum = MyEnum::Plain;
48    let enum_with_u8_and_string = MyEnum::WithU8AndString(8, String::from("hey"));
49    let enum_with_u8 = MyEnum::WithU8(8);
50    let enum_with_named_fields = MyEnum::WithStruct {
51        my_u8: 8,
52        my_string: String::from("hey"),
53    };
54
55    // create file
56    let mut dot_file = File::create("my_enum.dot")?;
57
58    // create graph
59    Graph::new()
60        .set_id("my_enum_visualization")
61        .add_node(&plain_enum)
62        .add_node(&enum_with_named_fields)
63        .add_node(&enum_with_u8)
64        .add_node(&enum_with_u8_and_string)
65        .write_to(&mut dot_file)?;
66
67    Ok(())
68}
examples/my_enum_derived.rs (line 35)
19pub fn main() -> Result<(), Box<dyn std::error::Error>> {
20    // create some values
21    let plain_enum = MyEnum::Plain;
22    let enum_with_u8_and_string = MyEnum::WithU8AndString(6, String::from("hey"));
23    let enum_with_u8 = MyEnum::WithU8(10);
24    let enum_with_named_fields = MyEnum::WithStruct {
25        my_u8: 8,
26        my_string: String::from("hey hey mic check 1 2 3"),
27    };
28
29    // create file
30    let mut dot_file = File::create("my_enum.dot")?;
31
32    // create graph
33    Graph::new()
34        .set_id("my_enum_visualization")
35        .add_node(&plain_enum)
36        .add_node(&enum_with_named_fields)
37        .add_node(&enum_with_u8)
38        .add_node(&enum_with_u8_and_string)
39        .write_to(&mut dot_file)?;
40
41    Ok(())
42}
Source

pub fn render(&self) -> String

Create the full DOT graph file contents as a String

Source

pub fn write_to<W: Write>(self, writer: &mut W) -> Result<()>

Write the DOT file to the filesystem

Examples found in repository?
examples/my_tuple_struct_derived.rs (line 18)
10pub fn main() -> Result<(), Box<dyn Error>> {
11    // create some data
12    let my_struct = MyStruct(45, 42_000_000_000, String::from("this is my tuple struct"));
13
14    // create file
15    let mut dot_file = File::create("my_struct.dot")?;
16
17    // create graph
18    Graph::new().add_node(&my_struct).write_to(&mut dot_file)?;
19
20    Ok(())
21}
More examples
Hide additional examples
examples/my_struct_derive.rs (line 32)
16pub fn main() -> Result<(), Box<dyn Error>> {
17    // create some data
18    let unowned_string = String::from("yabadabadoo!");
19    let my_struct = MyStruct {
20        my_u8: 42,
21        my_string: "HELLO WORLD".into(),
22        my_ref: &unowned_string,
23    };
24
25    // create file
26    let mut dot_file = File::create("my_struct.dot")?;
27
28    // create graph
29    Graph::new()
30        .add_node(&my_struct)
31        .add_node(&unowned_string)
32        .write_to(&mut dot_file)?;
33
34    Ok(())
35}
examples/my_struct_manual.rs (line 44)
28pub fn main() -> Result<(), Box<dyn Error>> {
29    // create some data
30    let unowned_string = String::from("yabadabadoo!");
31    let my_struct = MyStruct {
32        my_u8: 42,
33        my_string: "HELLO WORLD".into(),
34        my_ref: &unowned_string,
35    };
36
37    // create file
38    let mut dot_file = File::create("my_struct.dot")?;
39
40    // create graph
41    Graph::new()
42        .add_node(&my_struct)
43        .add_node(&unowned_string)
44        .write_to(&mut dot_file)?;
45
46    Ok(())
47}
examples/my_enum_manual.rs (line 65)
45pub fn main() -> Result<(), Box<dyn std::error::Error>> {
46    // create some values
47    let plain_enum = MyEnum::Plain;
48    let enum_with_u8_and_string = MyEnum::WithU8AndString(8, String::from("hey"));
49    let enum_with_u8 = MyEnum::WithU8(8);
50    let enum_with_named_fields = MyEnum::WithStruct {
51        my_u8: 8,
52        my_string: String::from("hey"),
53    };
54
55    // create file
56    let mut dot_file = File::create("my_enum.dot")?;
57
58    // create graph
59    Graph::new()
60        .set_id("my_enum_visualization")
61        .add_node(&plain_enum)
62        .add_node(&enum_with_named_fields)
63        .add_node(&enum_with_u8)
64        .add_node(&enum_with_u8_and_string)
65        .write_to(&mut dot_file)?;
66
67    Ok(())
68}
examples/my_enum_derived.rs (line 39)
19pub fn main() -> Result<(), Box<dyn std::error::Error>> {
20    // create some values
21    let plain_enum = MyEnum::Plain;
22    let enum_with_u8_and_string = MyEnum::WithU8AndString(6, String::from("hey"));
23    let enum_with_u8 = MyEnum::WithU8(10);
24    let enum_with_named_fields = MyEnum::WithStruct {
25        my_u8: 8,
26        my_string: String::from("hey hey mic check 1 2 3"),
27    };
28
29    // create file
30    let mut dot_file = File::create("my_enum.dot")?;
31
32    // create graph
33    Graph::new()
34        .set_id("my_enum_visualization")
35        .add_node(&plain_enum)
36        .add_node(&enum_with_named_fields)
37        .add_node(&enum_with_u8)
38        .add_node(&enum_with_u8_and_string)
39        .write_to(&mut dot_file)?;
40
41    Ok(())
42}

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Graph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Graph

Source§

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

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

impl Default for Graph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnwindSafe for Graph

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.