mirror of
https://github.com/rust-lang/log.git
synced 2026-02-14 20:08:22 +00:00
Merge pull request #336 from tmccombs/kv-error
Add conversion to/from io::Error for kv::Error
This commit is contained in:
commit
bcbee9f64f
@ -1,29 +1,47 @@
|
||||
use std::fmt;
|
||||
#[cfg(feature = "std")]
|
||||
use std::io;
|
||||
|
||||
/// An error encountered while working with structured data.
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
msg: &'static str,
|
||||
inner: Inner
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Inner {
|
||||
#[cfg(feature = "std")]
|
||||
Io(io::Error),
|
||||
Msg(&'static str),
|
||||
Fmt,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Create an error from the given message.
|
||||
pub fn msg(msg: &'static str) -> Self {
|
||||
Error {
|
||||
msg: msg,
|
||||
inner: Inner::Msg(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.msg.fmt(f)
|
||||
use self::Inner::*;
|
||||
match &self.inner {
|
||||
#[cfg(feature = "std")]
|
||||
&Io(ref err) => err.fmt(f),
|
||||
&Msg(ref msg) => msg.fmt(f),
|
||||
&Fmt => fmt::Error.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fmt::Error> for Error {
|
||||
fn from(_: fmt::Error) -> Self {
|
||||
Error::msg("formatting failed")
|
||||
Error {
|
||||
inner: Inner::Fmt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,11 +54,29 @@ impl From<Error> for fmt::Error {
|
||||
#[cfg(feature = "std")]
|
||||
mod std_support {
|
||||
use super::*;
|
||||
use std::error;
|
||||
use std::{error, io};
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
"key values error"
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(err: io::Error) -> Self {
|
||||
Error {
|
||||
inner: Inner::Io(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for io::Error {
|
||||
fn from(err: Error) -> Self {
|
||||
if let Inner::Io(err) = err.inner {
|
||||
err
|
||||
} else {
|
||||
io::Error::new(io::ErrorKind::Other, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user