changeset 66:ae022a4c1234

Implement better graph rendering function
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 31 Aug 2019 20:40:24 +0200
parents 094c91f3213f
children 31905126f204
files src/lib.rs src/state.rs src/tests.rs
diffstat 3 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib.rs	Fri Aug 30 22:59:37 2019 +0200
+++ b/src/lib.rs	Sat Aug 31 20:40:24 2019 +0200
@@ -9,7 +9,12 @@
 
 mod tests;
 
-pub use state::dot as debug_graph_to_dot;
+pub fn render_graph(re: &str) -> String {
+    return format!(
+        "digraph st {{ {} }}",
+        state::dot(&compile::start_compile(parse::parse(re).as_ref().unwrap()))
+    );
+}
 
 fn parse(re: &str) -> Result<repr::Pattern, String> {
     return parse::parse(re);
@@ -28,13 +33,18 @@
 /// regular expression will be compiled every time. Use `compile()` and `match_re()` to make this
 /// more efficient (about 3x faster).
 pub fn match_re_str(re: &str, s: &str) -> Result<(bool, Vec<(usize, usize)>), String> {
-    return Ok(compile_and_match(&repr::optimize::optimize(parse::parse(re)?), s));
+    return Ok(compile_and_match(
+        &repr::optimize::optimize(parse::parse(re)?),
+        s,
+    ));
 }
 
 /// Compile a regular expression into a representation that can be directly used for matching with
 /// `match_re()`.
 pub fn compile(re: &str) -> Result<state::CompiledRE, String> {
-    Ok(compile::start_compile(&repr::optimize::optimize(parse(re)?)))
+    Ok(compile::start_compile(&repr::optimize::optimize(parse(
+        re,
+    )?)))
 }
 
 /// Match a regular expression compiled with `compile()` against a string. Returns a tuple of a
--- a/src/state.rs	Fri Aug 30 22:59:37 2019 +0200
+++ b/src/state.rs	Sat Aug 31 20:40:24 2019 +0200
@@ -126,3 +126,4 @@
     }
     result
 }
+
--- a/src/tests.rs	Fri Aug 30 22:59:37 2019 +0200
+++ b/src/tests.rs	Sat Aug 31 20:40:24 2019 +0200
@@ -2,7 +2,7 @@
 
 //! A general test suite aiming for wide coverage of positive and negative matches.
 
-use crate::{compile, matching, parse, repr, state};
+use crate::{compile, matching, parse, repr};
 
 fn match_re(re: &str, s: &str) -> (bool, Vec<(usize, usize)>) {
     let parsed = parse::parse(re).unwrap();
@@ -11,13 +11,9 @@
     matching::do_match(&ready, s)
 }
 
-fn render_graph(re: &str) {
-    println!("digraph st {{ {} }}", state::dot(&compile::start_compile(parse::parse(re).as_ref().unwrap())));
-}
-
 #[test]
 fn test_notorious_graph() {
-    render_graph("(x+x+)+y");
+    println!("{}", crate::render_graph("(x+x+)+y"));
 }
 
 #[test]