@@ -8,7 +8,7 @@ use dropshot::ConfigLogging;
8
8
use serde:: Deserialize ;
9
9
use serde:: Serialize ;
10
10
use std:: fmt;
11
- use std:: path:: Path ;
11
+ use std:: path:: { Path , PathBuf } ;
12
12
13
13
/**
14
14
* Configuration for an OXC server
@@ -24,34 +24,45 @@ pub struct ConfigController {
24
24
}
25
25
26
26
#[ derive( Debug ) ]
27
- pub enum LoadError {
27
+ pub struct LoadError {
28
+ path : PathBuf ,
29
+ kind : LoadErrorKind ,
30
+ }
31
+ #[ derive( Debug ) ]
32
+ pub enum LoadErrorKind {
28
33
Io ( std:: io:: Error ) ,
29
34
Parse ( toml:: de:: Error ) ,
30
35
}
31
36
32
- impl From < std:: io:: Error > for LoadError {
33
- fn from ( err : std:: io:: Error ) -> Self {
34
- LoadError :: Io ( err)
37
+ impl From < ( PathBuf , std:: io:: Error ) > for LoadError {
38
+ fn from ( ( path, err) : ( PathBuf , std:: io:: Error ) ) -> Self {
39
+ LoadError {
40
+ path,
41
+ kind : LoadErrorKind :: Io ( err) ,
42
+ }
35
43
}
36
44
}
37
- impl From < toml:: de:: Error > for LoadError {
38
- fn from ( err : toml:: de:: Error ) -> Self {
39
- LoadError :: Parse ( err)
45
+
46
+ impl From < ( PathBuf , toml:: de:: Error ) > for LoadError {
47
+ fn from ( ( path, err) : ( PathBuf , toml:: de:: Error ) ) -> Self {
48
+ LoadError {
49
+ path,
50
+ kind : LoadErrorKind :: Parse ( err) ,
51
+ }
40
52
}
41
53
}
42
54
43
55
impl std:: error:: Error for LoadError { }
44
56
45
57
impl fmt:: Display for LoadError {
46
58
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
47
- // default to debug for now
48
- write ! ( f, "{:?}" , self )
59
+ write ! ( f, "read {}: {:?}" , self . path. display( ) , self . kind)
49
60
}
50
61
}
51
62
52
63
impl std:: cmp:: PartialEq < std:: io:: Error > for LoadError {
53
64
fn eq ( & self , other : & std:: io:: Error ) -> bool {
54
- if let LoadError :: Io ( e) = self {
65
+ if let LoadErrorKind :: Io ( e) = & self . kind {
55
66
e. kind ( ) == other. kind ( )
56
67
} else {
57
68
false
@@ -67,16 +78,18 @@ impl ConfigController {
67
78
* The format is described in the README.
68
79
*/
69
80
pub fn from_file ( path : & Path ) -> Result < ConfigController , LoadError > {
70
- let file_contents = std:: fs:: read_to_string ( path) ?;
71
- let config_parsed: ConfigController = toml:: from_str ( & file_contents) ?;
81
+ let file_contents = std:: fs:: read_to_string ( path)
82
+ . map_err ( |e| ( path. to_path_buf ( ) , e) ) ?;
83
+ let config_parsed: ConfigController = toml:: from_str ( & file_contents)
84
+ . map_err ( |e| ( path. to_path_buf ( ) , e) ) ?;
72
85
Ok ( config_parsed)
73
86
}
74
87
}
75
88
76
89
#[ cfg( test) ]
77
90
mod test {
78
91
use super :: ConfigController ;
79
- use super :: LoadError ;
92
+ use super :: { LoadError , LoadErrorKind } ;
80
93
use dropshot:: ConfigDropshot ;
81
94
use dropshot:: ConfigLogging ;
82
95
use dropshot:: ConfigLoggingIfExists ;
@@ -141,7 +154,7 @@ mod test {
141
154
fn test_config_bad_toml ( ) {
142
155
let error =
143
156
read_config ( "bad_toml" , "foo =" ) . expect_err ( "expected failure" ) ;
144
- if let LoadError :: Parse ( error) = error {
157
+ if let LoadErrorKind :: Parse ( error) = & error. kind {
145
158
assert_eq ! ( error. line_col( ) , Some ( ( 0 , 5 ) ) ) ;
146
159
} else {
147
160
panic ! (
@@ -159,7 +172,7 @@ mod test {
159
172
#[ test]
160
173
fn test_config_empty ( ) {
161
174
let error = read_config ( "empty" , "" ) . expect_err ( "expected failure" ) ;
162
- if let LoadError :: Parse ( error) = error {
175
+ if let LoadErrorKind :: Parse ( error) = & error. kind {
163
176
assert_eq ! ( error. line_col( ) , None ) ;
164
177
} else {
165
178
panic ! (
0 commit comments