neutralts/bif/
parse_bif_snippet.rs

1#![doc = include_str!("../../doc/bif-snippet.md")]
2
3use crate::{bif::constants::*, bif::Bif, bif::BifError, constants::*, json, utils::*};
4use std::collections::HashSet;
5
6impl<'a> Bif<'a> {
7    /*
8        Play snippet:
9        {:snippet; snippet-name :}
10
11        Set snippet:
12        {:snippet; snippet-name >>
13            content to set
14        :}
15    */
16    pub(crate) fn parse_bif_snippet(&mut self) -> Result<(), BifError> {
17        if self.mod_filter || self.mod_negate || self.mod_scope {
18            return Err(self.bif_error(BIF_ERROR_MODIFIER_NOT_ALLOWED));
19        }
20
21        self.alias = "snippet".to_string();
22
23        let is_set = self.extract_params_code(true);
24
25        if !self.flags.is_empty() {
26            let flags_allowed: HashSet<&str> = ["static"].into_iter().collect();
27
28            for f in self.flags.split('|').filter(|s| !s.is_empty()) {
29                if !flags_allowed.contains(f) {
30                    return Err(self.bif_error(&format!("{} flag not allowed", f)));
31                }
32            }
33        }
34
35        if is_set {
36            // Set snippets in snippet files and inside snippets
37            if self.inherit.current_file.contains(SNIPPETS_FILES) || self.inherit.alias == "snippet"
38            {
39                if self.flags.contains("|static|") {
40                    self.code = new_child_parse!(self, &self.code, self.mod_scope);
41                } else {
42                    // required regardless of mod_scope or static
43                    self.inherit.create_block_schema(self.shared);
44                }
45                self.shared.schema["__indir"][&self.inherit.indir]["snippets"][&self.params] =
46                    json!(&self.code);
47
48                // The directory inside the snippet is that of the template that created it.
49                self.shared.schema["__indir"][&self.inherit.indir]["snippets_set_dir"]
50                    [&self.params] = json!(&self.inherit.current_dir);
51
52                self.out = EMPTY_STRING;
53
54                Ok(())
55            } else {
56                return Err(self.bif_error("snippet cannot be set here"));
57            }
58        } else {
59            // parse snippet name if need
60            if self.code.contains(BIF_OPEN) {
61                self.code = new_child_parse!(self, &self.code, false);
62            }
63            let snip_name = self.code.clone();
64
65            self.code = get_from_key(
66                &self.shared.schema["__indir"][&self.inherit.indir]["snippets"],
67                &self.code,
68            );
69
70            if self.code.contains(BIF_OPEN) {
71                // The directory inside the snippet is that of the template that created it.
72                let set_dir = get_from_key(
73                    &self.shared.schema["__indir"][&self.inherit.indir]["snippets_set_dir"],
74                    &snip_name,
75                );
76
77                if !set_dir.is_empty() {
78                    self.inherit.current_dir = set_dir;
79                }
80
81                // auto mod_scope in snippets for snippets inside snippets
82                self.code = new_child_parse!(self, &self.code, self.code.contains("{:snip"));
83            }
84
85            self.out = self.code.to_string();
86
87            Ok(())
88        }
89    }
90}
91
92#[cfg(test)]
93#[path = "parse_bif_snippet_tests.rs"]
94mod tests;