gml_parser/lib.rs
1/*!
2A parser for the [Graph Modelling Language (GML)](https://web.archive.org/web/20190303094704/http://www.fim.uni-passau.de:80/fileadmin/files/lehrstuhl/brandenburg/projekte/gml/gml-technical-report.pdf) format.
3
4Example graph:
5
6```gml
7graph [
8 directed 1
9 node [
10 id 0
11 label "Node 0"
12 ]
13 node [
14 id 1
15 label "Node 1"
16 ]
17 edge [
18 source 0
19 target 0
20 ]
21 edge [
22 source 1
23 target 0
24 ]
25]
26```
27*/
28
29// https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md
30#![deny(unsafe_op_in_unsafe_fn)]
31
32pub mod gml;
33mod parser;
34
35use nom::Finish;
36
37/// Parse the graph string into a [`gml::Gml`] object. If the graph contains syntax errors, a
38/// human-readable error message will be returned.
39/// ```
40/// let graph = r#"
41/// graph [
42/// node [
43/// id 0
44/// ]
45/// edge [
46/// source 0
47/// target 0
48/// ]
49/// ]"#;
50/// let graph = match gml_parser::parse(graph) {
51/// Ok(g) => g,
52/// Err(e) => panic!("Could not parse graph: {}", e),
53/// };
54/// ```
55pub fn parse(gml_str: &str) -> Result<gml::Gml, String> {
56 match parser::gml::<nom_language::error::VerboseError<&str>>(gml_str).finish() {
57 Ok((_remaining, graph)) => Ok(graph),
58 Err(e) => Err(nom_language::error::convert_error(gml_str, e)),
59 }
60}