Implement GraphViz Dot Format Serialization with Serde

I currently have 2 structs that I can serialize into Graphviz dot format but I'm doing in a non Serde manner:

struct DawgState_ {
    is_final: bool,
    id: u64,
    transitions: BTreeMap<char, DawgState>
}

type DawgState = Rc<RefCell<DawgState_>>;

struct Dawg {
    start: DawgState,
    nb_states: u64
}

impl DawgState_ {
    fn write_dot(&self, is_initial: bool) {
        // Prints the graph transitions: nb -> nb [label="label"]
    }
}

impl Dawg {
    fn write_dot(&self) {
        // Opens graph object
        // Breadth First Search on State and call DawgState_.write_dot
        // Closes graph object
    }
}

This works and I'm able to have graph printed on the terminal with println!. eg output:

Digraph MDA {
rankdir=LR;
ordering=out;
1 [peripheries=3]
1 -> 2 [label="i"];
1 -> 8 [label="u"];
2 -> 3 [label="n"];
8 -> 2 [label="i"];
3 -> 4 [label="t"];
4 -> 5 [label="3"];
4 -> 7 [label="6"];
5 -> 6 [label="2"];
7 -> 6 [label="4"];
6 [peripheries=2]
s[height=0.1, width=0.1, fixedsize = true, fontsize = 0.1,
fontcolor=blue, style=filled, fillcolor=blue, color=blue];
edge[color=blue];
s->1;}

Now, I would like to transform that logic into a Serde Serializer. I read the Write a data format section on the Serde doc but I'm not really sure I should go that way since GraphViz format is not really storing data in a JSON/YAML way.

What would be the way to extract the Serializer from the impl of both DawgState_ and Dawg and serialize this graph?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.