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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! The `restructure` crate provides functionalities to match regexp patterns
//! into struct fields. This crate only builds on **rust-nightly** as of now.
//!
//! To use this, add the following to your `Cargo.toml`.
//!
//!```text
//! [dependencies]
//! restructure = "0.1.1"
//!```
//!
//! and add this to your crate root:
//!
//!```
//! #![feature(cell_extras)]
//! extern crate regex;
//!```
   

#![feature(cell_extras)]
extern crate regex;

use regex::{Regex, Error};
use std::cell::{RefCell, Ref};

/// This trait allows you to match struct fields with regexp
pub trait RegexStruct {
    /// This function returns the Regex representation of the struct
    fn as_regex(&self) -> Ref<Regex>;
    
    /// This function takes a Slice, find Captures in it and assigns
    /// it to the appropriate struct fields
    fn find(&self, text: &str) -> Self;
}

/// Contains the `fill` method used to fill the struct with fields
pub struct Restruct;

impl Restruct {
    /// This function takes a `RegexStruct` and a `Slice` and returns a `RegexStruct` with its fields filled with 
    /// the patterns from the text
    ///
    ///```
    /// # #![feature(cell_extras)]
    ///
    /// # #[macro_use] extern crate restructure;
    /// # extern crate regex;
    /// # use std::cell::{RefCell, Ref};
    /// # use regex::{Regex, Error};
    /// # use restructure::{Restruct, RegexStruct};
    /// # fn main() {
    /// regexify! ( Details {
    /// name, String, r"\w+"
    /// _w, String, r"\s+"
    /// age, i32, r"\d+"
    /// });
    /// 
    ///let user: Details = Default::default();
    ///
    ///let obama = Restruct::fill(&user, "Obama 54");
    ///
    ///assert_eq!("Obama", obama.name);
    ///assert_eq!(54, obama.age);
    /// # }
    ///```
    pub fn fill<T: RegexStruct>(regex_struct: &T, text: &str) -> T {
        regex_struct.find(text)
    }
}

/// Create a struct with regex patterns and implements RegexStruct trait on it.
///
///```
/// # #![feature(cell_extras)]
/// # #[macro_use] extern crate restructure;
/// # extern crate regex;
/// # use std::cell::{RefCell, Ref};
/// # use regex::{Regex, Error};
/// # use restructure::{Restruct, RegexStruct};
/// # fn main() {
/// regexify! ( Details {
/// name, String, r"\w+"
/// _w, String, r"\s+"
/// age, i32, r"\d+"
/// });
/// # }
///```
#[macro_export]
macro_rules! regexify {
    ($name : ident {
        $($field : ident, $field_type : ty, $pattern : expr)*
    }) => {
        struct $name {
            $(
              $field: $field_type,
            )*
            _regex : RefCell<Result<Regex, Error>>
        }

        impl Default for $name {
          fn default() -> $name {
            
            let mut regex = String::new();
              
              $(
                   match stringify!($field) {
                    x if x.starts_with("_") => {
                      regex.push_str($pattern);
                    },
                    y => {
                       let capture_with_name = format!("(?P<{}>{})", y, $pattern);
                       regex.push_str(&capture_with_name);
                    }   
                   }
              )*
              
            $name {
              $($field : Default::default(),)*
              _regex : RefCell::new(Regex::new(&regex))
            } 
          }
        }
                
        impl RegexStruct for $name {

            fn as_regex(&self) -> Ref<Regex> {
              let re: Ref<Regex> = Ref::filter_map(self._regex.borrow(), |o| o.as_ref().ok()).unwrap();
              re
            }
            
            fn find(&self, text: &str) -> $name {
      
              let captures = self.as_regex().captures(text).unwrap();
      
              let mut filled : $name  = Default::default();
              
              $(
                  if let Some(value) = captures.name(stringify!($field)) {
                    filled.$field = value.parse::<$field_type>().unwrap();
                  }
              )*
              
              filled
            } 
        }
    }
}

#[cfg(test)]
mod test {

    use std::cell::{RefCell, Ref};
    use regex::{Regex, Error};
    use super::{Restruct, RegexStruct};

    #[test]
    fn single_struct_regex() {
        
        regexify!(SemVer {
        major, i32, r"\d+"
        _1, String, r"\."
        minor, i32, r"\d+"
        _2, String, r"\."
        patch, i32, r"\d+"
      });

        let version: SemVer = Default::default();

        assert_eq!(r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)",
                   version.as_regex().as_str());
    }

    #[test]
    fn single_struct_with_same_types() {

        regexify!(HostName {
        domain, String, r"\w+"
        _dot, String, r"\."
        tld, String, r"\w+"
      });

        let host: HostName = Default::default();

        let filled_host = Restruct::fill(&host, "example.com");

        assert_eq!("example", filled_host.domain);
        assert_eq!("com", filled_host.tld);
    }

    #[test]
    fn single_struct_with_diff_types() {

        regexify!(Movies {
        title, String, r"'[^']+'"
        _1, String, r"\s+\("
        year, i32, r"\d+"
        _2, String, r"\)"
      });

        let movie: Movies = Default::default();

        let filled_movie = Restruct::fill(&movie, "Not my favorite movie: 'Citizen Kane' (1941).");

        assert_eq!(r"'Citizen Kane'", filled_movie.title);
        assert_eq!(1941, filled_movie.year);
    }
}