my_enum_derived/
my_enum_derived.rs

1//! Example of deriving Visualize for an enum
2//!
3//! The generated graph has no references between nodes, and thus is a bit contrived. But it
4//! demonstrates adding unrelated nodes to the same graph.
5
6use std::fs::File;
7
8use vizz::Graph;
9use vizz::Visualize;
10
11#[derive(Visualize)]
12enum MyEnum {
13    Plain,
14    WithU8(u8),
15    WithU8AndString(u8, String),
16    WithStruct { my_u8: u8, my_string: String },
17}
18
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}