bitcoin/network/
mod.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//   Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Network Support
16//!
17//! This module defines support for (de)serialization and network transport
18//! of Bitcoin data and network messages.
19//!
20
21use std::fmt;
22use std::io;
23use std::error;
24
25pub mod constants;
26
27pub mod address;
28pub use self::address::Address;
29pub mod message;
30pub mod message_blockdata;
31pub mod message_network;
32pub mod message_filter;
33pub mod stream_reader;
34
35/// Network error
36#[derive(Debug)]
37pub enum Error {
38    /// And I/O error
39    Io(io::Error),
40    /// Socket mutex was poisoned
41    SocketMutexPoisoned,
42    /// Not connected to peer
43    SocketNotConnectedToPeer,
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48        match *self {
49            Error::Io(ref e) => fmt::Display::fmt(e, f),
50            Error::SocketMutexPoisoned => f.write_str("socket mutex was poisoned"),
51            Error::SocketNotConnectedToPeer => f.write_str("not connected to peer"),
52        }
53    }
54}
55
56#[doc(hidden)]
57impl From<io::Error> for Error {
58    fn from(err: io::Error) -> Self {
59        Error::Io(err)
60    }
61}
62
63impl error::Error for Error {
64
65    fn cause(&self) -> Option<&dyn error::Error> {
66        match *self {
67            Error::Io(ref e) => Some(e),
68            Error::SocketMutexPoisoned | Error::SocketNotConnectedToPeer => None,
69        }
70    }
71}