use regex::{Captures, Regex};
use std::borrow::Cow;
pub const PATCH : &'static str ="__XYZZ__patch_me__XYZZ__";
type N = [(&'static str, &'static str); 10];
const NAMES: N = [
("brack", r"\s*\[\s+\]"),
("brace", r"\{\s+\}"),
("colon", r"\s+[:]\s"),
("bar", r"\s\|\s+\{"),
("enl", r"\n+\}"),
("fnl", r"\{\n+"),
("result", PATCH), ("lt", r"\s<\s"),
("gt", r"\s>(\s|$)"),
("nl", r"\n+"), ];
lazy_static! {
static ref RE: Regex = {
let v = NAMES
.iter()
.map(|(n, re)| format!("(?P<{}>{})", n, re))
.collect::<Vec<_>>()
.join("|");
Regex::new(&v).unwrap()
};
}
trait Has {
fn has(&self, s: &'static str) -> bool;
fn key(&self) -> &'static str;
}
impl Has for Captures<'_> {
#[inline]
fn has(&self, s: &'static str) -> bool {
self.name(s).is_some()
}
fn key(&self) -> &'static str {
for n in &NAMES {
if self.has(n.0) {
return n.0;
}
}
"?"
}
}
pub fn patch<'t>(s: &'t str) -> Cow<'t, str> {
RE.replace_all(s, |c: &Captures| {
let key = c.key();
match key {
"brace" => "{}",
"brack" => "[]",
"colon" => ": ",
"fnl" => "{ ",
"bar" => "\n | {",
"enl" => " }",
"nl" => " ",
"result" => "|",
"lt" => "<",
"gt" => ">",
_ => c.get(0).unwrap().as_str(),
}
})
}