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 mod message;
29pub mod message_blockdata;
30pub mod message_network;
31
32/// Network error
33#[derive(Debug)]
34pub enum Error {
35    /// And I/O error
36    Io(io::Error),
37    /// Socket mutex was poisoned
38    SocketMutexPoisoned,
39    /// Not connected to peer
40    SocketNotConnectedToPeer,
41}
42
43impl fmt::Display for Error {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        match *self {
46            Error::Io(ref e) => fmt::Display::fmt(e, f),
47            Error::SocketMutexPoisoned | Error::SocketNotConnectedToPeer => f.write_str(error::Error::description(self)),
48        }
49    }
50}
51
52impl error::Error for Error {
53    fn cause(&self) -> Option<&error::Error> {
54        match *self {
55            Error::Io(ref e) => Some(e),
56            Error::SocketMutexPoisoned | Error::SocketNotConnectedToPeer => None,
57        }
58    }
59
60    fn description(&self) -> &str {
61        match *self {
62            Error::Io(ref e) => e.description(),
63            Error::SocketMutexPoisoned => "socket mutex was poisoned",
64            Error::SocketNotConnectedToPeer => "not connected to peer",
65        }
66    }
67}