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
//! Builder pattern for constructing [`Blob`](libvctrl_handler::Blob) objects.
//!
//! # Purpose
//!
//! This module provides the [`BlobBuilder`], an ergonomic utility for
//! incrementally constructing version control blobs. While a [`Blob`] can be
//! created directly via [`Blob::new`](libvctrl_handler::Blob::new), the
//! builder pattern provides a consistent API across all object types in the
//! system.
//!
//! # Design Rationale
//!
//! - **API consistency**: Complex objects like commits and trees have many
//! fields and benefit greatly from the builder pattern. Providing a builder
//! for blobs ensures a uniform construction experience across the crate.
//! - **Extensibility**: If future versions of the system require
//! pre-processing (like compression) or validation (like checking
//! `MAX_BLOB_SIZE`) before creating a blob, this logic can be added to the
//! builder without breaking the existing `Blob::new` API.
//! - **Ownership management**: The builder takes ownership of the underlying
//! `Vec<u8>` during the `with_data` phase. When [`build`] is called, the
//! vector is moved into the final [`Blob`] with zero heap allocations.
//! - **Fluent interface**: The builder consumes and returns `self` by value,
//! enabling method chaining that reads naturally.
//!
//! # Internal Mechanism
//!
//! The builder holds a private `Vec<u8>`. The [`build`] method consumes the
//! builder and moves the vector directly into a new [`Blob`] instance.
//! The `new` method initializes an empty buffer; `with_data` replaces the
//! buffer with caller-provided content; and `build` converts the builder into
//! a finalized blob.
//!
//! # Examples
//!
//! Building a blob with data:
//!
//! ```
//! use libvctrl_core::object::BlobBuilder;
//!
//! let blob = BlobBuilder::new()
//! .with_data(b"file content".to_vec())
//! .build();
//!
//! assert_eq!(blob.size(), 12);
//! ```
//!
//! Building an empty blob using `Default`:
//!
//! ```
//! use libvctrl_core::object::BlobBuilder;
//!
//! let blob = BlobBuilder::default().build();
//! assert!(blob.is_empty());
//! ```
use Blob;
/// A builder for creating [`Blob`](libvctrl_handler::Blob) objects.
///
/// # Purpose
///
/// Provides a fluent interface for assembling a blob's data before
/// finalizing it into an immutable object.
///
/// # Design Rationale
///
/// Implements the standard builder pattern. It derives [`Default`] so it can
/// be easily instantiated, and [`Debug`] for logging purposes. The `build`
/// method consumes `self`, preventing the reuse of the builder after the
/// data has been moved into the final blob.
///
/// # Field Privacy
///
/// The internal `data` field is private. This encapsulation ensures that
/// all modifications go through the builder's methods, preserving the
/// linear construction flow and preventing direct external mutation.
///
/// # Memory Layout
///
/// The builder owns a [`Vec<u8>`], which consists of a pointer, length, and
/// capacity. When the builder is created, this vector is empty and allocates
/// nothing on the heap. If `with_data` is called, the vector takes ownership
/// of the provided buffer. When `build` is called, the vector is moved into
/// the [`Blob`] without any cloning or heap reallocation.
///
/// # Examples
///
/// Building a blob with some data:
///
/// ```
/// use libvctrl_core::object::BlobBuilder;
///
/// let blob = BlobBuilder::new()
/// .with_data(b"file content".to_vec())
/// .build();
///
/// assert_eq!(blob.size(), 12);
/// ```
///
/// Building an empty blob using `Default`:
///
/// ```
/// use libvctrl_core::object::BlobBuilder;
///
/// let blob = BlobBuilder::default().build();
/// assert!(blob.is_empty());
/// ```