neon_arch commited on
Commit
09227d8
1 Parent(s): df09ed9

✨ feat: move implementation of config file path to reduce duplication (#163)

Browse files
Files changed (1) hide show
  1. src/config/parser.rs +4 -52
src/config/parser.rs CHANGED
@@ -1,14 +1,12 @@
1
  //! This module provides the functionality to parse the lua config and convert the config options
2
  //! into rust readable form.
3
 
 
 
4
  use super::parser_models::Style;
5
  use log::LevelFilter;
6
  use rlua::Lua;
7
- use std::{collections::HashMap, format, fs, path::Path, thread::available_parallelism};
8
-
9
- // ------- Constants --------
10
- static COMMON_DIRECTORY_NAME: &str = "websurfx";
11
- static CONFIG_FILE_NAME: &str = "config.lua";
12
 
13
  /// A named struct which stores the parsed config file options.
14
  ///
@@ -69,7 +67,7 @@ impl Config {
69
  let globals = context.globals();
70
 
71
  context
72
- .load(&fs::read_to_string(Config::config_path()?)?)
73
  .exec()?;
74
 
75
  let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
@@ -114,52 +112,6 @@ impl Config {
114
  })
115
  })
116
  }
117
-
118
- /// A helper function which returns an appropriate config file path checking if the config
119
- /// file exists on that path.
120
- ///
121
- /// # Error
122
- ///
123
- /// Returns a `config file not found!!` error if the config file is not present under following
124
- /// paths which are:
125
- /// 1. `~/.config/websurfx/` if it not present here then it fallbacks to the next one (2)
126
- /// 2. `/etc/xdg/websurfx/config.lua` if it is not present here then it fallbacks to the next
127
- /// one (3).
128
- /// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
129
- /// here then it returns an error as mentioned above.
130
- fn config_path() -> Result<String, Box<dyn std::error::Error>> {
131
- // check user config
132
-
133
- let path = format!(
134
- "{}/.config/{}/config.lua",
135
- std::env::var("HOME").unwrap(),
136
- COMMON_DIRECTORY_NAME
137
- );
138
- if Path::new(path.as_str()).exists() {
139
- return Ok(format!(
140
- "{}/.config/{}/{}",
141
- std::env::var("HOME").unwrap(),
142
- COMMON_DIRECTORY_NAME,
143
- CONFIG_FILE_NAME
144
- ));
145
- }
146
-
147
- // look for config in /etc/xdg
148
- if Path::new(format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str())
149
- .exists()
150
- {
151
- return Ok("/etc/xdg/websurfx/config.lua".to_string());
152
- }
153
-
154
- // use dev config
155
- if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str()).exists()
156
- {
157
- return Ok("./websurfx/config.lua".to_string());
158
- }
159
-
160
- // if no of the configs above exist, return error
161
- Err("Config file not found!!".to_string().into())
162
- }
163
  }
164
 
165
  /// a helper function that sets the proper logging level
 
1
  //! This module provides the functionality to parse the lua config and convert the config options
2
  //! into rust readable form.
3
 
4
+ 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.
12
  ///
 
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")?;
 
112
  })
113
  })
114
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  }
116
 
117
  /// a helper function that sets the proper logging level