neon_arch commited on
Commit
4ccd048
1 Parent(s): 7b33744

⚙️ refactor: replace oncecell with oncelock from std library (#180)(#178)

Browse files
Files changed (2) hide show
  1. src/handler/paths.rs +58 -54
  2. src/results/user_agent.rs +21 -17
src/handler/paths.rs CHANGED
@@ -4,6 +4,7 @@
4
  use std::collections::HashMap;
5
  use std::io::Error;
6
  use std::path::Path;
 
7
 
8
  // ------- Constants --------
9
  static PUBLIC_DIRECTORY_NAME: &str = "public";
@@ -20,57 +21,7 @@ pub enum FileType {
20
  Theme,
21
  }
22
 
23
- static FILE_PATHS_FOR_DIFF_FILE_TYPES: once_cell::sync::Lazy<HashMap<FileType, Vec<String>>> =
24
- once_cell::sync::Lazy::new(|| {
25
- HashMap::from([
26
- (
27
- FileType::Config,
28
- vec![
29
- format!(
30
- "{}/.config/{}/{}",
31
- std::env::var("HOME").unwrap(),
32
- COMMON_DIRECTORY_NAME,
33
- CONFIG_FILE_NAME
34
- ),
35
- format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME),
36
- format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME),
37
- ],
38
- ),
39
- (
40
- FileType::Theme,
41
- vec![
42
- format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME),
43
- format!("./{}/", PUBLIC_DIRECTORY_NAME),
44
- ],
45
- ),
46
- (
47
- FileType::AllowList,
48
- vec![
49
- format!(
50
- "{}/.config/{}/{}",
51
- std::env::var("HOME").unwrap(),
52
- COMMON_DIRECTORY_NAME,
53
- ALLOWLIST_FILE_NAME
54
- ),
55
- format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME),
56
- format!("./{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME),
57
- ],
58
- ),
59
- (
60
- FileType::BlockList,
61
- vec![
62
- format!(
63
- "{}/.config/{}/{}",
64
- std::env::var("HOME").unwrap(),
65
- COMMON_DIRECTORY_NAME,
66
- BLOCKLIST_FILE_NAME
67
- ),
68
- format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME),
69
- format!("./{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME),
70
- ],
71
- ),
72
- ])
73
- });
74
 
75
  /// A helper function which returns an appropriate config file path checking if the config
76
  /// file exists on that path.
@@ -95,11 +46,64 @@ static FILE_PATHS_FOR_DIFF_FILE_TYPES: once_cell::sync::Lazy<HashMap<FileType, V
95
  /// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2)
96
  /// 2. Under project folder ( or codebase in other words) if it is not present
97
  /// here then it returns an error as mentioned above.
98
- pub fn file_path(file_type: FileType) -> Result<String, Error> {
99
- let file_path = FILE_PATHS_FOR_DIFF_FILE_TYPES.get(&file_type).unwrap();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  for (idx, _) in file_path.iter().enumerate() {
101
  if Path::new(file_path[idx].as_str()).exists() {
102
- return Ok(file_path[idx].clone());
103
  }
104
  }
105
 
 
4
  use std::collections::HashMap;
5
  use std::io::Error;
6
  use std::path::Path;
7
+ use std::sync::OnceLock;
8
 
9
  // ------- Constants --------
10
  static PUBLIC_DIRECTORY_NAME: &str = "public";
 
21
  Theme,
22
  }
23
 
24
+ static FILE_PATHS_FOR_DIFF_FILE_TYPES: OnceLock<HashMap<FileType, Vec<String>>> = OnceLock::new();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  /// A helper function which returns an appropriate config file path checking if the config
27
  /// file exists on that path.
 
46
  /// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2)
47
  /// 2. Under project folder ( or codebase in other words) if it is not present
48
  /// here then it returns an error as mentioned above.
49
+ pub fn file_path(file_type: FileType) -> Result<&'static str, Error> {
50
+ let file_path: &Vec<String> = FILE_PATHS_FOR_DIFF_FILE_TYPES
51
+ .get_or_init(|| {
52
+ HashMap::from([
53
+ (
54
+ FileType::Config,
55
+ vec![
56
+ format!(
57
+ "{}/.config/{}/{}",
58
+ std::env::var("HOME").unwrap(),
59
+ COMMON_DIRECTORY_NAME,
60
+ CONFIG_FILE_NAME
61
+ ),
62
+ format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME),
63
+ format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME),
64
+ ],
65
+ ),
66
+ (
67
+ FileType::Theme,
68
+ vec![
69
+ format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME),
70
+ format!("./{}/", PUBLIC_DIRECTORY_NAME),
71
+ ],
72
+ ),
73
+ (
74
+ FileType::AllowList,
75
+ vec![
76
+ format!(
77
+ "{}/.config/{}/{}",
78
+ std::env::var("HOME").unwrap(),
79
+ COMMON_DIRECTORY_NAME,
80
+ ALLOWLIST_FILE_NAME
81
+ ),
82
+ format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME),
83
+ format!("./{}/{}", COMMON_DIRECTORY_NAME, ALLOWLIST_FILE_NAME),
84
+ ],
85
+ ),
86
+ (
87
+ FileType::BlockList,
88
+ vec![
89
+ format!(
90
+ "{}/.config/{}/{}",
91
+ std::env::var("HOME").unwrap(),
92
+ COMMON_DIRECTORY_NAME,
93
+ BLOCKLIST_FILE_NAME
94
+ ),
95
+ format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME),
96
+ format!("./{}/{}", COMMON_DIRECTORY_NAME, BLOCKLIST_FILE_NAME),
97
+ ],
98
+ ),
99
+ ])
100
+ })
101
+ .get(&file_type)
102
+ .unwrap();
103
+
104
  for (idx, _) in file_path.iter().enumerate() {
105
  if Path::new(file_path[idx].as_str()).exists() {
106
+ return Ok(std::mem::take(&mut &*file_path[idx]));
107
  }
108
  }
109
 
src/results/user_agent.rs CHANGED
@@ -1,28 +1,32 @@
1
  //! This module provides the functionality to generate random user agent string.
2
 
 
 
3
  use fake_useragent::{Browsers, UserAgents, UserAgentsBuilder};
4
 
5
- static USER_AGENTS: once_cell::sync::Lazy<UserAgents> = once_cell::sync::Lazy::new(|| {
6
- UserAgentsBuilder::new()
7
- .cache(false)
8
- .dir("/tmp")
9
- .thread(1)
10
- .set_browsers(
11
- Browsers::new()
12
- .set_chrome()
13
- .set_safari()
14
- .set_edge()
15
- .set_firefox()
16
- .set_mozilla(),
17
- )
18
- .build()
19
- });
20
 
21
  /// A function to generate random user agent to improve privacy of the user.
22
  ///
23
  /// # Returns
24
  ///
25
  /// A randomly generated user agent string.
26
- pub fn random_user_agent() -> String {
27
- USER_AGENTS.random().to_string()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
 
1
  //! This module provides the functionality to generate random user agent string.
2
 
3
+ use std::sync::OnceLock;
4
+
5
  use fake_useragent::{Browsers, UserAgents, UserAgentsBuilder};
6
 
7
+ static USER_AGENTS: OnceLock<UserAgents> = OnceLock::new();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  /// A function to generate random user agent to improve privacy of the user.
10
  ///
11
  /// # Returns
12
  ///
13
  /// A randomly generated user agent string.
14
+ pub fn random_user_agent() -> &'static str {
15
+ USER_AGENTS
16
+ .get_or_init(|| {
17
+ UserAgentsBuilder::new()
18
+ .cache(false)
19
+ .dir("/tmp")
20
+ .thread(1)
21
+ .set_browsers(
22
+ Browsers::new()
23
+ .set_chrome()
24
+ .set_safari()
25
+ .set_edge()
26
+ .set_firefox()
27
+ .set_mozilla(),
28
+ )
29
+ .build()
30
+ })
31
+ .random()
32
  }