Skip to content

Commit

Permalink
Consolidated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fpagliughi committed Mar 2, 2022
1 parent c32ceba commit b563353
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
57 changes: 57 additions & 0 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
convert::TryFrom,
error,
fmt,
io,
};


Expand Down Expand Up @@ -459,3 +460,59 @@ impl ControllerSpecificErrorInformation for CanFrame {
}
}

#[derive(Debug)]
/// Errors opening socket
pub enum CanSocketOpenError {
/// Device could not be found
LookupError(nix::Error),

/// System error while trying to look up device name
IOError(io::Error),
}

impl fmt::Display for CanSocketOpenError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CanSocketOpenError::LookupError(ref e) => write!(f, "CAN Device not found: {}", e),
CanSocketOpenError::IOError(ref e) => write!(f, "IO: {}", e),
}
}
}

impl error::Error for CanSocketOpenError {}

#[derive(Debug, Copy, Clone)]
/// Error that occurs when creating CAN packets
pub enum ConstructionError {
/// CAN ID was outside the range of valid IDs
IDTooLarge,
/// More than 8 Bytes of payload data were passed in
TooMuchData,
}

impl fmt::Display for ConstructionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ConstructionError::IDTooLarge => write!(f, "CAN ID too large"),
ConstructionError::TooMuchData => {
write!(f, "Payload is larger than CAN maximum of 8 bytes")
}
}
}
}

impl error::Error for ConstructionError {}

impl From<nix::Error> for CanSocketOpenError {
fn from(e: nix::Error) -> CanSocketOpenError {
CanSocketOpenError::LookupError(e)
}
}

impl From<io::Error> for CanSocketOpenError {
fn from(e: io::Error) -> CanSocketOpenError {
CanSocketOpenError::IOError(e)
}
}


58 changes: 1 addition & 57 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))]

mod err;
pub use err::{CanError, CanErrorDecodingFailure};
pub use err::{CanError, CanErrorDecodingFailure, CanSocketOpenError, ConstructionError};

pub mod dump;

Expand All @@ -84,7 +84,6 @@ use itertools::Itertools;
use nix::net::if_::if_nametoindex;
use std::{
os::raw::{c_int, c_short, c_void, c_uint, c_ulong},
error,
fmt,
io,
time,
Expand Down Expand Up @@ -198,61 +197,6 @@ struct CanAddr {
tx_id: u32,
}

#[derive(Debug)]
/// Errors opening socket
pub enum CanSocketOpenError {
/// Device could not be found
LookupError(nix::Error),

/// System error while trying to look up device name
IOError(io::Error),
}

impl fmt::Display for CanSocketOpenError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CanSocketOpenError::LookupError(ref e) => write!(f, "CAN Device not found: {}", e),
CanSocketOpenError::IOError(ref e) => write!(f, "IO: {}", e),
}
}
}

impl error::Error for CanSocketOpenError {}

#[derive(Debug, Copy, Clone)]
/// Error that occurs when creating CAN packets
pub enum ConstructionError {
/// CAN ID was outside the range of valid IDs
IDTooLarge,
/// More than 8 Bytes of payload data were passed in
TooMuchData,
}

impl fmt::Display for ConstructionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ConstructionError::IDTooLarge => write!(f, "CAN ID too large"),
ConstructionError::TooMuchData => {
write!(f, "Payload is larger than CAN maximum of 8 bytes")
}
}
}
}

impl error::Error for ConstructionError {}

impl From<nix::Error> for CanSocketOpenError {
fn from(e: nix::Error) -> CanSocketOpenError {
CanSocketOpenError::LookupError(e)
}
}

impl From<io::Error> for CanSocketOpenError {
fn from(e: io::Error) -> CanSocketOpenError {
CanSocketOpenError::IOError(e)
}
}

/// A socket for a CAN device.
///
/// Will be closed upon deallocation. To close manually, use std::drop::Drop.
Expand Down
5 changes: 2 additions & 3 deletions src/nl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ use neli::{
use nix::{self, unistd, net::if_::if_nametoindex};
use std::{
os::raw::{c_int, c_uint},
result,
fmt::Debug,
};

/// A result for Netlink errors.
type NlResult<T> = result::Result<T, NlError>;
type NlResult<T> = Result<T, NlError>;

/// SocketCAN interface
///
Expand All @@ -45,7 +44,7 @@ impl CanInterface {
/// Open CAN interface by name
///
/// Similar to `open_if`, but looks up the device by name instead
pub fn open(ifname: &str) -> result::Result<Self, nix::Error> {
pub fn open(ifname: &str) -> Result<Self, nix::Error> {
let if_index = if_nametoindex(ifname)?;
Ok(Self::open_iface(if_index))
}
Expand Down

0 comments on commit b563353

Please sign in to comment.