diff --git a/Cargo.toml b/Cargo.toml index 95dc994..9cce55c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ev3dev-lang-rust" -version = "0.12.0" +version = "0.12.1" edition = "2021" authors = ["Lars Westermann "] @@ -30,6 +30,7 @@ members = [ [profile.release] lto = true strip = "debuginfo" +opt-level = "z" [[example]] name = "screen" diff --git a/src/utils.rs b/src/utils.rs index 3a1080a..ddc7f02 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,7 @@ //! Utility things. +use std::{error::Error, fmt}; + /// Helper `Result` type for easy access. pub type Ev3Result = Result; @@ -26,6 +28,23 @@ pub enum Ev3Error { ports: Vec, }, } + +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 for Ev3Error { fn from(err: std::io::Error) -> Self { Ev3Error::InternalError {