Skip to content

Commit dc9e671

Browse files
committed
clippy: implement FromStr on GpioConfig
``` src/config.rs:203:5: 213:6 warning: defining a method called `from_str` on this type; consider implementing the `std::str::FromStr` trait or choosing a less ambiguous name, #[warn(should_implement_trait)] on by default src/config.rs:203 pub fn from_str(config: &str) -> Result<GpioConfig, Error> { src/config.rs:204 let mut parser = toml::Parser::new(config); src/config.rs:205 let root = try!(parser.parse().ok_or(parser.errors)); src/config.rs:206 match GpioConfig::decode(&mut toml::Decoder::new(toml::Value::Table(root))) { src/config.rs:207 Ok(cfg) => { src/config.rs:208 try!(cfg.validate().or_else(|e| Err(Error::from(e)))); ... src/config.rs:203:5: 213:6 help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#should_implement_trait ``` Signed-off-by: Paul Osborne <[email protected]>
1 parent ba21254 commit dc9e671

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/config.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fs::{self, File};
1414
use std::io;
1515
use std::io::prelude::*;
1616
use std::path::Path;
17+
use std::str::FromStr;
1718
use sysfs_gpio;
1819
use toml;
1920

@@ -130,6 +131,23 @@ impl PinConfig {
130131
}
131132
}
132133

134+
impl FromStr for GpioConfig {
135+
type Err = Error;
136+
137+
/// Load a GPIO configuration for the provided toml string
138+
fn from_str(config: &str) -> Result<Self, Error> {
139+
let mut parser = toml::Parser::new(config);
140+
let root = try!(parser.parse().ok_or(parser.errors));
141+
match Self::decode(&mut toml::Decoder::new(toml::Value::Table(root))) {
142+
Ok(cfg) => {
143+
try!(cfg.validate().or_else(|e| Err(Error::from(e))));
144+
Ok(cfg)
145+
}
146+
Err(e) => Err(Error::from(e)),
147+
}
148+
}
149+
}
150+
133151
impl GpioConfig {
134152
/// Validate invariants on the config that cannot easily be done earlier
135153
///
@@ -199,19 +217,6 @@ impl GpioConfig {
199217
}
200218
}
201219

202-
/// Load a GPIO configuration for the provided toml string
203-
pub fn from_str(config: &str) -> Result<GpioConfig, Error> {
204-
let mut parser = toml::Parser::new(config);
205-
let root = try!(parser.parse().ok_or(parser.errors));
206-
match GpioConfig::decode(&mut toml::Decoder::new(toml::Value::Table(root))) {
207-
Ok(cfg) => {
208-
try!(cfg.validate().or_else(|e| Err(Error::from(e))));
209-
Ok(cfg)
210-
}
211-
Err(e) => Err(Error::from(e)),
212-
}
213-
}
214-
215220
/// Load a GPIO config from the specified path
216221
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GpioConfig, Error> {
217222
let mut contents = String::new();
@@ -286,6 +291,7 @@ mod test {
286291
use std::iter::FromIterator;
287292
use std::collections::BTreeSet;
288293
use sysfs_gpio::Direction as D;
294+
use std::str::FromStr;
289295

290296
const BASIC_CFG: &'static str = r#"
291297
[[pins]]

0 commit comments

Comments
 (0)