|
| 1 | +use core::fmt; |
| 2 | + |
| 3 | +use crate::hex; |
| 4 | + |
| 5 | + |
| 6 | +/// Error |
| 7 | +#[derive(Debug, PartialEq, Eq)] |
| 8 | +pub enum Error { |
| 9 | + /// ID too big |
| 10 | + IdTooBig, |
| 11 | + /// Invalid ID size |
| 12 | + InvalidIdSize, |
| 13 | + /// IdSizeNotMatch |
| 14 | + IdSizeNotMatch, |
| 15 | + /// Frame size limit too small |
| 16 | + FrameSizeLimitTooSmall, |
| 17 | + /// Not sealed |
| 18 | + NotSealed, |
| 19 | + /// Already sealed |
| 20 | + AlreadySealed, |
| 21 | + /// Already built initial message |
| 22 | + AlreadyBuiltInitialMessage, |
| 23 | + /// Initiator error |
| 24 | + Initiator, |
| 25 | + /// Non-initiator error |
| 26 | + NonInitiator, |
| 27 | + /// Initiate after reconcile |
| 28 | + InitiateAfterReconcile, |
| 29 | + /// Unexpected mode |
| 30 | + UnexpectedMode(u64), |
| 31 | + /// Parse ends prematurely |
| 32 | + ParseEndsPrematurely, |
| 33 | + /// Duplicate item added |
| 34 | + DuplicateItemAdded, |
| 35 | + /// Invalid protocol version |
| 36 | + InvalidProtocolVersion, |
| 37 | + /// Unsupported protocol version |
| 38 | + UnsupportedProtocolVersion, |
| 39 | + /// Unexpected output |
| 40 | + UnexpectedOutput { |
| 41 | + /// Expected output |
| 42 | + expected: String, |
| 43 | + /// Found output |
| 44 | + found: String, |
| 45 | + }, |
| 46 | + /// Hex error |
| 47 | + Hex(hex::Error), |
| 48 | + /// Bad range |
| 49 | + BadRange, |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(feature = "std")] |
| 53 | +impl std::error::Error for Error {} |
| 54 | + |
| 55 | +impl fmt::Display for Error { |
| 56 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | + match self { |
| 58 | + Self::IdTooBig => write!(f, "ID too big"), |
| 59 | + Self::InvalidIdSize => write!(f, "Invalid ID size"), |
| 60 | + Self::IdSizeNotMatch => write!(f, "Current item ID not match the client ID size"), |
| 61 | + Self::FrameSizeLimitTooSmall => write!(f, "Frame size limit too small"), |
| 62 | + Self::NotSealed => write!(f, "Not sealed"), |
| 63 | + Self::AlreadySealed => write!(f, "Already sealed"), |
| 64 | + Self::AlreadyBuiltInitialMessage => write!(f, "Already built initial message"), |
| 65 | + Self::Initiator => write!(f, "initiator not asking for have/need IDs"), |
| 66 | + Self::NonInitiator => write!(f, "non-initiator asking for have/need IDs"), |
| 67 | + Self::InitiateAfterReconcile => write!(f, "can't initiate after reconcile"), |
| 68 | + Self::UnexpectedMode(m) => write!(f, "Unexpected mode: {}", m), |
| 69 | + Self::ParseEndsPrematurely => write!(f, "parse ends prematurely"), |
| 70 | + Self::DuplicateItemAdded => write!(f, "duplicate item added"), |
| 71 | + Self::InvalidProtocolVersion => write!(f, "invalid negentropy protocol version byte"), |
| 72 | + Self::UnsupportedProtocolVersion => { |
| 73 | + write!(f, "server does not support our negentropy protocol version") |
| 74 | + } |
| 75 | + Self::UnexpectedOutput { expected, found } => write!( |
| 76 | + f, |
| 77 | + "Unexpected output: expected={}, found={}", |
| 78 | + expected, found |
| 79 | + ), |
| 80 | + Self::Hex(e) => write!(f, "Hex: {}", e), |
| 81 | + Self::BadRange => write!(f, "bad range"), |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl From<hex::Error> for Error { |
| 87 | + fn from(e: hex::Error) -> Self { |
| 88 | + Self::Hex(e) |
| 89 | + } |
| 90 | +} |
0 commit comments