1
//! Declare an error type for the tor-consdiff crate.
2

            
3
use thiserror::Error;
4

            
5
use std::num::ParseIntError;
6

            
7
/// An error type from the tor-consdiff crate.
8
#[derive(Clone, Debug, Error)]
9
#[non_exhaustive]
10
pub enum Error {
11
    /// We got a consensus diff that we couldn't parse, or which we found
12
    /// to be somehow invalid.
13
    // TODO: it would be neat to have line numbers here.
14
    #[error("Invalid diff: {0}")]
15
    BadDiff(&'static str),
16

            
17
    /// We got a consensus diff that looked valid, but we couldn't apply it
18
    /// to the given input.
19
    #[error("Diff didn't apply to input: {0}")]
20
    CantApply(&'static str),
21
}
22

            
23
impl From<ParseIntError> for Error {
24
    fn from(_e: ParseIntError) -> Error {
25
        Error::BadDiff("can't parse line number")
26
    }
27
}
28
impl From<hex::FromHexError> for Error {
29
    fn from(_e: hex::FromHexError) -> Error {
30
        Error::BadDiff("invalid hexadecimal in 'hash' line")
31
    }
32
}