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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Centralized error handling for Parcode.
//!
//! This module provides a robust error handling system that strictly avoids panics,
//! ensuring that all failure conditions are properly propagated through the `Result` type.
//!
//! ## Design Philosophy
//!
//! Parcode's error handling is designed with the following principles:
//!
//! 1. **No Panics:** All error conditions are represented as `Result` values. The library
//! enforces this through `#![deny(clippy::panic)]` and `#![deny(clippy::unwrap_used)]`.
//!
//! 2. **Contextual Information:** Errors include descriptive messages that help diagnose
//! the root cause of failures.
//!
//! 3. **Error Chaining:** Where possible, errors preserve the underlying cause through
//! the `source()` method, enabling full error traces.
//!
//! 4. **Cloneable Errors:** The [`ParcodeError`] type is `Clone`, allowing errors to be
//! shared across threads or stored for later analysis.
//!
//! ## Error Categories
//!
//! Errors are categorized by their domain:
//!
//! - **I/O Errors** ([`ParcodeError::Io`]): Low-level file system operations
//! - **Serialization Errors** ([`ParcodeError::Serialization`]): Bincode encoding/decoding
//! - **Compression Errors** ([`ParcodeError::Compression`]): Compression/decompression failures
//! - **Format Errors** ([`ParcodeError::Format`]): Invalid file format or corruption
//! - **Internal Errors** ([`ParcodeError::Internal`]): Logic errors (should not occur in production)
//!
//! ## Usage Patterns
//!
//! ### Basic Error Handling
//!
//! ```rust
//! use parcode::{Parcode, ParcodeError, ParcodeObject};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, ParcodeObject)]
//! struct MyData { val: i32 }
//! let my_data = MyData { val: 10 };
//!
//! match Parcode::save("data_err.par", &my_data) {
//! Ok(()) => println!("Saved successfully"),
//! Err(ParcodeError::Io(e)) => eprintln!("I/O error: {}", e),
//! Err(e) => eprintln!("Other error: {}", e),
//! }
//! # std::fs::remove_file("data_err.par").ok();
//! ```
//!
//! ### Error Propagation with `?`
//!
//! ```rust
//! use parcode::{Parcode, ParcodeObject};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, ParcodeObject)]
//! struct GameState { level: u32 }
//!
//! fn save_game_state(state: &GameState) -> parcode::Result<()> {
//! Parcode::save("game_err.par", state)?;
//! Ok(())
//! }
//! # let state = GameState { level: 1 };
//! # save_game_state(&state)?;
//! # std::fs::remove_file("game_err.par").ok();
//! # Ok::<(), parcode::ParcodeError>(())
//! ```
//!
//! ### Accessing Error Sources
//!
//! ```rust
//! use std::error::Error;
//! use parcode::{Parcode, ParcodeObject};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, ParcodeObject)]
//! struct MyData { val: i32 }
//! let my_data = MyData { val: 10 };
//!
//! if let Err(e) = Parcode::save("data_source.par", &my_data) {
//! eprintln!("Error: {}", e);
//! if let Some(source) = e.source() {
//! eprintln!("Caused by: {}", source);
//! }
//! }
//! # std::fs::remove_file("data_source.par").ok();
//! ```
use fmt;
use io;
use Arc;
/// A specialized `Result` type for Parcode operations.
///
/// This type alias is used throughout the library to simplify error handling.
/// It is equivalent to `std::result::Result<T, ParcodeError>`.
///
/// ## Examples
///
/// ```rust
/// use parcode::Result;
///
/// fn my_function() -> Result<i32> {
/// Ok(42)
/// }
/// ```
pub type Result<T> = Result;
/// The master error enum covering all failure domains in Parcode.
///
/// This enum represents all possible error conditions that can occur during
/// Parcode operations. Each variant corresponds to a specific failure domain
/// and contains contextual information about the error.
///
/// ## Variants
///
/// - **Io:** Low-level I/O failures (file not found, permission denied, disk full, etc.)
/// - **Serialization:** Bincode encoding/decoding failures (type mismatch, invalid data, etc.)
/// - **Compression:** Compression algorithm failures (corrupted compressed data, etc.)
/// - **Format:** File format validation failures (wrong magic bytes, version mismatch, corruption)
/// - **Internal:** Logic errors in the library (should not occur in production; please report as bugs)
///
/// ## Cloneability
///
/// This type is `Clone` to support error sharing across threads and storage for later analysis.
/// I/O errors are wrapped in `Arc` to make cloning efficient.
///
/// ## Examples
///
/// ```rust
/// use parcode::ParcodeError;
///
/// fn check_error(err: &ParcodeError) {
/// match err {
/// ParcodeError::Io(e) => println!("I/O error: {}", e),
/// ParcodeError::Format(msg) => println!("Format error: {}", msg),
/// _ => println!("Other error"),
/// }
/// }
/// ```