use core::fmt;
#[cfg(feature = "std")]
use std::io;
#[cfg(not(feature = "std"))]
use core2::io;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
UnknownCodec,
InputTooShort,
ParsingError,
InvalidCidVersion,
InvalidCidV0Codec,
InvalidCidV0Multihash,
InvalidCidV0Base,
VarIntDecodeError,
Io(io::Error),
InvalidExplicitCidV0,
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
let error = match self {
UnknownCodec => "Unknown codec",
InputTooShort => "Input too short",
ParsingError => "Failed to parse multihash",
InvalidCidVersion => "Unrecognized CID version",
InvalidCidV0Codec => "CIDv0 requires a DagPB codec",
InvalidCidV0Multihash => "CIDv0 requires a Sha-256 multihash",
InvalidCidV0Base => "CIDv0 requires a Base58 base",
VarIntDecodeError => "Failed to decode unsigned varint format",
Io(err) => return write!(f, "{}", err),
InvalidExplicitCidV0 => "CIDv0 cannot be specified in CIDv1 format",
};
f.write_str(error)
}
}
#[cfg(feature = "alloc")]
impl From<multibase::Error> for Error {
fn from(_: multibase::Error) -> Error {
Error::ParsingError
}
}
impl From<multihash::Error> for Error {
fn from(_: multihash::Error) -> Error {
Error::ParsingError
}
}
impl From<unsigned_varint::decode::Error> for Error {
fn from(_: unsigned_varint::decode::Error) -> Self {
Error::VarIntDecodeError
}
}
#[cfg(feature = "std")]
impl From<unsigned_varint::io::ReadError> for Error {
fn from(err: unsigned_varint::io::ReadError) -> Self {
use unsigned_varint::io::ReadError::*;
match err {
Io(err) => Self::Io(err),
_ => Self::VarIntDecodeError,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}