neutralts/
shared.rs

1use crate::utils::{get_from_key, is_bool_key};
2use serde_json::{json, Value};
3use std::collections::HashMap;
4use std::env;
5use std::rc::Rc;
6
7pub(crate) struct Shared {
8    pub(crate) schema: Value,
9    pub(crate) indir_store: HashMap<String, Rc<Value>>,
10    pub(crate) lang: String,
11    pub(crate) comments: String,
12    pub(crate) bisf_count: u64,
13    pub(crate) bisf_max: u64,
14    pub(crate) flags: String,
15    pub(crate) exit: bool,
16    pub(crate) has_error: bool,
17    pub(crate) status_code: String,
18    pub(crate) status_text: String,
19    pub(crate) status_param: String,
20    pub(crate) redirect_js: String,
21    pub(crate) filter_all: bool,
22    pub(crate) filter_bifs: bool,
23    pub(crate) cache_prefix: String,
24    pub(crate) cache_dir: String,
25    pub(crate) cache_on_post: bool,
26    pub(crate) cache_on_get: bool,
27    pub(crate) cache_on_cookies: bool,
28    pub(crate) cache_disable: bool,
29    pub(crate) disable_js: bool,
30    pub(crate) already_js: bool,
31    pub(crate) debug_expire: u64,
32    pub(crate) debug_file: String,
33    pub(crate) working_dir: String,
34}
35
36impl Shared {
37    pub(crate) fn new(schema: Value) -> Self {
38        let bisf_max = schema["config"]["infinite_loop_max_bifs"].as_u64().unwrap();
39        let comments = get_from_key(&schema["config"], "comments");
40        let lang = get_from_key(&schema["inherit"]["locale"], "current");
41        let filter_all = is_bool_key(&schema["config"], "filter_all");
42        let cache_prefix = get_from_key(&schema["config"], "cache_prefix");
43        let mut cache_dir = get_from_key(&schema["config"], "cache_dir");
44        let working_dir = env::current_dir().unwrap().to_string_lossy().into_owned();
45        let cache_on_post = is_bool_key(&schema["config"], "cache_on_post");
46        let cache_on_get = is_bool_key(&schema["config"], "cache_on_get");
47        let cache_on_cookies = is_bool_key(&schema["config"], "cache_on_cookies");
48        let cache_disable = is_bool_key(&schema["config"], "cache_disable");
49        let disable_js = is_bool_key(&schema["config"], "disable_js");
50        let debug_expire = schema["config"]["debug_expire"].as_u64().unwrap();
51        let debug_file = get_from_key(&schema["config"], "debug_file");
52        let mut filter_bifs = false;
53
54        if !cache_disable {
55            filter_bifs = true;
56        }
57
58        if cache_dir.is_empty() {
59            cache_dir = env::temp_dir().to_string_lossy().into_owned();
60        }
61
62        Shared {
63            schema,
64            indir_store: HashMap::new(),
65            lang,
66            comments,
67            bisf_count: 0,
68            bisf_max,
69            flags: String::new(),
70            exit: false,
71            has_error: false,
72            status_code: "200".to_string(),
73            status_text: "OK".to_string(),
74            status_param: String::new(),
75            redirect_js: String::new(),
76            filter_all,
77            filter_bifs,
78            cache_prefix,
79            cache_dir,
80            cache_on_post,
81            cache_on_get,
82            cache_on_cookies,
83            cache_disable,
84            disable_js,
85            already_js: false,
86            debug_expire,
87            debug_file,
88            working_dir,
89        }
90    }
91
92    /// Read - no clone
93    pub(crate) fn get_indir(&self, key: &str) -> &Value {
94        static EMPTY: Value = Value::Null;
95        self.indir_store
96            .get(key)
97            .map(|rc| rc.as_ref())
98            .unwrap_or(&EMPTY)
99    }
100
101    /// Write - clone-on-write
102    pub(crate) fn get_indir_mut(&mut self, key: &str) -> &mut Value {
103        let entry = self.indir_store
104            .entry(key.to_string())
105            .or_insert_with(|| Rc::new(json!({})));
106        Rc::make_mut(entry)
107    }
108}