render_regex 0.0.0

SVG visualization of regex DFAs.
Documentation
//! Convert an NFA into a VisualGraph.

use rusty_regex::nfa::{NFA, TransitionLabel};
use crate::graph::{VisualGraph, VisualNode, VisualEdge, NodeType, ShapeHint, EdgeStyle};

pub fn nfa_to_graph(nfa: &NFA) -> VisualGraph {
    let mut graph = VisualGraph::with_capacity(nfa.states.len(), 64);

    for (id, state) in nfa.states.iter().enumerate() {
        graph.nodes.push(VisualNode {
            id,
            label: format!("s{}", id),
            shape: ShapeHint::Circle,
            is_accept: id == nfa.accept,
            node_type: NodeType::Nfa,
        });

        for transition in &state.transitions {
            let label = match &transition.label {
                TransitionLabel::Epsilon => "ε".to_string(),
                TransitionLabel::Char(c) => format!("'{}'", c),
                TransitionLabel::Any => ".".to_string(),
                TransitionLabel::CharClass(chars) => format!("[{}]", chars.iter().collect::<String>()),
                TransitionLabel::CharClassNeg(chars) => format!("[^{}]", chars.iter().collect::<String>()),
                TransitionLabel::Predefined(p) => format!("{:?}", p),
            };

            graph.edges.push(VisualEdge {
	        from: id,
   		to: transition.target,
	        label,
	        style: EdgeStyle::Solid,
	        bend_offset: 0.0,
	    });


        }
    }

    graph
}