gml_parser/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*!
A 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.

Example graph:

```gml
graph [
  directed 1
  node [
    id 0
    label "Node 0"
  ]
  node [
    id 1
    label "Node 1"
  ]
  edge [
    source 0
    target 0
  ]
  edge [
    source 1
    target 0
  ]
]
```
*/

// https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md
#![deny(unsafe_op_in_unsafe_fn)]

pub mod gml;
mod parser;

use nom::Finish;

/// Parse the graph string into a [`gml::Gml`] object. If the graph contains syntax errors, a
/// human-readable error message will be returned.
/// ```
/// let graph = r#"
/// graph [
///   node [
///     id 0
///   ]
///   edge [
///     source 0
///     target 0
///   ]
/// ]"#;
/// let graph = match gml_parser::parse(graph) {
///     Ok(g) => g,
///     Err(e) => panic!("Could not parse graph: {}", e),
/// };
/// ```
pub fn parse(gml_str: &str) -> Result<gml::Gml, String> {
    match parser::gml::<nom::error::VerboseError<&str>>(gml_str).finish() {
        Ok((_remaining, graph)) => Ok(graph),
        Err(e) => Err(nom::error::convert_error(gml_str, e)),
    }
}