rs-graph 0.14.1

A library for graph algorithms and combinatorial optimization
Documentation
use std::collections::HashMap;
use super::EdgeAttr;

pub steinlib -> Vec<Section>
  = ws* "33D32945"i ws+ "STP"i ws+ "File,"i ws+ "STP"i ws+ "Format"i ws+ "Version"i ws+ "1."i "0"i+ nl+
    secs:(
	   comment_section /
		graph_section /
		terminals_section /
		coordinates_section /
		drawing_section
		) ** (nl*)
	 nl*
	 "EOF"i nl* !.
	 { secs }

pub comment_section -> Section
  = "SECTION"i ws+ "Comment"i nl+
    h:comment_entry ** (nl*)
	 nl*
	 "END"i nl
	 {
	   Section::Comment(h.into_iter().collect())
	 }

comment_entry -> (String, String)
  = ws* key:$([a-zA-Z]+) ws+ val:(quoted / word) nl
    {
	   (key.to_string(), val.to_string())
    }

pub graph_section -> Section
  = "SECTION"i ws+ "Graph"i nl+
    g:(edge_line / arc_line / node_count / edge_count / arc_count) ** (nl*)
	 nl*
	 "END"i nl { Section::Graph(g) }

edge_line -> GraphLine
  = "E"i ws+ u:int ws+ v:int ws+ w:float nl { GraphLine::Edge(u, v, w) }

arc_line -> GraphLine
  = "A"i ws+ u:int ws+ v:int ws+ w:float nl { GraphLine::Arc(u, v, w) }

node_count -> GraphLine
  = "Nodes"i ws+ n:int nl { GraphLine::NumNodes(n) }

edge_count -> GraphLine
  = "Edges"i ws+ n:int nl { GraphLine::NumEdges(n) }

arc_count -> GraphLine
  = "Arcs"i ws+ n:int nl { GraphLine::NumArcs(n) }


pub terminals_section -> Section
  = "SECTION"i ws+ "Terminals"i
    nl+
    "Terminals"i ws+ n:int
	 nl+
    t:terminal ** (nl*)
	 nl*
	 "END"i nl
	 { Section::Terminals(n, t) }

terminal -> usize
  = "T"i ws* n:int nl { n }


pub coordinates_section -> Section
  = "SECTION"i ws+ "Coordinates"i nl
    coords:coordinate ** (nl*)
	 nl*
	 "END"i nl
	 { Section::Coordinates(coords) }

coordinate -> (usize,Vec<f64>)
  = ws* beg:#position "D"i+ end:#position ws+ u:int ws+ nums:float **<{end-beg}> (ws+) nl { (u,nums) }


pub drawing_section -> Section
  = "SECTION"i ws+ "Drawing"i nl
    d:arc_drawing ** (nl*)
	 nl*
	 "END"i nl
	 { Section::Drawing(d) }

arc_drawing -> (usize, usize, Vec<EdgeAttr>)
  = [aAeE] ws+ u:int ws+ v:int ws+ attrs:arc_attrs ** (ws+) nl
    { (u, v, attrs) }

arc_attrs -> EdgeAttr
  = a:$("bl"i / "br"i)
    {
	   match a {
		  "bl" | "bL" | "Bl" | "BL" => EdgeAttr::BendLeft,
		  _ => EdgeAttr::BendRight
      }
    }

int -> usize
  = n:$([-+]? [0-9]+) { n.parse().unwrap() }

float -> f64
  = n:$([-+]? ([0-9]+ "."? [0-9]* / "." [0-9]+) ([eE] [-+]? [0-9]+)?) { n.parse().unwrap() }

quoted -> String
  = "\"" s:$(( "\\" . / [^\"] )*) "\"" { s.to_string() }

word -> String
  = s:$([^ \t\r\n]+) { s.to_string() }

nl
  = #quiet<ws* ("#" [^\n]*)? "\n" ws*>

ws
  = #quiet<[ \t\r]>