neutralts/bif/
parse_bif_count.rs

1#![doc = include_str!("../../doc/bif-count.md")]
2
3/*
4    .------------.
5    | DEPRECATED |
6    '------------'
7*/
8
9use crate::{
10    constants::*,
11    bif::Bif,
12    bif::BifError,
13    bif::constants::*,
14};
15
16impl<'a> Bif<'a> {
17    /*
18        This bif is poorly designed, it also sets the values globally
19        which can cause variables to be overwritten.
20
21        {:count; name >> 0 :}
22        {:count; name :}
23    */
24    pub(crate) fn parse_bif_count(&mut self) -> Result<(), BifError> {
25        if self.mod_filter || self.mod_negate || self.mod_scope {
26            return Err(self.bif_error(BIF_ERROR_MODIFIER_NOT_ALLOWED));
27        }
28
29        let is_set = self.extract_params_code(true);
30
31        if self.code.contains(BIF_OPEN) {
32            self.code = new_child_parse!(self, &self.code, self.mod_scope);
33        }
34
35        if is_set {
36            let count_name = self.params.clone();
37            let count_value = match self.code.parse::<i32>() {
38                Ok(num) => num,
39                Err(_) => {
40                    return Err(self.bif_error(BIF_ERROR_ARGUMENT_NOT_NUMBER));
41                }
42            };
43
44            self.set_data(&count_name, &count_value.to_string());
45            self.out = EMPTY_STRING;
46        } else {
47            let count_name = self.code.clone();
48            let count_value = match self.get_data(&count_name).parse::<i32>() {
49                Ok(num) => num,
50                Err(_) => {
51                    return Err(self.bif_error(BIF_ERROR_ARGUMENT_NOT_NUMBER));
52                }
53            };
54            let new_value = count_value + 1;
55
56            self.set_data(&count_name, &new_value.to_string());
57            self.out = count_value.to_string();
58        }
59
60        Err(self.bif_error(BIF_ERROR_BIF_DEPRECATED))
61    }
62}