neon_arch commited on
Commit
89796a0
1 Parent(s): 4079255

feat: add ability to put config file on different paths

Browse files
Files changed (1) hide show
  1. src/config_parser/parser.rs +39 -5
src/config_parser/parser.rs CHANGED
@@ -3,7 +3,7 @@
3
 
4
  use super::parser_models::Style;
5
  use rlua::Lua;
6
- use std::fs;
7
 
8
  /// A named struct which stores the parsed config file options.
9
  ///
@@ -32,13 +32,13 @@ impl Config {
32
  /// or io error if the config.lua file doesn't exists otherwise it returns a newly contructed
33
  /// Config struct with all the parsed config options from the parsed config file.
34
  pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
35
- let lua = Lua::new();
36
-
37
- lua.context(|context| {
38
  let globals = context.globals();
39
 
40
  context
41
- .load(&fs::read_to_string("./websurfx/config.lua")?)
 
 
42
  .exec()?;
43
 
44
  Ok(Config {
@@ -52,4 +52,38 @@ impl Config {
52
  })
53
  })
54
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  }
 
3
 
4
  use super::parser_models::Style;
5
  use rlua::Lua;
6
+ use std::{format, fs, path::Path};
7
 
8
  /// A named struct which stores the parsed config file options.
9
  ///
 
32
  /// or io error if the config.lua file doesn't exists otherwise it returns a newly contructed
33
  /// Config struct with all the parsed config options from the parsed config file.
34
  pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
35
+ Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
 
 
36
  let globals = context.globals();
37
 
38
  context
39
+ .load(&fs::read_to_string(
40
+ Config::handle_different_config_file_path()?,
41
+ )?)
42
  .exec()?;
43
 
44
  Ok(Config {
 
52
  })
53
  })
54
  }
55
+ /// A helper function which returns an appropriate config file path checking if the config
56
+ /// file exists on that path.
57
+ ///
58
+ /// # Error
59
+ ///
60
+ /// Returns a `config file not found!!` error if the config file is not present under following
61
+ /// paths which are:
62
+ /// 1. `~/.config/websurfx/` if it not present here then it fallbacks to the next one (2)
63
+ /// 2. `/etc/xdg/websurfx/config.lua` if it is not present here then it fallbacks to the next
64
+ /// one (3).
65
+ /// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
66
+ /// here then it returns an error as mentioned above.
67
+ fn handle_different_config_file_path() -> Result<String, Box<dyn std::error::Error>> {
68
+ if Path::new(
69
+ format!(
70
+ "{}/.config/websurfx/config.lua",
71
+ std::env::var("HOME").unwrap()
72
+ )
73
+ .as_str(),
74
+ )
75
+ .exists()
76
+ {
77
+ Ok(format!(
78
+ "{}/.config/websurfx/config.lua",
79
+ std::env::var("HOME").unwrap()
80
+ ))
81
+ } else if Path::new("/etc/xdg/websurfx/config.lua").exists() {
82
+ Ok("/etc/xdg/websurfx/config.lua".to_string())
83
+ } else if Path::new("./websurfx/config.lua").exists() {
84
+ Ok("./websurfx/config.lua".to_string())
85
+ } else {
86
+ Err(format!("Config file not found!!").into())
87
+ }
88
+ }
89
  }