sysfs_pwm/
error.rs

1// Copyright 2016, Paul Osborne <osbpau@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option.  This file may not be copied, modified, or distributed
7// except according to those terms.
8//
9// Portions of this implementation are based on work by Nat Pryce:
10// https://github.com/npryce/rusty-pi/blob/master/src/pi/gpio.rs
11
12use std::convert;
13use std::fmt;
14use std::io;
15
16#[derive(Debug)]
17pub enum Error {
18    /// Simple IO error
19    Io(io::Error),
20    /// Read unusual data from sysfs file.
21    Unexpected(String),
22}
23
24impl ::std::error::Error for Error {
25    fn description(&self) -> &str {
26        match *self {
27            Error::Io(ref e) => e.description(),
28            Error::Unexpected(_) => "something unexpected",
29        }
30    }
31
32    fn cause(&self) -> Option<&::std::error::Error> {
33        match *self {
34            Error::Io(ref e) => Some(e),
35            _ => None,
36        }
37    }
38}
39
40impl fmt::Display for Error {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        match *self {
43            Error::Io(ref e) => e.fmt(f),
44            Error::Unexpected(ref s) => write!(f, "Unexpected: {}", s),
45        }
46    }
47}
48
49
50impl convert::From<io::Error> for Error {
51    fn from(e: io::Error) -> Error {
52        Error::Io(e)
53    }
54}