#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

generate() {
cat <<"EOF"
//! Generated by `tests/regex-gen.sh` -- do not modify directly.

mod common;
use common::regex;

EOF

i=0
while IFS= read -r line; do
    i=$((i + 1))
  
    pattern=$(jq -r '.regex' <<<"$line")
  
    echo "#[test]"
    echo "fn r${i}() {"
  
    echo "    let re = regex(r#\"${pattern}\"#);"
  
    jq -r --arg pat "$pattern" '
      .accepts[]
      | "    assert!(re.matches(r#\"\(.)\"#), \"pattern: \($pat) | expected accept: \(. )\");"
    ' <<<"$line"
  
    jq -r --arg pat "$pattern" '
      .rejects[]
      | "    assert!(!re.matches(r#\"\(.)\"#), \"pattern: \($pat) | expected reject: \(. )\");"
    ' <<<"$line"
  
    echo "}"
    echo
done
}

generate < regex-data.json > regex.rs
