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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! 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`).
//!
//! # Architecture
//!
//! The `libvctrl` ecosystem is built around a set of **abstract capabilities**
//! rather than concrete types. Each trait represents a single responsibility:
//!
//! | Trait | Responsibility | Typical implementor |
//! |---|---|---|
//! | [`ObjectStore`] | Content‑addressed key/value storage | In‑memory store, file‑system backend, database |
//! | [`RefStore`] | Mutable named pointers to objects | Branch & tag storage |
//! | [`Hasher`] | Cryptographic hash function | SHA‑512, SHA‑256, BLAKE3 |
//! | [`Encoder`] | Serialise domain objects to bytes | Binary format, Git‑compatible format |
//! | [`Decoder`] | Deserialise bytes back to domain objects | Same as Encoder |
//! | [`Signer`] | Create digital signatures | Ed25519, RSA, ECDSA |
//! | [`Verifier`] | Verify digital signatures | Same as Signer |
//! | [`Transport`] | Push/fetch raw object data across network | HTTP, SSH, custom protocol |
//!
//! By depending only on these traits, higher‑level logic (plumbing, porcelain)
//! becomes **backend‑agnostic** – you can swap storage backends, hashing
//! algorithms, serialisation formats, and network transports without changing
//! a single line of business logic.
//!
//! # The "everything is a trait" philosophy
//!
//! This crate does **not** provide a `Repository` struct with hard‑coded
//! behaviour. Instead, you compose the traits you need. Want a read‑only
//! repository that fetches objects over HTTP and verifies Ed25519 signatures?
//! Provide implementations of `ObjectStore` (that fetches on miss), `RefStore`,
//! `Hasher`, `Decoder`, `Verifier`, and `Transport`. The plumbing functions
//! will accept those implementations and do the right thing.
//!
//! # Safety, errors, and panics
//!
//! - **No panics** – All operations return [`Result<T, VctrlError>`].
//! Implementations must never panic (except on unrecoverable internal bugs,
//! which should be `unreachable!`).
//! - **Validation** – Where validation is expected (names, hashes), the
//! implementation must return the appropriate error; callers are
//! **not** required to pre‑validate inputs (but may do so for efficiency).
//! - **Thread‑safety** – Traits do not mandate `Sync` or `Send`. Implementors
//! should add these bounds if their backend supports concurrent access.
//! Consumers of the traits are encouraged to require `Send + Sync` only
//! where strictly necessary.
use crateVctrlError;
use crate;
// ============================================================================
// ObjectStore
// ============================================================================
/// A content‑addressable object store.
///
/// This is the lowest‑level storage abstraction. It maps a fixed‑length
/// [`Hash`] to a variable‑length byte sequence (the **object**). There is
/// exactly one object per hash, and the hash is **cryptographically derived**
/// from the content (hence “content‑addressable”).
///
/// # Why content‑addressable?
///
/// Content‑addressable storage has two critical properties:
/// 1. **Integrity** – any modification to the data changes its hash. There is
/// no way to silently corrupt an object without being detected.
/// 2. **Deduplication** – identical data produces identical hashes, so storing
/// the same content twice (e.g., identical files in different commits) costs
/// nothing.
///
/// # Preconditions (what the caller must guarantee)
///
/// - `hash` *must* be a valid [`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.
/// Failing to uphold it will make objects irretrievable or incorrectly
/// addressable.
/// - `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 (e.g., a [`Tree`] entry).
///
/// # Postconditions (what the store guarantees after a successful operation)
///
/// - After a successful [`put`](Self::put), calling [`exists`](Self::exists)
/// with the same hash returns `Ok(true)` (unless the object was deleted in
/// the meantime).
/// - After a successful [`put`](Self::put), calling [`get`](Self::get) with
/// the same hash returns the **identical** `data` that was stored.
/// - After a successful [`delete`](Self::delete), [`exists`](Self::exists)
/// returns `Ok(false)` and [`get`](Self::get) returns
/// [`VctrlError::ObjectNotFound`].
///
/// # Idempotency
///
/// Storing the same `(hash, data)` pair multiple times should succeed
/// without error. This makes batch operations simpler – an import script
/// can call `put` for every object without first checking whether it already
/// exists.
///
/// # Example (minimal in‑memory implementation)
///
/// ```rust
/// # 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 mutable mapping from human‑readable names to [`Hash`] values.
///
/// References are the **only** mutable primitive in `libvctrl`. Objects
/// themselves are immutable (content‑addressed), but references let you
/// update which commit a branch points to, or which object a lightweight
/// tag targets.
///
/// # Typical usage
///
/// - Branches: `"refs/heads/main"` → commit hash
/// - Lightweight tags: `"refs/tags/v1.0"` → commit hash
/// - Special refs: `"HEAD"` → current branch name or commit hash
///
/// # Preconditions
///
/// - `name` must be non‑empty and ≤ [`MAX_NAME_LENGTH`](crate::MAX_NAME_LENGTH).
/// - `hash` must be a valid [`Hash`].
///
/// # Postconditions
///
/// - After a successful [`set_ref`](Self::set_ref), [`get_ref`](Self::get_ref)
/// with the same name returns the same hash (unless overwritten).
/// - [`list_refs`](Self::list_refs) returns every name that was successfully
/// set and not yet deleted.
///
/// # Implementation notes
///
/// Implementations **must** validate the name (length, emptiness) and return
/// [`VctrlError::InvalidName`] on failure. The list returned by
/// [`list_refs`](Self::list_refs) may be in any order; callers must not
/// rely on ordering.
///
/// # Example (minimal in‑memory implementation)
///
/// ```rust
/// # 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.to_string(), *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.
///
/// This trait is the sole entry point for producing [`Hash`] values. All
/// object identity in `libvctrl` flows through a `Hasher` implementation.
///
/// # Requirements
///
/// - **Deterministic** – `hash(data)` must always return the same `Hash` for
/// the same `data` (no nonce, no randomness).
/// - **Collision‑resistant** – it must be computationally infeasible to find
/// two different inputs that produce the same `Hash`.
/// - **Fixed output length** – the returned `Hash` must be exactly
/// [`HASH_LENGTH`](crate::HASH_LENGTH) bytes.
///
/// # Why SHA‑512?
///
/// `libvctrl` ships with a battle‑tested SHA‑512 implementation
/// (`libvctrl_sha512`). You are free to provide your own hasher; as long as
/// it meets the contract, the entire ecosystem will work.
///
/// # Example (dummy hasher for illustration)
///
/// A real implementation would use a cryptographic library, but for
/// demonstration purposes we show a trivial (insecure!) hasher that always
/// returns the same zero‑filled hash.
///
/// ```rust
/// use libvctrl_handler::{Hash, Hasher, HASH_LENGTH};
///
/// struct DummyHasher;
/// impl Hasher for DummyHasher {
/// fn hash(&self, _data: &[u8]) -> Hash {
/// // NEVER do this in production! Always use a strong hash like SHA-512.
/// Hash::from_bytes(&[0u8; HASH_LENGTH]).expect("64 bytes")
/// }
/// }
///
/// let hasher = DummyHasher;
/// let h = hasher.hash(b"hello");
/// assert_eq!(h.as_bytes().len(), HASH_LENGTH);
/// ```
// ============================================================================
// Encoder
// ============================================================================
/// Serialises high‑level objects into a byte representation.
///
/// The encoder is responsible for defining a **wire format** for objects.
/// It takes domain types ([`Blob`], [`Tree`], [`Commit`], [`Tag`]) and
/// produces a `Vec<u8>` that can be stored in an [`ObjectStore`] or
/// transmitted over a [`Transport`].
///
/// # Round‑trip contract
///
/// For any object `obj` and a matching [`Decoder`] implementation,
/// `decoder.decode_*(encoder.encode_*(obj)?)` must return `Ok(obj)`.
/// This is the fundamental guarantee that data survives storage.
///
/// # Format freedom
///
/// The exact binary format is **entirely up to the implementor**. The
/// reference implementation in `libvctrl_core` uses a simple deterministic
/// binary format, but you could implement Git‑compatible formats, JSON,
/// CBOR, or anything else. The only requirement is that a corresponding
/// `Decoder` can reverse the process.
///
/// # Example (simple identity encoder for Blob)
///
/// This example implements a trivial encoder that passes blobs through as-is.
/// Other object types are not implemented and return errors.
///
/// ```rust
/// use libvctrl_handler::{Blob, Commit, Encoder, Tag, Tree, VctrlError};
///
/// struct SimpleEncoder;
/// impl Encoder for SimpleEncoder {
/// fn encode_blob(&self, blob: &Blob) -> Result<Vec<u8>, VctrlError> {
/// Ok(blob.data().to_vec())
/// }
/// fn encode_tree(&self, _tree: &Tree) -> Result<Vec<u8>, VctrlError> {
/// Err(VctrlError::Other("tree encoding not implemented".into()))
/// }
/// fn encode_commit(&self, _commit: &Commit) -> Result<Vec<u8>, VctrlError> {
/// Err(VctrlError::Other("commit encoding not implemented".into()))
/// }
/// fn encode_tag(&self, _tag: &Tag) -> Result<Vec<u8>, VctrlError> {
/// Err(VctrlError::Other("tag encoding not implemented".into()))
/// }
/// }
/// ```
// ============================================================================
// Decoder
// ============================================================================
/// Reconstructs objects from their byte representation.
///
/// The mirror image of [`Encoder`]. Given a byte slice that was produced by
/// a compatible encoder, a decoder must reconstruct the original object.
///
/// # Errors
///
/// Decoders are the **first line of defence** against data corruption and
/// malicious payloads. Implementations must:
/// - Reject truncated or oversized data ([`VctrlError::CorruptedData`]).
/// - Reject data that violates structural invariants (e.g., unsorted tree
/// entries, invalid UTF‑8) – also [`VctrlError::CorruptedData`].
/// - Respect the DoS‑prevention limits defined in [`constants`](crate::constants)
/// ([`MAX_BLOB_SIZE`], [`MAX_TREE_ENTRIES`], [`MAX_MESSAGE_LENGTH`]).
///
/// # Example (simple identity decoder for Blob)
///
/// This example shows a minimal decoder that only handles blobs.
/// Other object types are not implemented and return errors.
///
/// ```rust
/// use libvctrl_handler::{Blob, Commit, Decoder, Tag, Tree, VctrlError};
///
/// struct SimpleDecoder;
/// impl Decoder for SimpleDecoder {
/// fn decode_blob(&self, data: &[u8]) -> Result<Blob, VctrlError> {
/// Ok(Blob::new(data.to_vec()))
/// }
/// fn decode_tree(&self, _data: &[u8]) -> Result<Tree, VctrlError> {
/// Err(VctrlError::Other("tree decoding not implemented".into()))
/// }
/// fn decode_commit(&self, _data: &[u8]) -> Result<Commit, VctrlError> {
/// Err(VctrlError::Other("commit decoding not implemented".into()))
/// }
/// fn decode_tag(&self, _data: &[u8]) -> Result<Tag, VctrlError> {
/// Err(VctrlError::Other("tag decoding not implemented".into()))
/// }
/// }
/// ```
// ============================================================================
// Signer
// ============================================================================
/// A digital signature provider.
///
/// Used to produce cryptographic signatures over commit messages, tags, or
/// any other data. The key material and algorithm are implementation‑defined.
///
/// # Contract
/// - `sign(data)` must produce a signature that can be verified by a
/// corresponding [`Verifier`].
/// - The same data + key must always produce the same signature (deterministic
/// algorithms like Ed25519) or at least produce a signature that verifies
/// correctly (randomised algorithms like ECDSA).
///
/// # Example (stub)
///
/// ```rust
/// use libvctrl_handler::{Signer, VctrlError};
///
/// 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.
/// // Here we just return the data itself as a dummy signature.
/// Ok(data.to_vec())
/// }
/// }
/// ```
// ============================================================================
// Verifier
// ============================================================================
/// A digital signature verifier.
///
/// Matches a [`Signer`] implementation. Given data and a signature, returns
/// `Ok(true)` if the signature is valid, `Ok(false)` if it is not.
///
/// # Why not `Result<bool, …>` where `false` is an error?
/// Because an invalid signature is not a system failure – it's an expected
/// outcome during verification. Errors should be reserved for cases where
/// the verifier cannot operate (e.g., missing key, corrupted key).
///
/// # Example (stub)
///
/// ```rust
/// use libvctrl_handler::{Verifier, VctrlError};
///
/// struct StubVerifier;
/// impl Verifier for StubVerifier {
/// fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, VctrlError> {
/// // Dummy check: signature is valid iff it equals the data.
/// Ok(data == signature)
/// }
/// }
/// ```
// ============================================================================
// Transport
// ============================================================================
/// Object transport between repositories.
///
/// This trait abstracts the communication channel used to send and receive
/// raw object data. It does not handle authentication, encryption, or
/// negotiation – those are implementation details.
///
/// # Concurrent fetches
/// `fetch_object` takes `&self` (not `&mut self`) to allow multiple threads
/// to fetch objects simultaneously. Implementations that need mutable state
/// (e.g., an HTTP connection pool) should use interior mutability
/// (`Mutex`, `Atomic*`).
///
/// # Example (stub using a `HashMap`)
///
/// ```rust
/// # use std::collections::HashMap;
/// use libvctrl_handler::{Hash, Transport, VctrlError};
///
/// struct FakeTransport(HashMap<Hash, Vec<u8>>);
///
/// impl Transport for FakeTransport {
/// fn fetch_object(&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(())
/// }
/// }
/// ```