pwr_core/error.rs
1use thiserror::Error;
2
3/// Unified error type for all pwr operations.
4///
5/// Covers I/O failures, serialization errors, protocol violations,
6/// cryptographic failures, authentication problems, network issues,
7/// and storage-layer errors on the server side.
8#[derive(Error, Debug)]
9pub enum PwrError {
10 /// File or directory I/O operation failed.
11 #[error("I/O error: {0}")]
12 Io(#[from] std::io::Error),
13
14 /// TOML configuration file could not be parsed.
15 #[error("TOML parse error in {path}: {source}")]
16 TomlParse {
17 path: String,
18 #[source]
19 source: toml::de::Error,
20 },
21
22 /// TOML serialization failed.
23 #[error("TOML serialize error: {0}")]
24 TomlSerialize(#[from] toml::ser::Error),
25
26 /// JSON serialization or deserialization failed.
27 #[error("JSON error: {0}")]
28 Json(#[from] serde_json::Error),
29
30 /// Bincode serialization or deserialization failed (wire protocol).
31 #[error("Bincode error: {0}")]
32 Bincode(#[from] bincode::error::EncodeError),
33
34 /// Client or server configuration is missing or invalid.
35 #[error("Config error: {0}")]
36 Config(String),
37
38 /// No configuration found — user must run init first.
39 #[error("No config found — run `pwr init` first")]
40 NoConfig,
41
42 /// Protocol framing error: magic bytes mismatch, truncated frame,
43 /// or message exceeding size limit.
44 #[error("Protocol framing error: {0}")]
45 Framing(String),
46
47 /// Received an unexpected or invalid protocol message.
48 #[error("Protocol error: {0}")]
49 Protocol(String),
50
51 /// Cryptographic operation failed (key generation, encryption,
52 /// decryption, or hash computation).
53 #[error("Crypto error: {0}")]
54 Crypto(String),
55
56 /// Authentication with the server failed (wrong token, expired
57 /// credentials, or too many attempts).
58 #[error("Authentication failed: {0}")]
59 Auth(String),
60
61 /// Network connection could not be established or was lost.
62 #[error("Network error: {0}")]
63 Network(String),
64
65 /// Operation timed out.
66 #[error("Timeout: {0}")]
67 Timeout(String),
68
69 /// Requested project does not exist on the server.
70 #[error("Project not found: {0}")]
71 NotFound(String),
72
73 /// Project already exists (cannot overwrite without explicit flag).
74 #[error("Project already exists: {0}")]
75 AlreadyExists(String),
76
77 /// Stored project data is corrupted or unreadable.
78 #[error("Corrupted project data: {0}")]
79 Corrupted(String),
80
81 /// UUID parsing or formatting failed.
82 #[error("UUID error: {0}")]
83 Uuid(#[from] uuid::Error),
84}
85
86/// Convenience result type used throughout the codebase.
87pub type Result<T> = std::result::Result<T, PwrError>;