diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index cb0201e69..3370bc073 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -98,6 +98,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "dyld" version = "0.0.0" +[[package]] +name = "failure" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "failure_derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "fringe" version = "1.1.0" @@ -111,6 +126,8 @@ name = "io" version = "0.0.0" dependencies = [ "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -191,6 +208,11 @@ dependencies = [ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "runtime" version = "0.0.0" @@ -247,6 +269,38 @@ dependencies = [ name = "std_artiq" version = "0.0.0" +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synstructure" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unwind_backtrace" version = "0.0.0" @@ -280,6 +334,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins)" = "" "checksum crc 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5d02c0aac6bd68393ed69e00bbc2457f3e89075c6349db7189618dc4ddc1d7" "checksum cslice 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f8cb7306107e4b10e64994de6d3274bd08996a7c1322a27b86482392f96be0a" +"checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82" +"checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b" "checksum fringe 1.1.0 (git+https://github.com/m-labs/libfringe?rev=bd23494)" = "" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b" @@ -288,8 +344,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum log_buffer 1.2.0 (git+https://github.com/whitequark/rust-log_buffer?rev=rust-1.25)" = "" "checksum managed 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43e2737ecabe4ae36a68061398bf27d2bfd0763f4c3c837a398478459494c4b7" "checksum managed 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5a31885241e61ba264d780d2e6686e7e59561c947b4581470364eb3e10102d86" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" "checksum smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=181083f)" = "" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/artiq/firmware/libio/Cargo.toml b/artiq/firmware/libio/Cargo.toml index 9729aca32..6aacf0c47 100644 --- a/artiq/firmware/libio/Cargo.toml +++ b/artiq/firmware/libio/Cargo.toml @@ -8,6 +8,8 @@ name = "io" path = "lib.rs" [dependencies] +failure = { version = "0.1", default-features = false } +failure_derive = { version = "0.1", default-features = false } byteorder = { version = "1.0", default-features = false, optional = true } [features] diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index 884f57f27..48f07394d 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -2,6 +2,9 @@ #![feature(never_type)] #![cfg_attr(feature = "alloc", feature(alloc))] +extern crate failure; +#[macro_use] +extern crate failure_derive; #[cfg(feature = "alloc")] #[macro_use] extern crate alloc; @@ -9,7 +12,6 @@ extern crate alloc; extern crate byteorder; use core::result; -use core::fmt; mod cursor; #[cfg(feature = "byteorder")] @@ -19,24 +21,14 @@ pub use cursor::Cursor; pub type Result = result::Result>; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Fail, Debug, Clone, PartialEq)] pub enum Error { + #[fail(display = "unexpected end of stream")] UnexpectedEof, + #[fail(display = "unrecognized input")] Unrecognized, - Other(T) -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &Error::UnexpectedEof => - write!(f, "unexpected end of stream"), - &Error::Unrecognized => - write!(f, "unrecognized input"), - &Error::Other(ref err) => - write!(f, "{}", err) - } - } + #[fail(display = "{}", _0)] + Other(#[cause] T) } impl From for Error { diff --git a/artiq/firmware/libio/proto.rs b/artiq/firmware/libio/proto.rs index 0722f6926..22a8deb3e 100644 --- a/artiq/firmware/libio/proto.rs +++ b/artiq/firmware/libio/proto.rs @@ -1,30 +1,18 @@ #[cfg(feature = "alloc")] -use core::fmt; -#[cfg(feature = "alloc")] -use alloc::string; +use {core::str::Utf8Error, alloc::String}; use byteorder::{ByteOrder, NetworkEndian}; use ::{Read, Write, Error as IoError}; #[cfg(feature = "alloc")] -#[derive(Debug)] +#[derive(Fail, Debug, Clone, PartialEq)] pub enum ReadStringError { - Utf8Error(string::FromUtf8Error), + #[fail(display = "invalid UTF-8: {}", _0)] + Utf8Error(Utf8Error), + #[fail(display = "{}", _0)] Other(T) } -#[cfg(feature = "alloc")] -impl fmt::Display for ReadStringError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &ReadStringError::Utf8Error(ref err) => - write!(f, "invalid UTF-8 ({})", err), - &ReadStringError::Other(ref err) => - write!(f, "{}", err) - } - } -} - #[cfg(feature = "alloc")] impl From>> for IoError { @@ -86,14 +74,8 @@ pub trait ProtoRead { #[cfg(feature = "alloc")] #[inline] fn read_string(&mut self) -> Result<::alloc::String, ReadStringError> { - match self.read_bytes() { - Ok(bytes) => - match ::alloc::String::from_utf8(bytes) { - Ok(string) => Ok(string), - Err(err) => Err(ReadStringError::Utf8Error(err)) - }, - Err(err) => Err(ReadStringError::Other(err)) - } + let bytes = self.read_bytes().map_err(ReadStringError::Other)?; + String::from_utf8(bytes).map_err(|err| ReadStringError::Utf8Error(err.utf8_error())) } }