chorrent 0.5.4

Peer-to-peer and swarm file transfer over Iroh/QUIC with verified, chunked downloads
Documentation
use std::path::PathBuf;
use std::time::Duration;
use thiserror::Error;

/// Everything that can go wrong when using a [`Client`](crate::Client).
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
    #[error("invalid share code: {0}")]
    InvalidShareCode(String),

    #[error(transparent)]
    Chunker(#[from] ChunkerError),

    #[error(transparent)]
    Node(#[from] NodeError),

    #[error(transparent)]
    Protocol(#[from] ProtocolError),

    #[error("failed to read {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("failed to join the gossip network: {0}")]
    Gossip(String),

    #[error("no peers were discovered within {0:?}")]
    NoPeersFound(Duration),

    #[error("could not connect to any discovered peer")]
    NoPeersConnected,

    #[error("can't share {}: its name {reason}", path.display())]
    UnsupportedName { path: PathBuf, reason: String },

    #[error("the share's file list is invalid: {0}")]
    BadManifest(String),

    #[error("downloaded file {} does not match its expected hash", .0.display())]
    HashMismatch(PathBuf),

    #[error("this client is already sharing that content")]
    AlreadySharing,

    #[error("persistent state error: {0}")]
    Storage(String),

    #[error("the data dir {} is in use by another chorrent client", .0.display())]
    DataDirInUse(PathBuf),

    #[error("not available: {0}")]
    NotAvailable(String),

    #[error("the transfer was cancelled")]
    Cancelled,

    #[error("a background task failed: {0}")]
    Task(String),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Error)]
pub enum ChunkerError {
    #[error("failed to open {path}: {source}")]
    Open {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("failed to hash {path}: {source}")]
    Hash {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("failed to extract a piece from {path}: {source}")]
    Serve {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("failed to verify/save an incoming piece to {path}: {source}")]
    Receive {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
}

#[derive(Debug, Error)]
pub enum NodeError {
    #[error("failed to bind endpoint: {message}")]
    Bind { message: String },

    #[error("failed to connect to peer: {message}")]
    Connect { message: String },

    #[error("failed to accept incoming connection: {message}")]
    Accept { message: String },
}

#[derive(Debug, Error)]
pub enum ProtocolError {
    #[error("failed to send protocol message: {message}")]
    Send { message: String },

    #[error("failed to receive protocol message: {message}")]
    Receive { message: String },
}