-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: Fix no_std support (removes error-chain)
The error-chain library does not support no_std: rust-lang-deprecated/error-chain#138 This switches to a somewhat error-chain-like set of handwritten error boilerplate, and makes sure to test in CI that the Rust crate compiles without the "std" cargo feature.
- Loading branch information
1 parent
15c219f
commit 68677f9
Showing
11 changed files
with
128 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,110 @@ | ||
//! errors.rs: error types based on error-chain | ||
//! errors.rs: error types used by this crate | ||
#![allow(missing_docs)] | ||
use core::fmt::{self, Debug, Display}; | ||
use core::result; | ||
|
||
error_chain! { | ||
types { | ||
Error, ErrorKind, ResultExt, Result; | ||
} | ||
#[cfg(feature = "std")] | ||
use std::error; | ||
|
||
errors { | ||
OversizedMessage(len: usize, limit: usize) { | ||
description("message too long") | ||
display("{}-byte message exceeds limit of {} bytes", len, limit) | ||
} | ||
#[cfg(not(feature = "std"))] | ||
use collections::boxed::Box; | ||
|
||
#[cfg(not(feature = "std"))] | ||
use collections::string::String; | ||
|
||
/// Errors which can occur when serializing or deserializing zser messages | ||
pub struct Error { | ||
err: Box<ErrorImpl>, | ||
} | ||
|
||
/// `Result` alias used by this crate | ||
pub type Result<T> = result::Result<T, Error>; | ||
|
||
#[derive(Debug)] | ||
struct ErrorImpl { | ||
kind: ErrorKind, | ||
byte_offset: Option<usize>, | ||
} | ||
|
||
/// Error variants encountered in this crate | ||
#[derive(Debug)] | ||
pub enum ErrorKind { | ||
/// Message exceeds configured size limit | ||
OversizedMessage(usize), | ||
|
||
/// Message exceeds configured maximum for nested messages | ||
MaxDepthExceeded(usize), | ||
|
||
/// Expected more data in message | ||
TruncatedMessage(String), | ||
|
||
/// Message encoded with an unknown wiretype | ||
UnknownWiretype(u64), | ||
|
||
/// Unconsumed messages remaining in buffer | ||
UnconsumedMessages(usize), | ||
} | ||
|
||
MaxDepthExceeded(max: usize) { | ||
description("max nested message depth exceeded") | ||
display("max nested message depth of {} bytes exceeded", max) | ||
impl Display for ErrorKind { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
ErrorKind::OversizedMessage(limit) => { | ||
write!(f, "message exceeds limit of {} bytes", limit) | ||
} | ||
ErrorKind::MaxDepthExceeded(max) => { | ||
write!(f, "max nested message depth of {} exceeded", max) | ||
} | ||
ErrorKind::TruncatedMessage(ref msg) => write!(f, "message truncated: {}", msg), | ||
ErrorKind::UnknownWiretype(wiretype) => write!(f, "unknown wiretype: {}", wiretype), | ||
ErrorKind::UnconsumedMessages(count) => { | ||
write!(f, "unconsumed messages in buffer: {} messages", count) | ||
} | ||
} | ||
} | ||
} | ||
|
||
TruncatedMessage(msg: String) { | ||
description("message truncated") | ||
display("message truncated: {}", msg) | ||
#[cfg(feature = "std")] | ||
impl error::Error for Error { | ||
fn description(&self) -> &str { | ||
match self.err.kind { | ||
ErrorKind::OversizedMessage(_) => "message too long", | ||
ErrorKind::MaxDepthExceeded(_) => "maximum number of nested messages exceeded", | ||
ErrorKind::TruncatedMessage(_) => "message truncated", | ||
ErrorKind::UnknownWiretype(_) => "unknown wiretype", | ||
ErrorKind::UnconsumedMessages(_) => "unconsumed messages in buffer", | ||
} | ||
} | ||
} | ||
|
||
UnknownWiretype(wiretype: u64) { | ||
description("unknown wiretype") | ||
display("unknown wiretype: {}", wiretype) | ||
impl From<ErrorKind> for Error { | ||
fn from(ek: ErrorKind) -> Self { | ||
Error { | ||
err: Box::new(ErrorImpl { | ||
kind: ek, | ||
byte_offset: None, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
UnconsumedMessages(count: usize) { | ||
description("unconsumed messages remaining in buffer") | ||
display("unconsumed messages in buffer: {} messages", count) | ||
impl Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
Display::fmt(&*self.err, f) | ||
} | ||
} | ||
|
||
impl Display for ErrorImpl { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.byte_offset { | ||
Some(offset) => write!(f, "{} at byte {}", self.kind, offset), | ||
None => Display::fmt(&self.kind, f), | ||
} | ||
|
||
} | ||
} | ||
|
||
impl Debug for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
Debug::fmt(&*self.err, f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.