Skip to main content

fx_torrent/peer/extension/
error.rs

1use crate::bencode;
2use std::io;
3use thiserror::Error;
4
5/// The extension specific result type
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The errors which may occur within extensions
9#[derive(Debug, Error)]
10pub enum Error {
11    #[error("failed to parse extension payload, {0}")]
12    Parsing(String),
13    #[error("failed to execute extension operation, {0}")]
14    Operation(String),
15    #[error("an io error occurred, {0}")]
16    Io(io::Error),
17    #[error("the payload or operation is not supported")]
18    Unsupported,
19}
20
21impl PartialEq for Error {
22    fn eq(&self, other: &Self) -> bool {
23        match (self, other) {
24            (Self::Parsing(_), Self::Parsing(_)) => true,
25            (Self::Operation(_), Self::Operation(_)) => true,
26            (Self::Io(a), Self::Io(b)) => a.kind() == b.kind(),
27            (Self::Unsupported, Self::Unsupported) => true,
28            _ => false,
29        }
30    }
31}
32
33impl From<bencode::Error> for Error {
34    fn from(error: bencode::Error) -> Self {
35        Self::Parsing(error.to_string())
36    }
37}
38
39impl From<io::Error> for Error {
40    fn from(err: io::Error) -> Self {
41        Self::Io(err)
42    }
43}