Skip to content

Commit cbae7c5

Browse files
committed
format: replace try! with ? operator
Signed-off-by: Paul Osborne <osbpau@gmail.com>
1 parent 3b325b0 commit cbae7c5

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

src/config.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,24 @@ impl GpioConfig {
171171

172172
// check /etc/gpio.toml
173173
if fs::metadata("/etc/gpio.toml").is_ok() {
174-
config_instances.push(try!(Self::from_file("/etc/gpio.toml")));
174+
config_instances.push(Self::from_file("/etc/gpio.toml")?);
175175
}
176176
// /etc/gpio.d/*.toml
177177
for fragment in glob("/etc/gpio.d/*.toml").unwrap().filter_map(Result::ok) {
178-
config_instances.push(try!(Self::from_file(fragment)));
178+
config_instances.push(Self::from_file(fragment)?);
179179
}
180180

181181
// additional from command-line
182182
for fragment in configs {
183-
config_instances.push(try!(Self::from_file(fragment)));
183+
config_instances.push(Self::from_file(fragment)?);
184184
}
185185

186186
if config_instances.is_empty() {
187187
Err(Error::NoConfigFound)
188188
} else {
189189
let mut cfg = config_instances.remove(0);
190190
for higher_priority_cfg in config_instances {
191-
try!(cfg.update(higher_priority_cfg));
191+
cfg.update(higher_priority_cfg)?;
192192
}
193193
Ok(cfg)
194194
}
@@ -197,10 +197,11 @@ impl GpioConfig {
197197
/// Load a GPIO config from the specified path
198198
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GpioConfig, Error> {
199199
let mut contents = String::new();
200-
let mut f = try!(File::open(path));
201-
try!(f.read_to_string(&mut contents));
202-
let config = try!(GpioConfig::from_str(&contents[..]));
203-
try!(config.validate());
200+
let mut f = File::open(path)?;
201+
f.read_to_string(&mut contents)?;
202+
let config = GpioConfig::from_str(&contents[..])?;
203+
config.validate()?;
204+
204205
Ok(config)
205206
}
206207

src/export.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ pub fn unexport(pin_config: &PinConfig, symlink_root: Option<&str>) -> Result<()
3939
for name in &pin_config.names {
4040
let mut dst = path::PathBuf::from(symroot);
4141
dst.push(name);
42-
try!(match fs::remove_file(dst) {
43-
Ok(_) => Ok(()),
44-
Err(ref e) if e.kind() == ErrorKind::NotFound => Ok(()),
45-
Err(e) => Err(e),
46-
});
42+
match fs::remove_file(dst) {
43+
Ok(_) => (),
44+
Err(ref e) if e.kind() == ErrorKind::NotFound => (),
45+
Err(e) => return Err(e.into()),
46+
};
4747
}
4848
}
4949

@@ -123,35 +123,27 @@ pub fn export(pin_config: &PinConfig, symlink_root: Option<&str>) -> Result<()>
123123
// if there is a symlink root provided, create symlink
124124
if let Some(symroot) = symlink_root {
125125
// create root directory if not exists
126-
try!(fs::create_dir_all(symroot));
126+
fs::create_dir_all(symroot)?;
127127

128128
// set the pin direction
129-
try!(
130-
pin_config
131-
.get_pin()
132-
.set_direction(pin_config.direction.clone())
133-
);
129+
pin_config
130+
.get_pin()
131+
.set_direction(pin_config.direction.clone())?;
134132

135-
// set active low direction
136-
try!(
137-
pin_config
138-
.get_pin()
139-
.set_active_low(pin_config.active_low.clone())
140-
);
133+
// set active low directio
134+
pin_config
135+
.get_pin()
136+
.set_active_low(pin_config.active_low.clone())?;
141137

142138
// create symlink for each name
143139
for name in &pin_config.names {
144140
let mut dst = path::PathBuf::from(symroot);
145141
dst.push(name);
146-
try!(
147-
match unix_fs::symlink(format!("/sys/class/gpio/gpio{}", pin_config.num), dst) {
148-
Ok(_) => Ok(()),
149-
Err(e) => match e.kind() {
150-
ErrorKind::AlreadyExists => Ok(()),
151-
_ => Err(e),
152-
},
153-
}
154-
);
142+
match unix_fs::symlink(format!("/sys/class/gpio/gpio{}", pin_config.num), dst) {
143+
Err(ref e) if e.kind() == ErrorKind::AlreadyExists => (),
144+
Err(e) => return Err(e.into()),
145+
_ => (),
146+
};
155147
}
156148
}
157149

0 commit comments

Comments
 (0)