neutralts/bif/
parse_bif_allow.rs

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
#![doc = include_str!("../../doc/bif-allow.md")]

use crate::{bif::constants::*, bif::Bif, bif::BifError, constants::*, utils::*};
use std::collections::HashSet;

impl<'a> Bif<'a> {
    /*
        {:allow; {:flg; partial casein replace :} name >> ... :}
    */
    pub(crate) fn parse_bif_allow(&mut self) -> Result<(), BifError> {
        if self.mod_filter || self.mod_scope {
            return Err(self.bif_error(BIF_ERROR_MODIFIER_NOT_ALLOWED));
        }

        self.extract_params_code(true);
        let mut found = String::new();
        let words_string = get_from_key(
            &self.shared.schema["__indir"][&self.inherit.indir]["declare"],
            &self.params,
        );

        if words_string.is_empty() {
            return Err(self.bif_error(&(self.params.clone() + BIF_ERROR_DECLARED_IS_EMPTY)));
        }

        if !self.flags.is_empty() {
            let flags_allowed: HashSet<&str> =
                ["partial", "replace", "casein"].into_iter().collect();

            for f in self.flags.split('|').filter(|s| !s.is_empty()) {
                if !flags_allowed.contains(f) {
                    return Err(self.bif_error(&format!("{} flag not allowed", f)));
                }
            }
        }

        let mut words_list: Vec<&str> = words_string.split_whitespace().collect();
        self.code = new_child_parse!(self, &self.code, self.mod_scope);

        for word in &mut words_list {
            let lower_haystack;
            let mut haystack = &self.code;
            let mut pattern = word.to_string().clone();

            if self.flags.contains("|partial|") || self.flags.contains("|replace|") {
                pattern = format!("{}{}{}", "*", pattern, "*");
            }

            if self.flags.contains("|casein|") {
                pattern = pattern.to_lowercase();
                lower_haystack = self.code.clone().to_lowercase();
                haystack = &lower_haystack;
            }

            if wildcard_match(haystack, &pattern) {
                found = word.to_string();
                break;
            }
        }

        if !found.is_empty() ^ self.mod_negate {
            if self.flags.contains("|replace|") {
                found = found.replace("~", "");
                found = found.replace("*", "");
                found = found.replace("?", "");
                found = found.replace(".", "");
                self.out = found.to_string();
            } else {
                self.out = self.code.to_string();
            }
        } else {
            self.out = EMPTY_STRING;
        }

        Ok(())
    }
}

#[cfg(test)]
#[path = "parse_bif_allow_tests.rs"]
mod tests;