@@ -14,11 +14,15 @@ use crate::errors::*;
14
14
/// - Then try `/usr/share/`
15
15
///
16
16
/// Automatically append an extension if not presented.
17
- pub fn find_file ( file : & str , subdir : Option < & str > , extension : Option < & str > ) -> Option < PathBuf > {
17
+ pub fn find_file (
18
+ file : & str ,
19
+ subdir : Option < & str > ,
20
+ extension : Option < & str > ,
21
+ ) -> Result < Option < PathBuf > > {
18
22
let file = Path :: new ( file) ;
19
23
20
- if file. is_absolute ( ) && file. exists ( ) {
21
- return Some ( file. to_path_buf ( ) ) ;
24
+ if file. is_absolute ( ) && file. try_exists ( ) . error ( "Unable to stat file" ) ? {
25
+ return Ok ( Some ( file. to_path_buf ( ) ) ) ;
22
26
}
23
27
24
28
// Try XDG_CONFIG_HOME (e.g. `~/.config`)
@@ -28,8 +32,8 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
28
32
xdg_config. push ( subdir) ;
29
33
}
30
34
xdg_config. push ( file) ;
31
- if let Some ( file) = exists_with_opt_extension ( & xdg_config, extension) {
32
- return Some ( file) ;
35
+ if let Some ( file) = exists_with_opt_extension ( & xdg_config, extension) ? {
36
+ return Ok ( Some ( file) ) ;
33
37
}
34
38
}
35
39
@@ -40,8 +44,8 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
40
44
xdg_data. push ( subdir) ;
41
45
}
42
46
xdg_data. push ( file) ;
43
- if let Some ( file) = exists_with_opt_extension ( & xdg_data, extension) {
44
- return Some ( file) ;
47
+ if let Some ( file) = exists_with_opt_extension ( & xdg_data, extension) ? {
48
+ return Ok ( Some ( file) ) ;
45
49
}
46
50
}
47
51
@@ -51,26 +55,26 @@ pub fn find_file(file: &str, subdir: Option<&str>, extension: Option<&str>) -> O
51
55
usr_share_path. push ( subdir) ;
52
56
}
53
57
usr_share_path. push ( file) ;
54
- if let Some ( file) = exists_with_opt_extension ( & usr_share_path, extension) {
55
- return Some ( file) ;
58
+ if let Some ( file) = exists_with_opt_extension ( & usr_share_path, extension) ? {
59
+ return Ok ( Some ( file) ) ;
56
60
}
57
61
58
- None
62
+ Ok ( None )
59
63
}
60
64
61
- fn exists_with_opt_extension ( file : & Path , extension : Option < & str > ) -> Option < PathBuf > {
62
- if file. exists ( ) {
63
- return Some ( file. into ( ) ) ;
65
+ fn exists_with_opt_extension ( file : & Path , extension : Option < & str > ) -> Result < Option < PathBuf > > {
66
+ if file. try_exists ( ) . error ( "Unable to stat file" ) ? {
67
+ return Ok ( Some ( file. into ( ) ) ) ;
64
68
}
65
69
// If file has no extension, test with given extension
66
70
if let ( None , Some ( extension) ) = ( file. extension ( ) , extension) {
67
71
let file = file. with_extension ( extension) ;
68
72
// Check again with extension added
69
- if file. exists ( ) {
70
- return Some ( file) ;
73
+ if file. try_exists ( ) . error ( "Unable to stat file" ) ? {
74
+ return Ok ( Some ( file) ) ;
71
75
}
72
76
}
73
- None
77
+ Ok ( None )
74
78
}
75
79
76
80
pub async fn new_dbus_connection ( ) -> Result < zbus:: Connection > {
95
99
let contents = std:: fs:: read_to_string ( path)
96
100
. or_error ( || format ! ( "Failed to read file: {}" , path. display( ) ) ) ?;
97
101
102
+ deserialize_toml_file_string ( contents, path)
103
+ }
104
+
105
+ pub async fn async_deserialize_toml_file < T , P > ( path : P ) -> Result < T >
106
+ where
107
+ T : DeserializeOwned ,
108
+ P : AsRef < Path > ,
109
+ {
110
+ let path = path. as_ref ( ) ;
111
+
112
+ let contents = read_file ( path)
113
+ . await
114
+ . or_error ( || format ! ( "Failed to read file: {}" , path. display( ) ) ) ?;
115
+
116
+ deserialize_toml_file_string ( contents, path)
117
+ }
118
+
119
+ fn deserialize_toml_file_string < T > ( contents : String , path : & Path ) -> Result < T >
120
+ where
121
+ T : DeserializeOwned ,
122
+ {
98
123
toml:: from_str ( & contents) . map_err ( |err| {
99
124
let location_msg = err
100
125
. span ( )
0 commit comments