1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Copyright 2018-2019 Tamas Blummer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//!
//! # Error type
//!
//!

use std::{fmt, io, sync};

/// Errors returned by this library
pub enum Error {
    /// pref is invalid (> 2^48)
    InvalidOffset,
    /// corrupted data
    Corrupted(String),
    /// key too long
    KeyTooLong,
    /// wrapped IO error
    IO(io::Error),
    /// Lock poisoned
    Poisoned(String),
    /// Queue error
    Queue(String),
    /// Bitcoin encoding error.
    #[cfg(feature = "bitcoin_support")]
    BitcoinDecode(bitcoin::consensus::encode::Error),
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }

    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            Error::InvalidOffset => None,
            Error::KeyTooLong => None,
            Error::Corrupted(_) => None,
            Error::IO(ref e) => Some(e),
            Error::Poisoned(_) => None,
            Error::Queue(_) => None,
            #[cfg(feature = "bitcoin_support")]
            Error::BitcoinDecode(ref e) => Some(e),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            Error::InvalidOffset => write!(f, "invalid pref"),
            Error::KeyTooLong => write!(f, "key too long"),
            Error::Corrupted(ref s) => write!(f, "corrupted data: {}", s),
            Error::IO(e) => e.fmt(f),
            Error::Poisoned(ref s) => write!(f, "lock poisoned: {}", s),
            Error::Queue(ref s) => write!(f, "queue error {}", s),
            #[cfg(feature = "bitcoin_support")]
            Error::BitcoinDecode(e) => write!(f, "bitcoin parsing error: {}", e),
        }
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (self as &dyn fmt::Display).fmt(f)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IO(err)
    }
}

impl From<Error> for io::Error {
    fn from(_: Error) -> io::Error {
        io::Error::from(io::ErrorKind::UnexpectedEof)
    }
}

impl<T> From<sync::PoisonError<T>> for Error {
    fn from(err: sync::PoisonError<T>) -> Error {
        Error::Poisoned(err.to_string())
    }
}

impl<T> From<sync::mpsc::SendError<T>> for Error {
    fn from(err: sync::mpsc::SendError<T>) -> Error {
        Error::Queue(err.to_string())
    }
}

#[cfg(feature = "bitcoin_support")]
impl From<bitcoin::consensus::encode::Error> for Error {
    fn from(e: bitcoin::consensus::encode::Error) -> Error {
        Error::BitcoinDecode(e)
    }
}