my_struct_manual/
my_struct_manual.rs

1//! This example implements a struct `MyStruct` and then implements the trait `Visualize` for it.
2//! End users will likely have no need to manually implement this trait, and should prefer to use
3//! the derive macro as shown in the `my_struct_derive` example.
4
5use std::error::Error;
6use std::fs::File;
7
8use vizz::DataDescription;
9use vizz::Graph;
10use vizz::Visualize;
11
12struct MyStruct<'a> {
13    my_u8: u8,
14    my_string: String,
15    my_ref: &'a String,
16}
17
18impl<'a> Visualize for MyStruct<'a> {
19    fn associated_data(&self) -> Option<Vec<DataDescription>> {
20        Some(vec![
21            DataDescription::from(&self.my_u8).with_label("my_u8"),
22            DataDescription::from(&self.my_string).with_label("my_string"),
23            DataDescription::from(&self.my_ref).with_label("my_ref"),
24        ])
25    }
26}
27
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}