Skip to main content

read_dot

Function read_dot 

Source
pub fn read_dot<R: Read>(input: R) -> IgraphResult<DotGraph>
Expand description

Read a graph from DOT (Graphviz) format.

Parses graph { ... } (undirected) or digraph { ... } (directed) blocks. Recognizes edge statements using -- or -> and node declarations. Attribute blocks ([key=value, ...]) on nodes and edges are parsed and stored via the Graph attribute system. Subgraphs are treated as flat (nodes are merged into the top-level graph).

Node identifiers may be bare identifiers, numbers, or double-quoted strings. They are assigned internal vertex indices in first-appearance order and returned in DotGraph::labels.

§Errors

Returns an error if the input lacks a graph/digraph keyword or contains -- edges in a digraph (or -> in a graph).

§Examples

use rust_igraph::{read_dot, AttributeValue};

let dot = r#"graph {
  Alice [color="red"];
  Alice -- Bob [weight=1.5];
  Bob -- Carol;
}"#;
let result = read_dot(dot.as_bytes()).unwrap();
assert_eq!(result.graph.vcount(), 3);
assert_eq!(result.graph.ecount(), 2);
assert!(!result.graph.is_directed());
assert_eq!(result.labels, vec!["Alice", "Bob", "Carol"]);
assert_eq!(
    result.graph.vertex_attribute("color", 0).and_then(|v| v.as_str()),
    Some("red"),
);
assert_eq!(
    result.graph.edge_attribute("weight", 0).and_then(|v| v.as_f64()),
    Some(1.5),
);