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
//! # `libvctrl_core` – Batteries-Included Implementations for `libvctrl_handler`
//!
//! **The reference implementation layer for building modular version control systems.**
//!
//! This crate provides production-ready, safe implementations of the
//! abstract contracts defined in [`libvctrl_handler`]. It is the
//! *first consumer* of those contracts, validating their design by
//! building a complete, working VCS backend stack.
//!
//! ## Why this crate exists
//!
//! - **Validation of contracts** – If a trait is too difficult to implement,
//! the problem is caught here before downstream users encounter it.
//! - **Batteries included** – Get a working VCS core (hashing, storage,
//! encoding, validation) in seconds, without writing boilerplate.
//! - **Quality exemplar** – All code is safe, strictly linted (`#![forbid(unsafe_code)]`,
//! `clippy::pedantic`, `clippy::nursery`), heavily tested, and documented
//! to serve as a model for custom backend implementations.
//!
//! ## Architecture & Modules
//!
//! The crate is structured by domain responsibility, mirroring the
//! separations in `libvctrl_handler`:
//!
//! | Module | Purpose | Key types/traits implemented |
//! |---|---|---|
//! | [`codec`] | Binary serialization/deserialization | `Encoder`, `Decoder` (via `BinaryEncoder`, `BinaryDecoder`) |
//! | [`hash`] | Cryptographic hashing | `Hasher` (via `Sha512Hasher`) |
//! | [`object`] | Builder patterns for ergonomic construction | `BlobBuilder`, `CommitBuilder`, `TagBuilder`, `TreeBuilder` |
//! | [`store`] | Ephemeral in-memory storage | `ObjectStore` (via `MemoryStore`), `RefStore` (via `MemoryRefStore`) |
//! | [`validate`] | Security and structure validation | `validate_name`, `validate_hash_bytes` |
//!
//! ## Key Features
//!
//! - **Streaming object reads** – [`MemoryStore::get`] returns
//! [`Box<dyn std::io::Read>`][std::io::Read] for zero-copy, lazy access,
//! aligning with the `libvctrl_handler` v4.0.0 streaming contracts.
//! - **Iterator-based ref listing** – [`MemoryRefStore::list_refs`] returns
//! a lazy iterator, enabling efficient handling of millions of references.
//! - **Full POSIX tree fidelity** – Encoder/decoder support all five
//! [`EntryKind`][libvctrl_handler::EntryKind] variants:
//! `Blob`, `Executable`, `Symlink`, `Tree`, `Submodule`.
//! - **Robust binary format** – Compact, little-endian binary encoding
//! with versioning, bounds checks, and `DoS` protection.
//! - **Defensive validation** – [`validate_name`][crate::validate::name::validate_name]
//! prevents path traversal attacks; [`validate_hash_bytes`][crate::validate::hash::validate_hash_bytes]
//! enforces strict hash integrity.
//! - **Thread-safe and safe** – `#![forbid(unsafe_code)]` guarantees no
//! undefined behavior; all types are `Send + Sync`.
//!
//! ## Quick Start
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! libvctrl_core = "1.1"
//! ```
//!
//! Then integrate hashing, encoding, and storage in one go:
//!
//! ```
//! use libvctrl_handler::{Blob, Encoder, Hasher, ObjectStore};
//! use libvctrl_core::codec::BinaryEncoder;
//! use libvctrl_core::hash::Sha512Hasher;
//! use libvctrl_core::store::MemoryStore;
//! use std::io::Read;
//!
//! // 1. Create content
//! let blob = Blob::new(b"my content".to_vec());
//!
//! // 2. Encode to deterministic bytes
//! let encoder = BinaryEncoder;
//! let bytes = encoder.encode_blob(&blob).unwrap();
//!
//! // 3. Hash the bytes to get a content address
//! let hasher = Sha512Hasher;
//! let hash = hasher.hash(&bytes).unwrap();
//!
//! // 4. Store the encoded bytes in memory
//! let mut store = MemoryStore::new();
//! store.put(&hash, &bytes).unwrap();
//!
//! // 5. Read back via streaming interface
//! let mut reader = store.get(&hash).unwrap();
//! let mut buf = Vec::new();
//! reader.read_to_end(&mut buf).unwrap();
//! assert_eq!(buf, bytes);
//! ```
use proptest as _;
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/// Binary serialization and deserialization implementations.
///
/// # Purpose
/// Provides the [`BinaryEncoder`](crate::codec::BinaryEncoder) and
/// [`BinaryDecoder`](crate::codec::BinaryDecoder) which translate in-memory
/// objects into a compact, deterministic byte format.
///
/// # Examples
///
/// ```
/// use libvctrl_handler::{Blob, Encoder, Decoder};
/// use libvctrl_core::codec::{BinaryEncoder, BinaryDecoder};
///
/// let blob = Blob::new(b"data".to_vec());
/// let bytes = BinaryEncoder.encode_blob(&blob).unwrap();
/// let decoded = BinaryDecoder.decode_blob(&bytes).unwrap();
/// assert_eq!(decoded, blob);
/// ```
/// Cryptographic hashing implementations.
///
/// # Purpose
/// Provides concrete [`Hasher`](libvctrl_handler::Hasher) implementations,
/// such as [`Sha512Hasher`](crate::hash::Sha512Hasher), for content addressing.
///
/// # Examples
///
/// ```
/// use libvctrl_handler::Hasher;
/// use libvctrl_core::hash::Sha512Hasher;
///
/// let hasher = Sha512Hasher;
/// let hash = hasher.hash(b"data").unwrap();
/// assert_eq!(hash.as_bytes().len(), 64);
/// ```
/// Builder patterns for constructing version control objects.
///
/// # Purpose
/// Provides fluent APIs like [`CommitBuilder`](crate::object::CommitBuilder)
/// to ergonomically assemble complex objects step-by-step.
///
/// # Examples
///
/// ```
/// use libvctrl_core::object::BlobBuilder;
///
/// let blob = BlobBuilder::new()
/// .with_data(b"hello".to_vec())
/// .build();
/// assert_eq!(blob.size(), 5);
/// ```
/// Storage backend implementations.
///
/// # Purpose
/// Provides concrete [`ObjectStore`](libvctrl_handler::ObjectStore) and
/// [`RefStore`](libvctrl_handler::RefStore) implementations, such as
/// [`MemoryStore`](crate::store::MemoryStore), for persisting data in RAM.
///
/// # Examples
///
/// ```
/// use libvctrl_core::store::MemoryStore;
/// use libvctrl_handler::{Hash, ObjectStore};
/// use std::io::Read;
///
/// let mut store = MemoryStore::new();
/// let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
/// store.put(&hash, b"data").unwrap();
///
/// let mut reader = store.get(&hash).unwrap();
/// let mut buf = Vec::new();
/// reader.read_to_end(&mut buf).unwrap();
/// assert_eq!(buf, b"data");
/// ```
/// Validation utilities for structural integrity and security.
///
/// # Purpose
/// Provides helper functions to validate raw inputs (like names and hashes)
/// before they are turned into strongly-typed objects, preventing path
/// traversal and resource exhaustion.
///
/// # Examples
///
/// ```
/// use libvctrl_core::validate::name::validate_name;
///
/// assert!(validate_name("valid_name").is_ok());
/// assert!(validate_name("../invalid").is_err());
/// ```