Spaces:
Running
Running
Milim
commited on
Commit
·
e2907ed
1
Parent(s):
1f90b4e
refactor config helper function
Browse files
src/config_parser/parser.rs
CHANGED
|
@@ -24,46 +24,35 @@ pub struct Config {
|
|
| 24 |
pub binding_ip_addr: String,
|
| 25 |
pub style: Style,
|
| 26 |
pub redis_connection_url: String,
|
| 27 |
-
pub aggregator:
|
| 28 |
pub logging: bool,
|
| 29 |
pub debug: bool,
|
| 30 |
}
|
| 31 |
|
| 32 |
/// Configuration options for the aggregator.
|
| 33 |
#[derive(Clone)]
|
| 34 |
-
pub struct
|
| 35 |
/// Whether to introduce a random delay before sending the request to the search engine.
|
| 36 |
pub random_delay: bool,
|
| 37 |
}
|
| 38 |
|
| 39 |
impl Config {
|
| 40 |
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
| 41 |
-
///
|
| 42 |
///
|
| 43 |
/// # Error
|
| 44 |
///
|
| 45 |
/// Returns a lua parse error if parsing of the config.lua file fails or has a syntax error
|
| 46 |
-
/// or io error if the config.lua file doesn't exists otherwise it returns a newly
|
| 47 |
/// Config struct with all the parsed config options from the parsed config file.
|
| 48 |
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
|
| 49 |
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
| 50 |
let globals = context.globals();
|
| 51 |
|
| 52 |
context
|
| 53 |
-
.load(&fs::read_to_string(
|
| 54 |
-
Config::handle_different_config_file_path()?,
|
| 55 |
-
)?)
|
| 56 |
.exec()?;
|
| 57 |
|
| 58 |
-
let production_use = globals.get::<_, bool>("production_use")?;
|
| 59 |
-
let aggregator_config = if production_use {
|
| 60 |
-
AggreatorConfig { random_delay: true }
|
| 61 |
-
} else {
|
| 62 |
-
AggreatorConfig {
|
| 63 |
-
random_delay: false,
|
| 64 |
-
}
|
| 65 |
-
};
|
| 66 |
-
|
| 67 |
Ok(Config {
|
| 68 |
port: globals.get::<_, u16>("port")?,
|
| 69 |
binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?,
|
|
@@ -72,7 +61,9 @@ impl Config {
|
|
| 72 |
globals.get::<_, String>("colorscheme")?,
|
| 73 |
),
|
| 74 |
redis_connection_url: globals.get::<_, String>("redis_connection_url")?,
|
| 75 |
-
aggregator:
|
|
|
|
|
|
|
| 76 |
logging: globals.get::<_, bool>("logging")?,
|
| 77 |
debug: globals.get::<_, bool>("debug")?,
|
| 78 |
})
|
|
@@ -90,35 +81,37 @@ impl Config {
|
|
| 90 |
/// one (3).
|
| 91 |
/// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
|
| 92 |
/// here then it returns an error as mentioned above.
|
| 93 |
-
fn
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
{
|
| 104 |
-
Ok(format!(
|
| 105 |
"{}/.config/{}/{}",
|
| 106 |
std::env::var("HOME").unwrap(),
|
| 107 |
COMMON_DIRECTORY_NAME,
|
| 108 |
CONFIG_FILE_NAME
|
| 109 |
-
))
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
.
|
| 114 |
-
{
|
| 115 |
-
Ok("/etc/xdg/websurfx/config.lua".to_string())
|
| 116 |
-
} else if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str())
|
| 117 |
.exists()
|
| 118 |
{
|
| 119 |
-
Ok("
|
| 120 |
-
} else {
|
| 121 |
-
Err("Config file not found!!".to_string().into())
|
| 122 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
}
|
|
|
|
| 24 |
pub binding_ip_addr: String,
|
| 25 |
pub style: Style,
|
| 26 |
pub redis_connection_url: String,
|
| 27 |
+
pub aggregator: AggregatorConfig,
|
| 28 |
pub logging: bool,
|
| 29 |
pub debug: bool,
|
| 30 |
}
|
| 31 |
|
| 32 |
/// Configuration options for the aggregator.
|
| 33 |
#[derive(Clone)]
|
| 34 |
+
pub struct AggregatorConfig {
|
| 35 |
/// Whether to introduce a random delay before sending the request to the search engine.
|
| 36 |
pub random_delay: bool,
|
| 37 |
}
|
| 38 |
|
| 39 |
impl Config {
|
| 40 |
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
| 41 |
+
/// constructed Config struct and returns it.
|
| 42 |
///
|
| 43 |
/// # Error
|
| 44 |
///
|
| 45 |
/// Returns a lua parse error if parsing of the config.lua file fails or has a syntax error
|
| 46 |
+
/// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
|
| 47 |
/// Config struct with all the parsed config options from the parsed config file.
|
| 48 |
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
|
| 49 |
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
| 50 |
let globals = context.globals();
|
| 51 |
|
| 52 |
context
|
| 53 |
+
.load(&fs::read_to_string(Config::get_config_path()?)?)
|
|
|
|
|
|
|
| 54 |
.exec()?;
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
Ok(Config {
|
| 57 |
port: globals.get::<_, u16>("port")?,
|
| 58 |
binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?,
|
|
|
|
| 61 |
globals.get::<_, String>("colorscheme")?,
|
| 62 |
),
|
| 63 |
redis_connection_url: globals.get::<_, String>("redis_connection_url")?,
|
| 64 |
+
aggregator: AggregatorConfig {
|
| 65 |
+
random_delay: globals.get::<_, bool>("production_use")?,
|
| 66 |
+
},
|
| 67 |
logging: globals.get::<_, bool>("logging")?,
|
| 68 |
debug: globals.get::<_, bool>("debug")?,
|
| 69 |
})
|
|
|
|
| 81 |
/// one (3).
|
| 82 |
/// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
|
| 83 |
/// here then it returns an error as mentioned above.
|
| 84 |
+
fn get_config_path() -> Result<String, Box<dyn std::error::Error>> {
|
| 85 |
+
// check user config
|
| 86 |
+
|
| 87 |
+
let path = format!(
|
| 88 |
+
"{}/.config/{}/config.lua",
|
| 89 |
+
std::env::var("HOME").unwrap(),
|
| 90 |
+
COMMON_DIRECTORY_NAME
|
| 91 |
+
);
|
| 92 |
+
if Path::new(path.as_str()).exists() {
|
| 93 |
+
return Ok(format!(
|
|
|
|
|
|
|
| 94 |
"{}/.config/{}/{}",
|
| 95 |
std::env::var("HOME").unwrap(),
|
| 96 |
COMMON_DIRECTORY_NAME,
|
| 97 |
CONFIG_FILE_NAME
|
| 98 |
+
));
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
// look for config in /etc/xdg
|
| 102 |
+
if Path::new(format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str())
|
|
|
|
|
|
|
|
|
|
| 103 |
.exists()
|
| 104 |
{
|
| 105 |
+
return Ok("/etc/xdg/websurfx/config.lua".to_string());
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
+
|
| 108 |
+
// use dev config
|
| 109 |
+
if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str()).exists()
|
| 110 |
+
{
|
| 111 |
+
return Ok("./websurfx/config.lua".to_string());
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
// if no of the configs above exist, return error
|
| 115 |
+
Err("Config file not found!!".to_string().into())
|
| 116 |
}
|
| 117 |
}
|
src/config_parser/parser_models.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
//! This module provides public models for handling, storing and serializing parsed config file
|
| 2 |
-
//! options from config.lua by grouping them
|
| 3 |
|
| 4 |
use serde::{Deserialize, Serialize};
|
| 5 |
|
|
|
|
| 1 |
//! This module provides public models for handling, storing and serializing parsed config file
|
| 2 |
+
//! options from config.lua by grouping them together.
|
| 3 |
|
| 4 |
use serde::{Deserialize, Serialize};
|
| 5 |
|