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
236
237
238
239
240
241
242
243
244
245
//! Error handling for the `libvctrl_handler` version control contracts.
//!
//! # Purpose
//! This module defines [`VctrlError`], the unified error type returned by all
//! fallible operations within the crate. It encapsulates various failure modes
//! ranging from invalid input data to storage and serialization failures.
//!
//! # Design rationale
//! - **Simplicity and Cloning**: The error variants store `String` rather than
//! boxed trait objects (`Box<dyn std::error::Error>`). This ensures that
//! [`VctrlError`] implements [`Clone`], [`PartialEq`], and [`Eq`], which is
//! crucial for testing assertions and state comparisons.
//! - **Forward Compatibility**: The enum is marked `#[non_exhaustive]`. This
//! prevents downstream crates from exhaustively matching against it,
//! allowing new error variants to be added in future minor versions without
//! breaking the API.
//! - **`no_std` Readiness**: By avoiding complex heap-allocated error chains
//! and relying on `String`, the design keeps the door open for future
//! `#![no_std]` compatibility.
//!
//! # Internal mechanism
//! The [`std::error::Error`] trait is implemented explicitly. The `source`
//! method always returns `None` because the variant payloads are plain data
//! types (like `String` or [`Hash`](crate::Hash)), not wrapped causal errors.
use crateHash;
use fmt;
/// The unified error type returned by all fallible operations in the
/// `libvctrl_handler` crate.
///
/// # Design rationale
/// This enum is marked `#[non_exhaustive]` to ensure that adding new error
/// variants in the future is not considered a breaking change. Callers must
/// include a catch-all `_` arm when matching on it.
///
/// # Internal mechanism
/// Variants store `String` for messages to ensure the error type remains
/// `Clone` and `PartialEq`, unlike `Box<dyn Error>`.
///
/// # Examples
///
/// ```
/// use libvctrl_handler::{VctrlError, Hash};
///
/// let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
/// let err = VctrlError::ObjectNotFound(hash);
///
/// assert!(err.to_string().starts_with("Object not found:"));
/// ```
/// Formats the error using the given formatter.
///
/// # Design rationale
/// This implementation provides human-readable, context-rich error messages.
/// For example, [`InvalidHashLength`](VctrlError::InvalidHashLength) dynamically
/// references [`HASH_LENGTH`](crate::HASH_LENGTH) so the message is always
/// accurate even if the constant changes.
///
/// # Internal mechanism
/// It matches on `Self` and uses the `write!` macro to write the formatted
/// string directly to the formatter, avoiding intermediate allocations.
///
/// # Examples
///
/// ```
/// use libvctrl_handler::VctrlError;
/// use std::fmt::Display;
///
/// let err = VctrlError::Other("test".to_string());
/// let s = format!("{err}");
/// assert_eq!(s, "test");
/// ```
/// Implementation of the standard library's [`std::error::Error`] trait.
///
/// # Design rationale
/// Implementing this trait ensures that `VctrlError` integrates seamlessly
/// with the broader Rust error handling ecosystem, allowing it to be used
/// with crates like `anyhow` or `eyre`.
///
/// # Internal mechanism
/// The `source` method explicitly returns `None` for all variants. Because
/// the variant payloads are plain `String`s or value types (not wrapped
/// causal errors), there is no underlying error source to expose.
///
/// # Examples
///
/// ```
/// use libvctrl_handler::VctrlError;
/// use std::error::Error;
///
/// let err = VctrlError::IoError("disk full".to_string());
/// // Verifies it implements std::error::Error
/// fn assert_error<T: Error + ?Sized>(_: &T) {}
/// assert_error(&err);
/// assert!(err.source().is_none());
/// ```