neon_arch commited on
Commit
e4476aa
1 Parent(s): 5f1a439

⚙️ refactor: replace rlua with mlua code implementation (#180)(#178)

Browse files
Files changed (1) hide show
  1. 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 rlua::Lua;
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().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
67
- let globals = context.globals();
68
 
69
- context
70
- .load(&fs::read_to_string(file_path(FileType::Config)?)?)
71
- .exec()?;
72
 
73
- let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
74
 
75
- let debug: bool = globals.get::<_, bool>("debug")?;
76
- let logging:bool= globals.get::<_, bool>("logging")?;
77
 
78
- if !logging_initialized {
79
- set_logging_level(debug, logging);
80
- }
81
 
82
- let threads: u8 = if parsed_threads == 0 {
83
- let total_num_of_threads: usize = available_parallelism()?.get() / 2;
84
- log::error!("Config Error: The value of `threads` option should be a non zero positive integer");
85
- log::error!("Falling back to using {} threads", total_num_of_threads);
86
- total_num_of_threads as u8
87
- } else {
88
- parsed_threads
89
- };
 
 
90
 
91
- Ok(Config {
92
- port: globals.get::<_, u16>("port")?,
93
- binding_ip: globals.get::<_, String>("binding_ip")?,
94
- style: Style::new(
95
- globals.get::<_, String>("theme")?,
96
- globals.get::<_, String>("colorscheme")?,
97
- ),
98
- redis_url: globals.get::<_, String>("redis_url")?,
99
- aggregator: AggregatorConfig {
100
- random_delay: globals.get::<_, bool>("production_use")?,
101
- },
102
- logging,
103
- debug,
104
- upstream_search_engines: globals
105
- .get::<_, HashMap<String, bool>>("upstream_search_engines")?
106
- .into_iter()
107
- .filter_map(|(key, value)| value.then_some(key))
108
- .filter_map(|engine| crate::engines::engine_models::EngineHandler::new(&engine))
109
- .collect(),
110
- request_timeout: globals.get::<_, u8>("request_timeout")?,
111
- threads,
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
  }