neutralts/
shared.rs

1use serde_json::Value;
2use std::env;
3use crate::utils::{get_from_key, is_bool_key};
4
5pub(crate) struct Shared {
6    pub(crate) schema: Value,
7    pub(crate) lang: String,
8    pub(crate) comments: String,
9    pub(crate) bisf_count: u64,
10    pub(crate) bisf_max: u64,
11    pub(crate) flags: String,
12    pub(crate) exit: bool,
13    pub(crate) has_error: bool,
14    pub(crate) status_code: String,
15    pub(crate) status_text: String,
16    pub(crate) status_param: String,
17    pub(crate) redirect_js: String,
18    pub(crate) filter_all: bool,
19    pub(crate) filter_bifs: bool,
20    pub(crate) cache_prefix: String,
21    pub(crate) cache_dir: String,
22    pub(crate) cache_on_post: bool,
23    pub(crate) cache_on_get: bool,
24    pub(crate) cache_on_cookies: bool,
25    pub(crate) cache_disable: bool,
26    pub(crate) disable_js: bool,
27    pub(crate) already_js: bool,
28    pub(crate) debug_expire: u64,
29    pub(crate) debug_file: String,
30    pub(crate) working_dir: String,
31}
32
33impl Shared {
34    pub(crate) fn new(schema: Value) -> Self {
35        let bisf_max = schema["config"]["infinite_loop_max_bifs"].as_u64().unwrap();
36        let comments = get_from_key(&schema["config"], "comments");
37        let lang = get_from_key(&schema["inherit"]["locale"], "current");
38        let filter_all = is_bool_key(&schema["config"], "filter_all");
39        let cache_prefix = get_from_key(&schema["config"], "cache_prefix");
40        let mut cache_dir = get_from_key(&schema["config"], "cache_dir");
41        let working_dir   = env::current_dir().unwrap().to_string_lossy().into_owned();
42        let cache_on_post= is_bool_key(&schema["config"], "cache_on_post");
43        let cache_on_get= is_bool_key(&schema["config"], "cache_on_get");
44        let cache_on_cookies= is_bool_key(&schema["config"], "cache_on_cookies");
45        let cache_disable= is_bool_key(&schema["config"], "cache_disable");
46        let disable_js= is_bool_key(&schema["config"], "disable_js");
47        let debug_expire = schema["config"]["debug_expire"].as_u64().unwrap();
48        let debug_file= get_from_key(&schema["config"], "debug_file");
49        let mut filter_bifs = false;
50
51        if !cache_disable {
52            filter_bifs = true;
53        }
54
55        if cache_dir.is_empty() {
56            cache_dir = env::temp_dir().to_string_lossy().into_owned();
57        }
58
59        Shared {
60            schema,
61            lang,
62            comments,
63            bisf_count: 0,
64            bisf_max,
65            flags: String::new(),
66            exit: false,
67            has_error: false,
68            status_code: "200".to_string(),
69            status_text: "OK".to_string(),
70            status_param: String::new(),
71            redirect_js: String::new(),
72            filter_all,
73            filter_bifs,
74            cache_prefix,
75            cache_dir,
76            cache_on_post,
77            cache_on_get,
78            cache_on_cookies,
79            cache_disable,
80            disable_js,
81            already_js: false,
82            debug_expire,
83            debug_file,
84            working_dir,
85        }
86    }
87}