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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
extern crate regex;
extern crate yaml_rust;

use regex::Regex;
use regex::RegexSet;
use yaml_rust::{Yaml, YamlLoader};

// ID[IMPL::yaml-extraction::]
pub struct YogurtYaml<'a> {
    indicators: &'a [&'a str],
    combined_pair: RegexPair,
    regex_set: RegexSet,
}

enum State {
    Open,
    Closed,
}

pub struct Result {
    text: String,
    state: State,
    start: usize,
    end: usize,
}

struct RegexPair {
    // indicator: String,
    regex: Regex,
}

impl Result {
    pub fn get_text(&self) -> &String {
        &self.text
    }

    pub fn get_print(&self) -> String {
        let mut result = self.text.clone();
        result.push_str(" at ");
        result.push_str(&self.start.to_string());
        result.push_str(" -> ");
        result.push_str(&self.end.to_string());
        result.push_str(" : ");
        match &self.state {
            State::Open => result.push_str("Open"),
            State::Closed => result.push_str("Closed"),
        }
        result
    }

    pub fn get_yaml(&self) -> Vec<Yaml> {
        YamlLoader::load_from_str(&self.text).unwrap()
    }
}

impl<'a> YogurtYaml<'a> {
    pub fn new(indicators: &'a [&'a str]) -> YogurtYaml<'a> {
        let pairs = create_pairs(&indicators);
        let combined_pair = create_combined_pair(&indicators);
        YogurtYaml {
            indicators,
            regex_set: get_regexset(&pairs),
            combined_pair,
        }
    }

    pub fn check(&self, s: &str) -> bool {
        self.regex_set.is_match(s)
    }

    pub fn extract(&self, s: &str) -> Vec<Result> {
        cut_yaml(&self.combined_pair, &s.to_string())
    }

    pub fn extract2(&self, s: &str) -> Vec<Result> {
        cut_yaml_idents(&self.indicators, &s.to_string())
    }

    pub fn extract_clear(&self, s: &mut String) -> Vec<Result> {
        let result = cut_yaml(&self.combined_pair, &s);
        s.clear();
        result
    }

    pub fn extract2_clear(&self, s: &mut String) -> Vec<Result> {
        let result = cut_yaml_idents(&self.indicators, s);
        s.clear();
        result
    }

    pub fn verify(_extracts: Vec<Result>) {}

    pub fn combine(_extracts: Vec<Result>) {}
}

fn create_combined_pair(strs: &[&str]) -> RegexPair {
    let mut combined_pair_str: String = "(".to_string();

    for s in strs {
        combined_pair_str.push_str(&s);
        combined_pair_str.push('|');
    }

    combined_pair_str.pop();
    combined_pair_str.push(')');

    create_pair(&combined_pair_str)
}

fn create_pairs(strs: &[&str]) -> Vec<RegexPair> {
    let mut pairs = Vec::new();
    for s in strs {
        pairs.push(create_pair(s));
    }
    pairs
}

// ID[regex, info: "(?P<ident>{})\[(?P<content>[^\]]*)"]
fn create_pair(s: &str) -> RegexPair {
    let re_str = format!(r"(?P<ident>{})\[(?P<content>[^\]]*)", s);
    let re = Regex::new(&re_str).unwrap();
    RegexPair {
        // indicator: s.to_string(),
        regex: re,
    }
}

// fn get_yaml(pairs: Vec<RegexPair>, s: String) -> Vec<yaml_rust::Yaml> {
//     let _re = Regex::new(r"test").unwrap();
//     let mut yaml_str: String = "".to_string();
//     for pair in pairs {
//         let yaml_str_vec = cut_yaml(&pair, &s);
//         yaml_str = prettyfy(yaml_str_vec);
//     }
//     YamlLoader::load_from_str(&yaml_str).unwrap()
// }

fn get_regexset(pairs: &[RegexPair]) -> RegexSet {
    let mut regs = Vec::new();
    for pair in pairs {
        regs.push(pair.regex.to_string());
    }
    RegexSet::new(regs).unwrap()
}

// fn prettyfy(_yaml_vec: Vec<Result>) -> String {
//     "Test".to_string()
// }

fn cut_yaml(reg: &RegexPair, s: &str) -> Vec<Result> {
    let mut v = Vec::new();
    for caps in reg.regex.captures_iter(&s) {
        let mut yaml_str: String = (&caps["ident"]).to_string();
        yaml_str.push_str(": ");
        yaml_str.push_str(&caps["content"]);

        let pos_start = caps.get(0).unwrap().start();
        let pos_end = caps.get(0).unwrap().end();

        let mut result = check_nested(pos_start, &s, yaml_str);

        result.insert(0, '{');
        result.push('}');

        v.push(Result {
            text: result,
            state: State::Closed,
            start: pos_start,
            end: pos_end,
        });
    }
    v
}

struct Identcheck<'a> {
    ident: &'a str,
    first_char: char,
    begin_char: char,
    end_char: char,
    // mut:
    semantic_position: SemanticPosition,
    length: usize,
    closures: i32,
}

enum SemanticPosition {
    Out,
    Ident,
    In,
    InSingleQuote,
    InDoubleQuote,
    InSingleQuoteEscaped,
    InDoubleQuoteEscaped,
    Done,
}

fn check_out(identcheck: &mut Identcheck, c: char) {
    if c == identcheck.first_char {
        identcheck.length = 1;
        identcheck.semantic_position = SemanticPosition::Ident;
    }
}

fn clean_up(identcheck: &mut Identcheck, c: char) {
    reset(identcheck);
    check_out(identcheck, c); // Could be the start of a ident
}

fn reset(identcheck: &mut Identcheck) {
    identcheck.length = 0;
    identcheck.closures = 0;
    identcheck.semantic_position = SemanticPosition::Out;
}

fn check_ident(identcheck: &mut Identcheck, c: char) {
    let check_size = identcheck.ident.len() < identcheck.length;
    if check_size {
        if identcheck.begin_char == c {
            identcheck.semantic_position = SemanticPosition::In;
            identcheck.closures = 1;
        } else {
            clean_up(identcheck, c);
        }
    } else if c != identcheck.ident.chars().nth(identcheck.length - 1).unwrap() {
        clean_up(identcheck, c);
    }
}

fn check_in(identcheck: &mut Identcheck, c: char) {
    let begin = identcheck.begin_char;
    let end = identcheck.end_char;
    if c == begin {
        identcheck.closures += 1;
    } else if c == end {
        identcheck.closures -= 1;
        check_end(identcheck);
    } else if c == '\'' {
        identcheck.semantic_position = SemanticPosition::InSingleQuote;
    } else if c == '"' {
        identcheck.semantic_position = SemanticPosition::InDoubleQuote;
    }
}

fn check_single_quote(identcheck: &mut Identcheck, c: char) {
    if c == '\'' {
        identcheck.semantic_position = SemanticPosition::In;
    } else if c == '\\' {
        identcheck.semantic_position = SemanticPosition::InSingleQuoteEscaped;
    }
}

fn check_double_quote(identcheck: &mut Identcheck, c: char) {
    if c == '\'' {
        identcheck.semantic_position = SemanticPosition::In;
    } else if c == '\\' {
        identcheck.semantic_position = SemanticPosition::InDoubleQuoteEscaped;
    }
}

fn check_end(identcheck: &mut Identcheck) {
    if identcheck.closures == 0 {
        identcheck.semantic_position = SemanticPosition::Done;
    }
}

fn add_result(
    state: State,
    results: &mut Vec<Result>,
    identcheck: &mut Identcheck,
    s: &str,
    i: usize,
) {
    let length = identcheck.length.checked_sub(1).unwrap();
    let start = i.checked_sub(length).unwrap();
    let end = i;

    let mut text: String = s.chars().skip(start).take(length - 1).collect();
    text = text.replacen(identcheck.begin_char, ": ", 1);
    text.insert(0, '{');
    text.push('}');
    results.push(Result {
        text,
        state,
        start,
        end,
    });
}

fn cut_yaml_idents(idents: &[&str], s: &str) -> Vec<Result> {
    let mut v = Vec::new();
    let mut identchecks = Vec::new();

    for ident in idents {
        identchecks.push(Identcheck {
            ident,
            first_char: ident.chars().nth(0).unwrap(),
            begin_char: '[',
            end_char: ']',
            semantic_position: SemanticPosition::Out,
            length: 0,
            closures: 0,
        });
    }

    for (i, c) in s.chars().enumerate() {
        for identcheck in &mut identchecks {
            identcheck.length += 1;
            match identcheck.semantic_position {
                SemanticPosition::Out => {
                    check_out(identcheck, c);
                }
                SemanticPosition::Ident => {
                    check_ident(identcheck, c);
                }
                SemanticPosition::In => {
                    check_in(identcheck, c);
                }
                SemanticPosition::InSingleQuote => {
                    check_single_quote(identcheck, c);
                }
                SemanticPosition::InDoubleQuote => {
                    check_double_quote(identcheck, c);
                }
                SemanticPosition::InSingleQuoteEscaped => {
                    identcheck.semantic_position = SemanticPosition::InSingleQuote;
                }
                SemanticPosition::InDoubleQuoteEscaped => {
                    identcheck.semantic_position = SemanticPosition::InDoubleQuote;
                }
                SemanticPosition::Done => {
                    add_result(State::Closed, &mut v, identcheck, s, i);
                    reset(identcheck);
                    check_out(identcheck, c);
                }
            }
        }
    }
    for identcheck in &mut identchecks {
        match identcheck.semantic_position {
            SemanticPosition::In => add_result(State::Open, &mut v, identcheck, s, s.len()),
            SemanticPosition::Done => add_result(State::Closed, &mut v, identcheck, s, s.len()),
            _ => (),
        }
    }
    v
}

fn check_nested(pos: usize, s: &str, yaml_str: String) -> String {
    let reg = Regex::new(r#"(\[|\]|'|")"#).unwrap();

    if reg.is_match(&yaml_str) {
        let start = pos;
        let mut len = 0;
        let mut bracket = 0;
        let mut string_open_d = false;
        let mut string_open_s = false;
        let mut escaped = false;

        let split = s.split_at(start).1;
        for c in split.chars() {
            len += 1;
            if escaped {
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if c == '"' {
                if !string_open_s {
                    if !string_open_d {
                        string_open_d = true;
                    } else {
                        string_open_d = false;
                    }
                }
            } else if c == '\'' {
                if !string_open_d {
                    if !string_open_s {
                        string_open_s = true;
                    } else {
                        string_open_s = false;
                    }
                }
            } else if string_open_d || string_open_s {
            } else if c == '[' {
                bracket += 1;
            } else if c == ']' {
                bracket -= 1;
                if bracket == 0 {
                    break;
                }
            }
        }
        let result: String = s.chars().skip(start).take(len - 1).collect();
        return result.replacen("[", ": ", 1);
    }
    yaml_str
}

#[cfg(test)]
mod tests {
    use crate::create_pair;
    #[test]
    fn test_create_pair() {
        let name = "ID";
        let pair = create_pair(name);
        // assert_eq!(name, pair.indicator);
        assert!(pair.regex.to_string().contains(name));
    }

    use crate::cut_yaml;
    use crate::cut_yaml_idents;
    #[test]
    fn test_cut_yaml() {
        let pair = create_pair("ID");
        let result = cut_yaml(&pair, &"ID[Test]".to_string());
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_cut_yaml2() {
        let result = cut_yaml_idents(&["ID"], &"ID[Test]".to_string());
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_cut_yaml_distraction() {
        let pair = create_pair("ID");
        let result = cut_yaml(
            &pair,
            &"other stuff ID[Test, TestContent: 3] more stuff".to_string(),
        );
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].text, "{ID: Test, TestContent: 3}");
        // assert_eq!(result[0].start, 12);
        // assert_eq!(result[0].end, 35);
    }

    #[test]
    fn test_cut_yaml_multiple_entries() {
        let pair = create_pair("ID");
        let result = cut_yaml(&pair, &"other stuff ID[Test, TestContent: 3] more\n ID[Test2, TestContent: 4] stuID[Test3, TestContent: a7ad]ff".to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "{ID: Test, TestContent: 3}");
        assert_eq!(result[1].text, "{ID: Test2, TestContent: 4}");
        assert_eq!(result[2].text, "{ID: Test3, TestContent: a7ad}");
    }

    #[test]
    fn test_cut_yaml_multiple_entries2() {
        let result = cut_yaml_idents(&["ID"], &"other stuff ID[Test, TestContent: 3] more\n ID[Test2, TestContent: 4] stuID[Test3, TestContent: a7ad]ff".to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "{ID: Test, TestContent: 3}");
        assert_eq!(result[1].text, "{ID: Test2, TestContent: 4}");
        assert_eq!(result[2].text, "{ID: Test3, TestContent: a7ad}");
    }

    #[test]
    fn test_cut_yaml_multiple_lines() {
        let pair = create_pair("ID");
        let result = cut_yaml(&pair, &"other stuff ID[Test, \nTestContent: 3] more\n ID[Test2, \nTestContent: 4\n] stuID[Test3, TestContent: a7ad]ff".to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "{ID: Test, \nTestContent: 3}");
        // assert_eq!(result[0].start, 12);
        // assert_eq!(result[0].end, 36);
        assert_eq!(result[1].text, "{ID: Test2, \nTestContent: 4\n}");
        assert_eq!(result[2].text, "{ID: Test3, TestContent: a7ad}");
    }

    use crate::create_combined_pair;
    #[test]
    fn test_cut_yaml_many_id_multiple_entries() {
        let pair = create_combined_pair(&["ID", "REF", "ADD"]);
        let result = cut_yaml(&pair, &"other stuff ID[Test, TestContent: 3] more\n REF[Test, TestContent: 4] stuADD[Test3, TestContent: a7ad]ff".to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "{ID: Test, TestContent: 3}");
        assert_eq!(result[1].text, "{REF: Test, TestContent: 4}");
        assert_eq!(result[2].text, "{ADD: Test3, TestContent: a7ad}");
    }

    #[test]
    fn test_cut_yaml_nested() {
        let pair = create_combined_pair(&["ID", "REF", "ADD"]);
        let result = cut_yaml(&pair, &"other stuff ID[Test, \nTestContent: 3] more\n REF[Test2, \nTestContent: [4]\n] stuADD[Test3, TestContent: [[a,7],[a,d]]]ff".to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "{ID: Test, \nTestContent: 3}");
        assert_eq!(result[1].text, "{REF: Test2, \nTestContent: [4]\n}");
        assert_eq!(result[2].text, "{ADD: Test3, TestContent: [[a,7],[a,d]]}");
    }

    #[test]
    fn test_cut_yaml_escaped() {
        let pair = create_combined_pair(&["ID", "REF", "ADD"]);
        let result = cut_yaml(&pair, &r#"other stuff ID[Test, \nTestContent: ']3]]'] more\n REF[Test2, \nTestContent: [4]\n] stuADD[Test3, TestContent: [[a,7],[a,d]]]ff"#.to_string());
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, r#"{ID: Test, \nTestContent: ']3]]'}"#);
        assert_eq!(result[1].text, r#"{REF: Test2, \nTestContent: [4]\n}"#);
        assert_eq!(
            result[2].text,
            r#"{ADD: Test3, TestContent: [[a,7],[a,d]]}"#
        );
    }
}