my_struct_derive/
my_struct_derive.rs

1//! Example demonstrating how to derive the trait `Visualize` for a struct, and use it to generate
2//! a DOT file.
3
4use std::error::Error;
5use std::fs::File;
6
7use vizz::{Graph, Visualize};
8
9#[derive(Visualize)]
10struct MyStruct<'a> {
11    my_u8: u8,
12    my_string: String,
13    my_ref: &'a String,
14}
15
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}