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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Core abstractions that define the contract for every component.
//!
//! **No concrete implementations are allowed in this crate.**
//! These traits form the boundary between the fundamental definitions
//! and the actual implementations found in other crates
//! (e.g., `libvctrl_core`).
use crateVctrlError;
use crate;
// ============================================================================
// ObjectStore
// ============================================================================
/// A content‑addressable object store.
///
/// Implementations may store objects in memory, on disk, in a database,
/// or any other backend. The only requirement is that objects are
/// indexed by their [`struct@Hash`].
///
/// # Preconditions (for callers)
/// - `hash` must be a valid [`struct@Hash`] (guaranteed by its constructor).
/// - `data` provided to [`put`](Self::put) should be the exact bytes
/// that produced `hash`; the store does **not** verify this relationship.
/// - `hash` passed to [`get`](Self::get) or [`exists`](Self::exists) must
/// have been obtained from a previous [`put`](Self::put) or from a trusted source.
///
/// # Postconditions (guarantees after successful operations)
/// - After a successful [`put`](Self::put), calling [`exists`](Self::exists) with the same
/// hash will return `Ok(true)` (unless the object was deleted in the meantime).
/// - After a successful [`put`](Self::put), calling [`get`](Self::get) with the same hash
/// will return the identical `data` that was stored.
/// - A successful [`delete`](Self::delete) will cause subsequent [`exists`](Self::exists)
/// to return `Ok(false)` and [`get`](Self::get) to return [`VctrlError::ObjectNotFound`].
///
/// # Implementation notes
/// - The store must be thread‑safe if shared across threads (this is left
/// to the implementor; the trait does not enforce `Sync` or `Send`).
/// - Implementations should treat [`put`](Self::put) as idempotent: storing the same
/// `(hash, data)` pair multiple times should not fail.
/// - The [`exists`](Self::exists) method is fallible because real storage backends
/// may encounter I/O errors. Implementations must never panic.
///
/// # Example (minimal in‑memory implementation)
/// ```rust,ignore
/// # use std::collections::HashMap;
/// # use libvctrl_handler::*;
/// #
/// struct MemStore(HashMap<Hash, Vec<u8>>);
///
/// impl ObjectStore for MemStore {
/// fn put(&mut self, hash: &Hash, data: &[u8]) -> Result<(), VctrlError> {
/// self.0.insert(*hash, data.to_vec());
/// Ok(())
/// }
/// fn get(&self, hash: &Hash) -> Result<Vec<u8>, VctrlError> {
/// self.0.get(hash).cloned()
/// .ok_or(VctrlError::ObjectNotFound(*hash))
/// }
/// fn delete(&mut self, hash: &Hash) -> Result<(), VctrlError> {
/// self.0.remove(hash);
/// Ok(())
/// }
/// fn exists(&self, hash: &Hash) -> Result<bool, VctrlError> {
/// Ok(self.0.contains_key(hash))
/// }
/// }
/// ```
// ============================================================================
// RefStore
// ============================================================================
/// A reference store – maps names to [`struct@Hash`] values.
///
/// References are typically used for branches, tags (lightweight),
/// or any symbolic name that points to a commit or other object.
///
/// # Preconditions
/// - `name` must be non‑empty and ≤ [`MAX_NAME_LENGTH`](crate::MAX_NAME_LENGTH).
/// - `hash` must be a valid [`struct@Hash`].
///
/// # Postconditions
/// - After a successful [`set_ref`](Self::set_ref), calling [`get_ref`](Self::get_ref)
/// with the same name must return the same hash (unless overwritten or deleted).
/// - [`list_refs`](Self::list_refs) must return every name that was successfully
/// set and not yet deleted.
///
/// # Implementation notes
/// - Implementations must validate the name (length, empty) and return
/// [`VctrlError::InvalidName`] on failure.
/// - The list returned by [`list_refs`](Self::list_refs) may be in any order.
///
/// # Example (minimal in‑memory implementation)
/// ```rust,ignore
/// # use std::collections::HashMap;
/// # use libvctrl_handler::*;
/// #
/// struct MemRefs(HashMap<String, Hash>);
///
/// impl RefStore for MemRefs {
/// fn set_ref(&mut self, name: &str, hash: &Hash) -> Result<(), VctrlError> {
/// if name.is_empty() || name.len() > MAX_NAME_LENGTH {
/// return Err(VctrlError::InvalidName(name.into()));
/// }
/// self.0.insert(name.into(), *hash);
/// Ok(())
/// }
/// fn get_ref(&self, name: &str) -> Result<Hash, VctrlError> {
/// self.0.get(name).copied()
/// .ok_or_else(|| VctrlError::RefNotFound(name.into()))
/// }
/// fn delete_ref(&mut self, name: &str) -> Result<(), VctrlError> {
/// self.0.remove(name);
/// Ok(())
/// }
/// fn list_refs(&self) -> Result<Vec<String>, VctrlError> {
/// Ok(self.0.keys().cloned().collect())
/// }
/// }
/// ```
// ============================================================================
// Hasher
// ============================================================================
/// A cryptographically secure hash function.
///
/// Implementations must return a [`struct@Hash`] whose length is exactly
/// [`HASH_LENGTH`](crate::HASH_LENGTH) bytes.
///
/// # Contract
/// - The same input data must always produce the same hash.
/// - Different inputs should produce different hashes (collision resistance).
/// - The output length must be exactly [`HASH_LENGTH`](crate::HASH_LENGTH).
///
/// # Example (using SHA-512 from the `sha2` crate)
/// ```rust,ignore
/// # use sha2::{Sha512, Digest};
/// # use libvctrl_handler::*;
/// #
/// struct Sha512Hasher;
///
/// impl Hasher for Sha512Hasher {
/// fn hash(&self, data: &[u8]) -> Hash {
/// let digest = Sha512::digest(data);
/// Hash::from_bytes(&digest).expect("SHA-512 is 64 bytes")
/// }
/// }
/// ```
// ============================================================================
// Encoder
// ============================================================================
/// Serializes high‑level objects into a byte representation suitable for storage.
///
/// # Round‑trip property
/// For any object `obj` of a given type, a valid [`Decoder`] implementation
/// must be able to reconstruct the original object from the bytes produced
/// by this encoder:
/// ```text
/// decoder.decode_*(encoder.encode_*(obj)?) == Ok(obj)
/// ```
/// The exact binary format is unspecified and left to the implementor.
///
/// # Errors
/// Methods may return [`VctrlError::SerializationError`] if the object
/// cannot be encoded (e.g., contains invalid data according to the format).
///
/// # Example (trivial identity encoder for Blob – not suitable for production)
/// ```rust,ignore
/// # use libvctrl_handler::*;
/// #
/// struct IdentityEncoder;
///
/// impl Encoder for IdentityEncoder {
/// fn encode_blob(&self, blob: &Blob) -> Result<Vec<u8>, VctrlError> {
/// Ok(blob.data().to_vec())
/// }
/// // ... other methods
/// # fn encode_tree(&self, _: &Tree) -> Result<Vec<u8>, VctrlError> { todo!() }
/// # fn encode_commit(&self, _: &Commit) -> Result<Vec<u8>, VctrlError> { todo!() }
/// # fn encode_tag(&self, _: &Tag) -> Result<Vec<u8>, VctrlError> { todo!() }
/// }
/// ```
// ============================================================================
// Decoder
// ============================================================================
/// Reconstructs objects from their byte representation.
///
/// # Round‑trip property
/// For any valid encoded byte sequence produced by a corresponding [`Encoder`],
/// the decoder must return the original object.
///
/// # Errors
/// Methods must return [`VctrlError::CorruptedData`] or
/// [`VctrlError::SerializationError`] if the data is malformed, truncated,
/// or otherwise invalid.
///
/// # Example (trivial identity decoder for Blob)
/// ```rust,ignore
/// # use libvctrl_handler::*;
/// #
/// struct IdentityDecoder;
///
/// impl Decoder for IdentityDecoder {
/// fn decode_blob(&self, data: &[u8]) -> Result<Blob, VctrlError> {
/// Ok(Blob::new(data.to_vec()))
/// }
/// // ... other methods
/// # fn decode_tree(&self, _: &[u8]) -> Result<Tree, VctrlError> { todo!() }
/// # fn decode_commit(&self, _: &[u8]) -> Result<Commit, VctrlError> { todo!() }
/// # fn decode_tag(&self, _: &[u8]) -> Result<Tag, VctrlError> { todo!() }
/// }
/// ```
// ============================================================================
// Signer
// ============================================================================
/// A digital signature provider.
///
/// # Contract
/// - [`sign`](Self::sign) must produce a deterministic or verifiable signature
/// for a given input and key (the key management is implementation‑defined).
/// - The signature must be verifiable by a corresponding [`Verifier`].
///
/// # Implementation notes
/// - The trait does not dictate the algorithm (Ed25519, RSA, etc.) or key storage.
/// - Implementations may be stateless (if the key is provided externally) or
/// stateful (if the signer holds the key internally).
///
/// # Example (stub)
/// ```rust,ignore
/// # use libvctrl_handler::*;
/// #
/// struct StubSigner;
///
/// impl Signer for StubSigner {
/// fn sign(&self, data: &[u8]) -> Result<Vec<u8>, VctrlError> {
/// // In a real implementation, this would produce a cryptographic signature.
/// Ok(data.to_vec()) // dummy signature
/// }
/// }
/// ```
// ============================================================================
// Verifier
// ============================================================================
/// A digital signature verifier.
///
/// # Contract
/// - [`verify`](Self::verify) must return `Ok(true)` if and only if the
/// signature is valid for the given data and the configured key.
/// - If the signature is invalid or doesn't match, it must return `Ok(false)`.
///
/// # Errors
/// Returns an error if the verification process itself cannot be completed
/// (e.g., corrupted key material, unsupported algorithm), not when the
/// signature is simply invalid.
///
/// # Example (stub)
/// ```rust,ignore
/// # use libvctrl_handler::*;
/// #
/// struct StubVerifier;
///
/// impl Verifier for StubVerifier {
/// fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, VctrlError> {
/// Ok(data == signature) // dummy check
/// }
/// }
/// ```
// ============================================================================
// Transport
// ============================================================================
/// Object transport between repositories (fetch/push).
///
/// # Contract
/// - [`fetch_object`](Self::fetch_object) must return the exact bytes that were
/// pushed via [`push_object`](Self::push_object) on the remote side.
/// - Both methods operate on raw bytes; no encoding/decoding is performed.
///
/// # Implementation notes
/// - The transport protocol (HTTP, SSH, custom) and authentication are
/// implementation details. The trait only represents the data transfer.
/// - Implementors should retry transient failures if appropriate, but
/// must surface permanent errors via the returned `Result`.
///
/// # Example (stub)
/// ```rust,ignore
/// # use std::collections::HashMap;
/// # use libvctrl_handler::*;
/// #
/// struct FakeTransport(HashMap<Hash, Vec<u8>>);
///
/// impl Transport for FakeTransport {
/// fn fetch_object(&mut self, hash: &Hash) -> Result<Vec<u8>, VctrlError> {
/// self.0.get(hash).cloned()
/// .ok_or(VctrlError::ObjectNotFound(*hash))
/// }
/// fn push_object(&mut self, hash: &Hash, data: &[u8]) -> Result<(), VctrlError> {
/// self.0.insert(*hash, data.to_vec());
/// Ok(())
/// }
/// }
/// ```