Skip to main content

weirdnet_storage/
error.rs

1//! Storage errors.
2
3#[derive(Debug, thiserror::Error)]
4pub enum StorageError {
5    /// The key did not open the file, or the file is not a database of this kind.
6    #[error("wrong key or not a {what} database ({detail})")]
7    WrongKey { what: String, detail: String },
8    /// Conversion was asked for a file that is not plaintext SQLite.
9    #[error("existing database is not a plaintext SQLite file")]
10    NotPlaintext,
11    /// A rekey left the file unencrypted (it was never encrypted).
12    #[error("database is not encrypted; rekey cannot encrypt it (convert it first)")]
13    NotEncrypted,
14    /// The new key does not open the file afterwards (SQLCipher can report success without
15    /// rekeying, for example on a read-only connection).
16    #[error("rekey did not take effect; the database keeps its old key")]
17    RekeyFailed,
18    /// SQLite was built without SQLCipher: a key would be ignored.
19    #[error("this SQLite has no encryption (SQLCipher); refusing to apply a key")]
20    NoCipher,
21    #[error("invalid {what}: {msg}")]
22    Invalid { what: &'static str, msg: String },
23    #[error(transparent)]
24    Io(#[from] std::io::Error),
25    #[error(transparent)]
26    Sqlite(#[from] rusqlite::Error),
27    #[cfg(feature = "sync")]
28    #[error(transparent)]
29    Json(#[from] serde_json::Error),
30}
31
32impl StorageError {
33    pub fn invalid(what: &'static str, msg: impl Into<String>) -> Self {
34        StorageError::Invalid { what, msg: msg.into() }
35    }
36}
37
38pub type Result<T> = std::result::Result<T, StorageError>;