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
//! Serialization of version control objects into byte vectors.
//!
//! # Purpose
//!
//! This module defines the [`Encoder`] trait, which converts high-level
//! version control objects ([`Blob`](crate::Blob), [`Tree`](crate::Tree),
//! [`Commit`](crate::Commit), [`Tag`](crate::Tag)) into byte vectors
//! suitable for storage in an [`ObjectStore`](crate::ObjectStore) or
//! transmission via a [`Transport`](crate::Transport). It is the inverse of
//! [`Decoder`](crate::Decoder).
//!
//! # Design Rationale
//!
//! The trait provides separate methods for each object type rather than a
//! generic `encode<T>(&self, obj: &T)` for several reasons:
//!
//! - It avoids requiring all object types to implement a common trait.
//! - It allows encoder implementations to handle type-specific formatting.
//! - It keeps the domain data structures pure and decoupled from the
//! serialization interface.
//!
//! Encoding is fallible because an encoder may encounter unsupported
//! features, invalid internal state, or I/O errors during the process.
//! Therefore every method returns [`Result<Vec<u8>, VctrlError>`](crate::VctrlError).
//!
//! # Internal Mechanism
//!
//! A typical encoder implementation will access the fields of an object via
//! its public accessor methods, format them according to the chosen wire
//! format, and append them to a byte vector. The exact format is
//! implementation-defined; the trait only defines the contract.
//!
//! # Examples
//!
//! A complete dummy encoder implementation:
//!
//! ```
//! use libvctrl_handler::{Blob, Commit, Encoder, Hash, Tag, Tree, UserID, VctrlError};
//!
//! struct DummyEncoder;
//!
//! impl Encoder for DummyEncoder {
//! 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> {
//! Ok(vec![])
//! }
//!
//! fn encode_commit(&self, _commit: &Commit) -> Result<Vec<u8>, VctrlError> {
//! Ok(vec![])
//! }
//!
//! fn encode_tag(&self, _tag: &Tag) -> Result<Vec<u8>, VctrlError> {
//! Ok(vec![])
//! }
//! }
//!
//! let encoder = DummyEncoder;
//! let blob = Blob::new(b"data".to_vec());
//! assert_eq!(encoder.encode_blob(&blob).unwrap(), b"data");
//! ```
use crateVctrlError;
use crateBlob;
use crateCommit;
use crateTag;
use crateTree;
/// Defines the interface for serializing version control objects.
///
/// # Purpose
///
/// An `Encoder` translates in-memory data structures like [`Blob`] and
/// [`Commit`] into byte vectors suitable for storage in an
/// [`ObjectStore`](crate::ObjectStore) or transmission via a
/// [`Transport`](crate::Transport).
///
/// # Design Rationale
///
/// The trait provides separate methods for each object type rather than a
/// generic `encode<T>(&self, obj: &T)` to avoid requiring objects to
/// implement a shared trait, keeping the data structs pure and decoupled.
/// This design also permits specialized formatting for each object type.
///
/// # Why `&self`?
///
/// The methods take `&self` to allow a single encoder instance to be reused
/// for multiple encoding operations. Implementations may hold internal
/// buffers or configuration, and borrowing prevents unnecessary cloning of
/// the encoder itself.
///
/// # How It Works Internally
///
/// An implementation retrieves the necessary fields from the object via
/// accessor methods (e.g., [`Blob::data`](crate::Blob::data),
/// [`Commit::tree`](crate::Commit::tree)), formats them according to the
/// chosen serialization format, and writes the resulting bytes into a
/// [`Vec<u8>`]. The exact binary layout is not specified by this trait.
///
/// # Examples
///
/// A complete dummy encoder implementation:
///
/// ```
/// use libvctrl_handler::{Blob, Commit, Encoder, Hash, Tag, Tree, UserID, VctrlError};
///
/// struct DummyEncoder;
///
/// impl Encoder for DummyEncoder {
/// 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> {
/// Ok(vec![])
/// }
///
/// fn encode_commit(&self, _commit: &Commit) -> Result<Vec<u8>, VctrlError> {
/// Ok(vec![])
/// }
///
/// fn encode_tag(&self, _tag: &Tag) -> Result<Vec<u8>, VctrlError> {
/// Ok(vec![])
/// }
/// }
///
/// let encoder = DummyEncoder;
/// let blob = Blob::new(b"data".to_vec());
/// assert_eq!(encoder.encode_blob(&blob).unwrap(), b"data");
/// ```