Skip to content

Commit d44fa0b

Browse files
rumatoestposborne
authored andcommitted
Remove regexp dependency by replacing path match with custom function.
(cherry picked from commit 1d895e7) Signed-off-by: Paul Osborne <[email protected]>
1 parent 3559b4b commit d44fa0b

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,5 @@ tokio = ["futures", "tokio-core", "mio-evented"]
2222
[dependencies]
2323
futures = { version = "0.1", optional = true }
2424
nix = "0.6.0"
25-
regex = "0.1.0"
2625
mio = { version = "0.6", optional = true }
2726
tokio-core = { version = "0.1", optional = true }

src/lib.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ extern crate futures;
4949
#[cfg(feature = "mio-evented")]
5050
extern crate mio;
5151
extern crate nix;
52-
extern crate regex;
5352
#[cfg(feature = "tokio")]
5453
extern crate tokio_core;
5554

@@ -78,6 +77,8 @@ use tokio_core::reactor::{Handle, PollEvented};
7877
mod error;
7978
pub use error::Error;
8079

80+
const GPIO_PATH_PREFIX: &'static str = "/sys/class/gpio/gpio";
81+
8182
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8283
pub struct Pin {
8384
pin_num: u64,
@@ -176,26 +177,23 @@ impl Pin {
176177
a directory"
177178
.to_owned()));
178179
}
179-
180-
let re = regex::Regex::new(r"^/sys/.*?/gpio/gpio(\d+)$").unwrap();
181-
let caps = match re.captures(pb.to_str().unwrap_or("")) {
182-
Some(cap) => cap,
183-
None => return Err(Error::InvalidPath(format!("{:?}", pb))),
184-
};
185-
186-
let num: u64 = match caps.at(1) {
187-
Some(num) => {
188-
match num.parse() {
189-
Ok(unum) => unum,
190-
Err(_) => return Err(Error::InvalidPath(format!("{:?}", pb))),
191-
}
192-
}
193-
None => return Err(Error::InvalidPath(format!("{:?}", pb))),
194-
};
195-
180+
let num = Pin::extract_pin_from_path(&pb.to_str().unwrap_or(""))?;
196181
Ok(Pin::new(num))
197182
}
198183

184+
/// Extract pin number from paths like /sys/class/gpio/gpioXXX
185+
fn extract_pin_from_path(path: &str) -> Result<u64> {
186+
if path.starts_with(GPIO_PATH_PREFIX) {
187+
path.split_at(GPIO_PATH_PREFIX.len()).1.parse::<u64>().or(
188+
Err(
189+
Error::InvalidPath(format!("{:?}", path)),
190+
),
191+
)
192+
} else {
193+
Err(Error::InvalidPath(format!("{:?}", path)))
194+
}
195+
}
196+
199197
/// Get the pin number
200198
pub fn get_pin_num(&self) -> u64 {
201199
self.pin_num
@@ -483,6 +481,18 @@ impl Pin {
483481
}
484482
}
485483

484+
#[test]
485+
fn extract_pin_fom_path_test() {
486+
let tok = Pin::extract_pin_from_path(&"/sys/class/gpio/gpio951");
487+
assert_eq!(951, tok.unwrap());
488+
let err1 = Pin::extract_pin_from_path(&"/sys/is/error/gpio/gpio111");
489+
assert_eq!(true, err1.is_err());
490+
let err2 = Pin::extract_pin_from_path(&"/sys/CLASS/gpio/gpio");
491+
assert_eq!(true, err2.is_err());
492+
let err3 = Pin::extract_pin_from_path(&"/sys/class/gpio/gpioSDS");
493+
assert_eq!(true, err3.is_err());
494+
}
495+
486496
#[derive(Debug)]
487497
pub struct PinPoller {
488498
pin_num: u64,

0 commit comments

Comments
 (0)