@@ -28,6 +28,7 @@ pub struct GpioConfig {
28
28
pub enum Error {
29
29
IoError ( io:: Error ) ,
30
30
ParserErrors ( Vec < toml:: ParserError > ) ,
31
+ DecodingError ( toml:: DecodeError ) ,
31
32
NoConfigFound ,
32
33
}
33
34
@@ -53,6 +54,12 @@ impl From<Vec<toml::ParserError>> for Error {
53
54
}
54
55
}
55
56
57
+ impl From < toml:: DecodeError > for Error {
58
+ fn from ( e : toml:: DecodeError ) -> Self {
59
+ Error :: DecodingError ( e)
60
+ }
61
+ }
62
+
56
63
impl GpioConfig {
57
64
58
65
/// Load a GPIO Config from the system
@@ -82,7 +89,7 @@ impl GpioConfig {
82
89
83
90
// /etc/gpio.d/*.toml
84
91
for fragment in glob ( "/etc/gpio.d/*.toml" ) . unwrap ( ) . filter_map ( Result :: ok) {
85
- config_instances. push ( try!( Self :: from_file ( "/etc/gpio.toml" ) ) ) ;
92
+ config_instances. push ( try!( Self :: from_file ( fragment ) ) ) ;
86
93
}
87
94
88
95
// additional from command-line
@@ -104,7 +111,10 @@ impl GpioConfig {
104
111
let mut parser = toml:: Parser :: new ( config) ;
105
112
let root = try!( parser. parse ( ) . ok_or ( parser. errors ) ) ;
106
113
let mut d = toml:: Decoder :: new ( toml:: Value :: Table ( root) ) ;
107
- Ok ( Decodable :: decode ( & mut d) . unwrap ( ) )
114
+ match Decodable :: decode ( & mut d) {
115
+ Ok ( cfg) => Ok ( cfg) ,
116
+ Err ( e) => Err ( Error :: from ( e) ) ,
117
+ }
108
118
}
109
119
110
120
/// Load a GPIO config from the specified path
@@ -165,6 +175,27 @@ error_led = { num = 11, direction = "in", export = false}
165
175
assert_eq ! ( status_led. export, None ) ;
166
176
}
167
177
178
+ #[ test]
179
+ fn test_parser_empty_toml ( ) {
180
+ let configstr = "" ;
181
+ match GpioConfig :: from_str ( configstr) {
182
+ Err ( Error :: DecodingError ( _) ) => { } ,
183
+ _ => panic ! ( "Expected a decoding error" ) ,
184
+ }
185
+ }
186
+
187
+ #[ test]
188
+ fn test_parser_missing_pinnum ( ) {
189
+ let configstr = r#"
190
+ [pins.reset_button]
191
+ export = true
192
+ "# ;
193
+ match GpioConfig :: from_str ( configstr) {
194
+ Err ( Error :: DecodingError ( _) ) => { } ,
195
+ _ => panic ! ( "Expected a decoding error" ) ,
196
+ }
197
+ }
198
+
168
199
#[ test]
169
200
fn test_parse_error_bad_toml ( ) {
170
201
// basically, just garbage data
0 commit comments