Skip to main content

weirdnet_keys/
error.rs

1//! Key errors. None of them carries key material, a password or plaintext.
2use std::path::PathBuf;
3
4#[derive(Debug, thiserror::Error)]
5pub enum KeysError {
6    #[error("crypto error: {0}")]
7    Crypto(String),
8    /// Wrong password or key, or tampered data (indistinguishable on purpose).
9    #[error("could not unlock: {0}")]
10    Unlock(String),
11    #[error("invalid {what}: {msg}")]
12    Invalid { what: &'static str, msg: String },
13    /// No envelope in this home.
14    #[error("keys are not initialized in {0}")]
15    NotInitialized(PathBuf),
16    #[error(transparent)]
17    Io(#[from] std::io::Error),
18    #[error("toml: {0}")]
19    TomlDe(#[from] toml::de::Error),
20    #[error("toml: {0}")]
21    TomlSer(#[from] toml::ser::Error),
22    #[error(transparent)]
23    Json(#[from] serde_json::Error),
24}
25
26impl KeysError {
27    pub fn invalid(what: &'static str, msg: impl Into<String>) -> Self {
28        KeysError::Invalid { what, msg: msg.into() }
29    }
30    pub(crate) fn crypto(msg: impl Into<String>) -> Self {
31        KeysError::Crypto(msg.into())
32    }
33}
34
35pub type Result<T> = std::result::Result<T, KeysError>;