Skip to content

Commit

Permalink
Impl Error trait for Ev3Error
Browse files Browse the repository at this point in the history
  • Loading branch information
pixix4 committed Apr 25, 2022
1 parent fdc1c78 commit 425dd65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ev3dev-lang-rust"
version = "0.12.0"
version = "0.12.1"
edition = "2021"
authors = ["Lars Westermann <rust@lars-westermann.de>"]

Expand Down Expand Up @@ -30,6 +30,7 @@ members = [
[profile.release]
lto = true
strip = "debuginfo"
opt-level = "z"

[[example]]
name = "screen"
Expand Down
19 changes: 19 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utility things.
use std::{error::Error, fmt};

/// Helper `Result` type for easy access.
pub type Ev3Result<T> = Result<T, Ev3Error>;

Expand All @@ -26,6 +28,23 @@ pub enum Ev3Error {
ports: Vec<String>,
},
}

impl fmt::Display for Ev3Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Ev3Error::InternalError { msg } => write!(f, "InternalError: {}!", msg),
Ev3Error::NotConnected { device, port } => {
write!(f, "'{}' not connected at port {:?}!", device, port)
}
Ev3Error::MultipleMatches { device, ports } => {
write!(f, "Multiple '{}' connected at ports {:?}!", device, ports)
}
}
}
}

impl Error for Ev3Error {}

impl From<std::io::Error> for Ev3Error {
fn from(err: std::io::Error) -> Self {
Ev3Error::InternalError {
Expand Down

0 comments on commit 425dd65

Please sign in to comment.