Skip to content

Commit c04b48a

Browse files
authored
Merge pull request #43 from rust-embedded/update-deps-and-remove-regexp
Update deps and remove regexp
2 parents 3559b4b + 4f8e676 commit c04b48a

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22
sudo: false
33
rust:
4-
- 1.18.0
4+
- 1.20.0
55
- stable
66
- beta
77
- nightly

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
### Changed
88

99
- Add support for `active_low` configuration.
10+
- Remove dependency on regexp
11+
- Update nix to 0.10.0
1012

1113
## [0.5.1] - 2016-06-17
1214

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ tokio = ["futures", "tokio-core", "mio-evented"]
2121

2222
[dependencies]
2323
futures = { version = "0.1", optional = true }
24-
nix = "0.6.0"
25-
regex = "0.1.0"
24+
nix = "0.10.0"
2625
mio = { version = "0.6", optional = true }
2726
tokio-core = { version = "0.1", optional = true }

src/error.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::convert;
22
use std::fmt;
33
use std::io;
4+
use nix;
45

56
#[derive(Debug)]
67
pub enum Error {
@@ -50,8 +51,11 @@ impl convert::From<io::Error> for Error {
5051
}
5152
}
5253

53-
impl convert::From<::nix::Error> for Error {
54-
fn from(e: ::nix::Error) -> Error {
55-
Error::Io(io::Error::from_raw_os_error(e.errno() as i32))
54+
impl convert::From<nix::Error> for Error {
55+
fn from(e: nix::Error) -> Error {
56+
match e {
57+
nix::Error::Sys(errno) => Error::Io(errno.into()),
58+
other => Error::Unexpected(format!("{:?}", other)), // should just be dealing with errno case
59+
}
5660
}
5761
}

src/lib.rs

Lines changed: 31 additions & 28 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,
@@ -505,13 +515,9 @@ impl PinPoller {
505515
let devfile: File = File::open(&format!("/sys/class/gpio/gpio{}/value", pin_num))?;
506516
let devfile_fd = devfile.as_raw_fd();
507517
let epoll_fd = epoll_create()?;
508-
let events = EPOLLPRI | EPOLLET;
509-
let info = EpollEvent {
510-
events: events,
511-
data: 0u64,
512-
};
518+
let mut event = EpollEvent::new(EpollFlags::EPOLLPRI | EpollFlags::EPOLLET, 0u64);
513519

514-
match epoll_ctl(epoll_fd, EpollOp::EpollCtlAdd, devfile_fd, &info) {
520+
match epoll_ctl(epoll_fd, EpollOp::EpollCtlAdd, devfile_fd, &mut event) {
515521
Ok(_) => {
516522
Ok(PinPoller {
517523
pin_num: pin_num,
@@ -551,10 +557,7 @@ impl PinPoller {
551557
#[cfg(any(target_os = "linux", target_os = "android"))]
552558
pub fn poll(&mut self, timeout_ms: isize) -> Result<Option<u8>> {
553559
flush_input_from_file(&mut self.devfile, 255)?;
554-
let dummy_event = EpollEvent {
555-
events: EPOLLPRI | EPOLLET,
556-
data: 0u64,
557-
};
560+
let dummy_event = EpollEvent::new(EpollFlags::EPOLLPRI | EpollFlags::EPOLLET, 0u64);
558561
let mut events: [EpollEvent; 1] = [dummy_event];
559562
let cnt = epoll_wait(self.epoll_fd, &mut events, timeout_ms)?;
560563
Ok(match cnt {

0 commit comments

Comments
 (0)