signer_core/
error.rs

1use thiserror::Error;
2
3/// 本 crate 公共错误类型
4#[derive(Debug, Error)]
5pub enum SignerError {
6    /// 通用带字符串信息的错误
7    #[error("{0}")]
8    Msg(String),
9
10    /// IO 错误
11    #[error(transparent)]
12    Io(#[from] std::io::Error),
13
14    /// JSON 序列化 / 反序列化错误
15    #[error(transparent)]
16    SerdeJson(#[from] serde_json::Error),
17
18    /// Base64 解码错误
19    #[error(transparent)]
20    Base64(#[from] base64::DecodeError),
21
22    /// ChaCha20Poly1305 加解密错误
23    #[error("crypto error: {0}")]
24    Crypto(String),
25
26    /// UTF8 转换错误
27    #[error(transparent)]
28    Utf8(#[from] std::string::FromUtf8Error),
29
30    /// Schnorrkel 签名相关错误
31    #[error("signature error: {0}")]
32    Signature(String),
33}
34
35/// 统一结果类型
36pub type Result<T> = std::result::Result<T, SignerError>;
37
38impl From<chacha20poly1305::aead::Error> for SignerError {
39    fn from(err: chacha20poly1305::aead::Error) -> Self {
40        SignerError::Crypto(err.to_string())
41    }
42}
43
44impl From<schnorrkel::SignatureError> for SignerError {
45    fn from(err: schnorrkel::SignatureError) -> Self {
46        SignerError::Signature(err.to_string())
47    }
48}