neutralts/bif/
parse_bif_hash.rs

1#![doc = include_str!("../../doc/bif-hash.md")]
2
3use crate::{bif::constants::*, bif::Bif, bif::BifError, constants::*};
4use md5::{Digest, Md5};
5use rand::Rng;
6
7impl<'a> Bif<'a> {
8    /*
9        {:hash;  :}
10        {:hash; text :}
11    */
12    pub(crate) fn parse_bif_hash(&mut self) -> Result<(), BifError> {
13        if self.mod_filter || self.mod_negate || self.mod_scope {
14            return Err(self.bif_error(BIF_ERROR_MODIFIER_NOT_ALLOWED));
15        }
16
17        self.code = self.src.trim().to_string();
18
19        if self.src.contains(BIF_OPEN) {
20            self.code = new_child_parse!(self, &self.code, self.mod_scope);
21        } else {
22            self.code = self.code.trim().to_string();
23        }
24
25        if self.code.is_empty() {
26            let mut hasher = Md5::new();
27            let mut rng = rand::rng();
28            let rand = rng.random_range(100000000..=999999999).to_string();
29            hasher.update(&rand);
30            self.out = format!("{:x}", hasher.finalize())
31        } else {
32            let mut hasher = Md5::new();
33            hasher.update(&self.code);
34            self.out = format!("{:x}", hasher.finalize());
35        }
36
37        Ok(())
38    }
39}
40
41#[cfg(test)]
42#[path = "parse_bif_hash_tests.rs"]
43mod tests;