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
61
62
63
64
65
//!
//! Generalized regex like pattern match for vector.
//!
//!  ```rust
//!  use vec_reg::{Regex, CompiledRegex, vec_reg};
//!
//!  fn build_without_macro() {
//!      let is_fizz = |x: &i32| x % 3 == 0;
//!      let is_buzz = |x: &i32| x % 5 == 0;
//!      let is_fizz_buzz = |x: &i32| x % 15 == 0;
//!      let reg = Regex::concat(
//!          Regex::satisfy(is_fizz),
//!          Regex::repeat1(Regex::concat(Regex::satisfy(is_buzz), Regex::satisfy(is_fizz_buzz))),
//!      )
//!      .compile();
//!      assert!(!reg.is_match(&vec![1, 2, 3]));
//!      assert!(reg.is_match(&vec![3, 5, 15]));
//!      assert!(reg.is_match(&vec![6, 10, 15, 10, 30]));
//!  }
//!  
//!  fn build_with_macro() {
//!      let is_fizz = |x: &i32| x % 3 == 0;
//!      let is_buzz = |x: &i32| x % 5 == 0;
//!      let reg = vec_reg!({is_fizz}({is_buzz}{|x| x % 15 == 0})+).compile();    
//!      assert!(!reg.is_match(&vec![1, 2, 3]));
//!      assert!(reg.is_match(&vec![3, 5, 15]));
//!      assert!(reg.is_match(&vec![6, 10, 15, 10, 30]));
//!  }
//!  ```

pub use vec_reg_common::{CompiledRegex, Regex};
pub use vec_reg_macro::vec_reg;

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn match_without_macro() {
        let is_fizz = |x: &i32| x % 3 == 0;
        let is_buzz = |x: &i32| x % 5 == 0;
        let is_fizz_buzz = |x: &i32| x % 15 == 0;
        let reg = Regex::concat(
            Regex::satisfy(is_fizz),
            Regex::repeat1(Regex::concat(
                Regex::satisfy(is_buzz),
                Regex::satisfy(is_fizz_buzz),
            )),
        )
        .compile();
        assert!(!reg.is_match(&[1, 2, 3]));
        assert!(reg.is_match(&[3, 5, 15]));
        assert!(reg.is_match(&[6, 10, 15, 10, 30]));
    }

    #[test]
    fn match_with_macro() {
        let is_fizz = |x: &i32| x % 3 == 0;
        let is_buzz = |x: &i32| x % 5 == 0;
        let reg = vec_reg!({is_fizz}({is_buzz}{|x| x % 15 == 0})+).compile();
        assert!(!reg.is_match(&[1, 2, 3]));
        assert!(reg.is_match(&[3, 5, 15]));
        assert!(reg.is_match(&[6, 10, 15, 10, 30]));
    }
}