mini_sign/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use scrypt::{errors::InvalidOutputLen, password_hash::rand_core};

/// Error kind for minisign-rs
#[derive(Debug, Clone)]
pub enum ErrorKind {
    Io,
    Kdf,
    PrehashedMismatch,
    PublicKey,
    SecretKey,
    SignatureError,
}
impl std::fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorKind::Io => write!(f, "io error"),
            ErrorKind::Kdf => write!(f, "kdf error"),
            ErrorKind::PrehashedMismatch => write!(f, "prehashed mismatch"),
            ErrorKind::PublicKey => write!(f, "public key error"),
            ErrorKind::SecretKey => write!(f, "secret key error"),
            ErrorKind::SignatureError => write!(f, "signature error"),
        }
    }
}
/// Error type for minisign-rs
///
/// This type is used for all errors in minisign-rs
#[derive(Debug)]
pub struct SError {
    kind: ErrorKind,
    error: Box<dyn std::error::Error + Send + Sync>,
}
impl std::error::Error for SError {}
impl std::fmt::Display for SError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "kind:{} error:{}", self.kind, self.error)
    }
}
impl SError {
    pub(crate) fn new<E>(kind: ErrorKind, error: E) -> Self
    where
        E: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        Self {
            kind,
            error: error.into(),
        }
    }
}
impl From<rand_core::Error> for SError {
    fn from(error: rand_core::Error) -> Self {
        Self {
            kind: ErrorKind::Kdf,
            error: Box::new(error),
        }
    }
}
impl From<std::io::Error> for SError {
    fn from(error: std::io::Error) -> Self {
        Self {
            kind: ErrorKind::Io,
            error: Box::new(error),
        }
    }
}
impl From<ed25519_dalek::SignatureError> for SError {
    fn from(error: ed25519_dalek::SignatureError) -> Self {
        Self {
            kind: ErrorKind::SignatureError,
            error: Box::new(error),
        }
    }
}
impl From<scrypt::errors::InvalidParams> for SError {
    fn from(err: scrypt::errors::InvalidParams) -> SError {
        SError::new(ErrorKind::Kdf, err.to_string())
    }
}
impl From<scrypt::errors::InvalidOutputLen> for SError {
    fn from(err: InvalidOutputLen) -> SError {
        SError::new(ErrorKind::Kdf, err.to_string())
    }
}
pub type Result<T> = std::result::Result<T, SError>;