neon_arch commited on
Commit
fda6c3a
1 Parent(s): 4f28711

✨ feat: rename public_paths to paths (#163)

Browse files
src/handler/mod.rs CHANGED
@@ -1 +1 @@
1
- pub mod public_paths;
 
1
+ pub mod paths;
src/handler/paths.rs ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! This module provides the functionality to handle theme folder present on different paths and
2
+ //! provide one appropriate path on which it is present and can be used.
3
+
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";
10
+ static COMMON_DIRECTORY_NAME: &str = "websurfx";
11
+ static CONFIG_FILE_NAME: &str = "config.lua";
12
+ static ALLOWLIST_FILE_NAME: &str = "allowlist.txt";
13
+ static BLOCKLIST_FILE_NAME: &str = "blocklist.txt";
14
+
15
+ #[derive(Hash, PartialEq, Eq, Debug)]
16
+ pub enum FileType {
17
+ Config,
18
+ AllowList,
19
+ BlockList,
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.
77
+ ///
78
+ /// # Error
79
+ ///
80
+ /// Returns a `config file not found!!` error if the config file is not present under following
81
+ /// paths which are:
82
+ /// 1. `~/.config/websurfx/` if it not present here then it fallbacks to the next one (2)
83
+ /// 2. `/etc/xdg/websurfx/config.lua` if it is not present here then it fallbacks to the next
84
+ /// one (3).
85
+ /// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
86
+ /// here then it returns an error as mentioned above.
87
+
88
+ /// A function which returns an appropriate theme directory path checking if the theme
89
+ /// directory exists on that path.
90
+ ///
91
+ /// # Error
92
+ ///
93
+ /// Returns a `Theme (public) folder not found!!` error if the theme folder is not present under following
94
+ /// paths which are:
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
+
106
+ // if no of the configs above exist, return error
107
+ Err(Error::new(
108
+ std::io::ErrorKind::NotFound,
109
+ format!("{:?} file not found!!", file_type),
110
+ ))
111
+ }
src/handler/public_paths.rs DELETED
@@ -1,33 +0,0 @@
1
- //! This module provides the functionality to handle theme folder present on different paths and
2
- //! provide one appropriate path on which it is present and can be used.
3
-
4
- use std::io::Error;
5
- use std::path::Path;
6
-
7
- // ------- Constants --------
8
- static PUBLIC_DIRECTORY_NAME: &str = "public";
9
-
10
- /// A function which returns an appropriate theme directory path checking if the theme
11
- /// directory exists on that path.
12
- ///
13
- /// # Error
14
- ///
15
- /// Returns a `Theme (public) folder not found!!` error if the theme folder is not present under following
16
- /// paths which are:
17
- /// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2)
18
- /// 2. Under project folder ( or codebase in other words) if it is not present
19
- /// here then it returns an error as mentioned above.
20
- pub fn public_path() -> Result<String, Error> {
21
- if Path::new(format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() {
22
- return Ok(format!("/opt/websurfx/{}", PUBLIC_DIRECTORY_NAME));
23
- }
24
-
25
- if Path::new(format!("./{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() {
26
- return Ok(format!("./{}", PUBLIC_DIRECTORY_NAME));
27
- }
28
-
29
- Err(Error::new(
30
- std::io::ErrorKind::NotFound,
31
- "Themes (public) folder not found!!",
32
- ))
33
- }