neutralts/bif/
parse_bif_replace.rs

1#![doc = include_str!("../../doc/bif-replace.md")]
2
3use crate::{bif::constants::*, bif::Bif, bif::BifError, constants::*};
4
5impl<'a> Bif<'a> {
6    /*
7        {:replace; /from/to/ >> ... :}
8        /from/to/, ~from~to~, |from|to|, ...
9    */
10    pub(crate) fn parse_bif_replace(&mut self) -> Result<(), BifError> {
11        if self.mod_filter || self.mod_negate || self.mod_scope {
12            return Err(self.bif_error(BIF_ERROR_MODIFIER_NOT_ALLOWED));
13        }
14
15        self.extract_params_code(false);
16
17        if self.params.contains("{:flg;") {
18            return Err(self.bif_error(BIF_ERROR_FLAGS_NOT_ALLOWED));
19        }
20
21        let args = self.extract_args();
22
23        let from = args
24            .get(1)
25            .cloned()
26            .ok_or_else(|| self.bif_error(BIF_ERROR_ARGUMENTS_NOT_FOUND))?;
27
28        let to = args
29            .get(2)
30            .cloned()
31            .ok_or_else(|| self.bif_error(BIF_ERROR_ARGUMENTS_NOT_FOUND))?;
32
33        if self.code.contains(BIF_OPEN) {
34            self.code = new_child_parse!(self, &self.code, self.mod_scope);
35        }
36
37        self.out = self.code.replace(&from, &to);
38
39        Ok(())
40    }
41}
42
43#[cfg(test)]
44#[path = "parse_bif_replace_tests.rs"]
45mod tests;