Skip to main content

mtorrent_core/data/
mod.rs

1use std::io;
2use thiserror::Error;
3use tokio::sync::{mpsc, oneshot};
4
5mod block_accountant;
6mod piece_info;
7mod piece_requests;
8mod piece_tracker;
9mod storage;
10
11pub use block_accountant::BlockAccountant;
12pub use piece_info::PieceInfo;
13pub use piece_requests::PendingRequests;
14pub use piece_tracker::PieceTracker;
15pub use storage::{StorageClient, StorageServer, new_async_storage};
16
17#[cfg(feature = "mocks")]
18pub use storage::new_mock_storage;
19
20/// Common error type.
21#[derive(Debug, Error)]
22pub enum Error {
23    #[error(transparent)]
24    IOError(#[from] io::Error),
25    #[error("invalid Location")]
26    InvalidLocation,
27    #[error("channel closed")]
28    ChannelClosed,
29}
30
31impl From<Error> for io::Error {
32    fn from(e: Error) -> Self {
33        match e {
34            Error::IOError(e) => e,
35            Error::InvalidLocation => io::Error::new(io::ErrorKind::NotFound, "invalid location"),
36            Error::ChannelClosed => io::Error::new(io::ErrorKind::BrokenPipe, "channel closed"),
37        }
38    }
39}
40
41impl<T> From<mpsc::error::SendError<T>> for Error {
42    fn from(_: mpsc::error::SendError<T>) -> Self {
43        Self::ChannelClosed
44    }
45}
46
47impl From<oneshot::error::RecvError> for Error {
48    fn from(_: oneshot::error::RecvError) -> Self {
49        Self::ChannelClosed
50    }
51}