neon_arch
commited on
Commit
·
e4476aa
1
Parent(s):
5f1a439
⚙️ refactor: replace rlua with mlua code implementation (#180)(#178)
Browse files- src/config/parser.rs +42 -42
src/config/parser.rs
CHANGED
@@ -5,7 +5,7 @@ use crate::handler::paths::{file_path, FileType};
|
|
5 |
|
6 |
use super::parser_models::Style;
|
7 |
use log::LevelFilter;
|
8 |
-
use
|
9 |
use std::{collections::HashMap, fs, thread::available_parallelism};
|
10 |
|
11 |
/// A named struct which stores the parsed config file options.
|
@@ -63,53 +63,53 @@ impl Config {
|
|
63 |
/// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
|
64 |
/// Config struct with all the parsed config options from the parsed config file.
|
65 |
pub fn parse(logging_initialized: bool) -> Result<Self, Box<dyn std::error::Error>> {
|
66 |
-
Lua::new()
|
67 |
-
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
.exec()?;
|
72 |
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
}
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
})
|
113 |
})
|
114 |
}
|
115 |
}
|
|
|
5 |
|
6 |
use super::parser_models::Style;
|
7 |
use log::LevelFilter;
|
8 |
+
use mlua::Lua;
|
9 |
use std::{collections::HashMap, fs, thread::available_parallelism};
|
10 |
|
11 |
/// A named struct which stores the parsed config file options.
|
|
|
63 |
/// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
|
64 |
/// Config struct with all the parsed config options from the parsed config file.
|
65 |
pub fn parse(logging_initialized: bool) -> Result<Self, Box<dyn std::error::Error>> {
|
66 |
+
let lua = Lua::new();
|
67 |
+
let globals = lua.globals();
|
68 |
|
69 |
+
lua.load(&fs::read_to_string(file_path(FileType::Config)?)?)
|
70 |
+
.exec()?;
|
|
|
71 |
|
72 |
+
let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
|
73 |
|
74 |
+
let debug: bool = globals.get::<_, bool>("debug")?;
|
75 |
+
let logging: bool = globals.get::<_, bool>("logging")?;
|
76 |
|
77 |
+
if !logging_initialized {
|
78 |
+
set_logging_level(debug, logging);
|
79 |
+
}
|
80 |
|
81 |
+
let threads: u8 = if parsed_threads == 0 {
|
82 |
+
let total_num_of_threads: usize = available_parallelism()?.get() / 2;
|
83 |
+
log::error!(
|
84 |
+
"Config Error: The value of `threads` option should be a non zero positive integer"
|
85 |
+
);
|
86 |
+
log::error!("Falling back to using {} threads", total_num_of_threads);
|
87 |
+
total_num_of_threads as u8
|
88 |
+
} else {
|
89 |
+
parsed_threads
|
90 |
+
};
|
91 |
|
92 |
+
Ok(Config {
|
93 |
+
port: globals.get::<_, u16>("port")?,
|
94 |
+
binding_ip: globals.get::<_, String>("binding_ip")?,
|
95 |
+
style: Style::new(
|
96 |
+
globals.get::<_, String>("theme")?,
|
97 |
+
globals.get::<_, String>("colorscheme")?,
|
98 |
+
),
|
99 |
+
redis_url: globals.get::<_, String>("redis_url")?,
|
100 |
+
aggregator: AggregatorConfig {
|
101 |
+
random_delay: globals.get::<_, bool>("production_use")?,
|
102 |
+
},
|
103 |
+
logging,
|
104 |
+
debug,
|
105 |
+
upstream_search_engines: globals
|
106 |
+
.get::<_, HashMap<String, bool>>("upstream_search_engines")?
|
107 |
+
.into_iter()
|
108 |
+
.filter_map(|(key, value)| value.then_some(key))
|
109 |
+
.filter_map(|engine| crate::engines::engine_models::EngineHandler::new(&engine))
|
110 |
+
.collect(),
|
111 |
+
request_timeout: globals.get::<_, u8>("request_timeout")?,
|
112 |
+
threads,
|
|
|
113 |
})
|
114 |
}
|
115 |
}
|