Visualize

Trait Visualize 

Source
pub trait Visualize: Sized {
    // Provided methods
    fn data(&self) -> Option<Value> { ... }
    fn associated_data(&self) -> Option<Vec<DataDescription>> { ... }
    fn render_node(&self) -> String { ... }
}
Expand description

A trait for defining how to visually represent a type

For manual implementations, note that each of these methods has a default, so implementors need only implement the methods they need to modify.

Provided Methods§

Source

fn data(&self) -> Option<Value>

How to represent the data for this type, if at all

§Primitive Example

For primitives, this will likely be the primitive’s value. This example is ignored because it conflicts with the implementation of Visualize for u8 provided by this crate.

use vizz::Value;
use vizz::Visualize;

impl Visualize for u8 {
    fn data(&self) -> Option<Value> {
        Some(Value::Owned(self.to_string()))
    }
}
§Enum Example

For enums, this will likely be the variant name.

use vizz::Value;
use vizz::Visualize;

enum MyEnum {
    SimpleVariant,
    VariantWithData(u8),
}

impl Visualize for MyEnum {
    fn data(&self) -> Option<Value> {
        Some(Value::Owned(
            match self {
                MyEnum::SimpleVariant => "SimpleVariant",
                MyEnum::VariantWithData(_) => "VariantWithData",
            }.into()
        ))
    }
}
§Struct Example

For structs, this will likely be None because structs don’t have any inherent data, but instead contain other types, and therefore the default method implementation need not be overridden. The data contained in a struct should be used in Visualize::associated_data.

Source

fn associated_data(&self) -> Option<Vec<DataDescription>>

The list of associated data structures that should be included in a visualization of this data type

For named associated data, such as struct members or named enum variant members, the DataDescription::with_label method can be used to add a label.

§Primitive Example

For primitives, this will likely be None, and therefore the default method implementation need not be overridden.

§Enum Example

For enums with associated data, this will likely be those values. For enums with no associated data, this will likely be None.

use vizz::Visualize;
use vizz::DataDescription;

enum MyEnum {
    SimpleVariant,
    VariantWithData(u8),
}

impl Visualize for MyEnum {
    fn associated_data(&self) -> Option<Vec<DataDescription>> {
        match self {
            MyEnum::SimpleVariant => None,
            MyEnum::VariantWithData(data) => Some(vec![DataDescription::from(data)]),
        }
    }
}
§Struct Example

For structs, this will likely be the struct members.

use vizz::Visualize;
use vizz::DataDescription;

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

impl<'a> Visualize for MyStruct<'a> {
    fn associated_data(&self) -> Option<Vec<DataDescription>> {
        Some(vec![
            DataDescription::from(&self.my_string).with_label("my_string"),
            DataDescription::from(&self.my_ref).with_label("my_ref"),
        ])
    }
}
Source

fn render_node(&self) -> String

Render the node for this data

N.B. that this node is useless on its own and must be put in the context of a Graph. End users seeking to create an actual visualization should use the Graph struct instead of manually trying to use this method.

Examples found in repository?
examples/u8.rs (line 5)
3pub fn main() {
4    let my_int: u8 = 128;
5    let my_int_dot = (&my_int).render_node();
6    println!("{}", my_int_dot);
7}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Visualize for &String

Source§

fn data(&self) -> Option<Value>

Source§

impl Visualize for u8

Source§

fn data(&self) -> Option<Value>

Source§

impl Visualize for usize

Source§

fn data(&self) -> Option<Value>

Source§

impl Visualize for String

Source§

fn data(&self) -> Option<Value>

Source§

impl<T> Visualize for Option<T>
where T: Visualize,

Source§

impl<T> Visualize for Vec<T>
where T: Visualize,

Implementors§