neutralts/bif/
parse_bif_count.rs

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