libvctrl_core 3.0.0

Reference implementations of the libvctrl contracts (in-memory store, SHA-512 hasher, binary codec)
Documentation
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
//! # Binary Encoder
//!
//! This module provides a deterministic, versioned, little-endian binary
//! encoder for every core object type defined by `libvctrl_handler`.
//!
//! The encoder is the counterpart to [`BinaryDecoder`](super::binary_decoder::BinaryDecoder).
//! Data written by this encoder can always be decoded back into an equivalent
//! object, provided the same system limits and version are used.
//!
//! ## Design rationale
//!
//! Version control objects are content-addressed. Deterministic serialization
//! is therefore critical: the same object must always produce exactly the same
//! bytes, otherwise the hash changes and the object becomes unreachable.
//!
//! The encoder achieves determinism by:
//!
//! - Using a fixed version byte.
//! - Using little-endian integer encoding on all supported platforms.
//! - Writing fields in a strict, documented order.
//! - Never depending on platform-specific layouts.
//!
//! ## How it works
//!
//! Every `encode_*` method writes directly to the supplied writer. Length
//! prefixes are validated before conversion to prevent silent truncation.
//! All string fields are encoded as a one-byte length followed by UTF-8 bytes.
//! The writer uses [`std::io::Write::write_all`] to guarantee complete writes.

use libvctrl_handler::{
    Blob, Commit, Encoder, EntryKind, MAX_MESSAGE_LENGTH, Tag, Tree, VctrlError,
};
use std::io::Write;

/// The current version of the binary encoding format.
///
/// This version byte is written as the first byte of every encoded object.
/// The decoder rejects any input whose first byte does not equal this value.
///
/// # Examples
///
/// ```
/// # use libvctrl_core::codec::VERSION;
/// assert_eq!(VERSION, 3);
/// ```
pub const VERSION: u8 = 3;

/// An encoder for the binary format of Git objects.
///
/// `BinaryEncoder` is a stateless, zero-sized type that implements the
/// [`Encoder`] trait. It converts high-level objects such as [`Blob`],
/// [`Tree`], [`Commit`], and [`Tag`] into a compact, versioned byte stream.
///
/// # Why this struct exists
///
/// Serialization is isolated behind a trait so that different storage backends
/// can use different wire formats. `BinaryEncoder` is the reference
/// implementation and defines the canonical on-disk format for the workspace.
///
/// # How it works
///
/// Each method writes to a [`std::io::Write`] implementation. The encoder does
/// not allocate the entire payload upfront; it streams fields directly to the
/// writer. However, all length conversions are checked with `try_from`, so
/// impossible lengths are reported as [`VctrlError::SerializationError`]
/// instead of causing silent truncation.
///
/// # Examples
///
/// ```
/// # use std::io::Cursor;
/// # use libvctrl_handler::{Blob, Encoder};
/// # use libvctrl_core::codec::BinaryEncoder;
/// let blob = Blob::new(b"hello".to_vec()).unwrap();
/// let mut buf = Vec::new();
/// BinaryEncoder.encode_blob(&blob, &mut buf).unwrap();
/// assert_eq!(buf[0], 3);
/// assert_eq!(buf.len(), 1 + 8 + 5);
/// ```
pub struct BinaryEncoder;

impl Encoder for BinaryEncoder {
    /// Encodes a [`Blob`] into the binary format.
    ///
    /// The output layout is:
    ///
    /// | Offset | Size       | Field               |
    /// |--------|------------|---------------------|
    /// | 0      | 1          | Version byte        |
    /// | 1      | 8          | `data_len` (u64 LE) |
    /// | 9      | `data_len` | Raw blob data       |
    ///
    /// # Errors
    ///
    /// Returns [`VctrlError::IoError`] if the writer fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io::Cursor;
    /// # use libvctrl_handler::{Blob, Encoder};
    /// # use libvctrl_core::codec::{BinaryEncoder, VERSION};
    /// let blob = Blob::new(b"hello world".to_vec()).unwrap();
    /// let mut encoded = Vec::new();
    /// BinaryEncoder.encode_blob(&blob, &mut encoded).unwrap();
    ///
    /// assert_eq!(encoded[0], VERSION);
    /// assert_eq!(encoded.len(), 1 + 8 + blob.data().len());
    /// ```
    fn encode_blob<W: Write + Send>(&self, blob: &Blob, writer: &mut W) -> Result<(), VctrlError> {
        let data = blob.data();
        writer.write_all(&[VERSION]).map_err(VctrlError::from_io)?;
        writer
            .write_all(&(data.len() as u64).to_le_bytes())
            .map_err(VctrlError::from_io)?;
        writer.write_all(data).map_err(VctrlError::from_io)?;
        Ok(())
    }

    /// Encodes a [`Tree`] into the binary format.
    ///
    /// The output layout is:
    ///
    /// | Offset | Size       | Field                                   |
    /// |--------|------------|------------------------------------------|
    /// | 0      | 1          | Version byte                             |
    /// | 1      | 4          | `entry_count` (u32 LE)                   |
    /// | 5      | varies     | Repeated entries, each consisting of:    |
    /// |        |            | - `name_len` (u8)                        |
    /// |        |            | - `name` (UTF-8)                         |
    /// |        |            | - `kind_byte` (u8)                       |
    /// |        |            | - `hash` (64 bytes)                      |
    ///
    /// # Errors
    ///
    /// Returns [`VctrlError::SerializationError`] if:
    ///
    /// - the tree contains more than `u32::MAX` entries,
    /// - an entry name is longer than `u8::MAX` bytes,
    /// - an entry kind is unknown.
    ///
    /// Returns [`VctrlError::IoError`] if the writer fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io::Cursor;
    /// # use libvctrl_handler::{Encoder, EntryKind, Hash, Tree, TreeEntry};
    /// # use libvctrl_core::codec::{BinaryEncoder, VERSION};
    /// let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
    /// let entry = TreeEntry::new("a.txt".to_owned(), EntryKind::Blob, hash).unwrap();
    /// let tree = Tree::new(vec![entry]).unwrap();
    ///
    /// let mut encoded = Vec::new();
    /// BinaryEncoder.encode_tree(&tree, &mut encoded).unwrap();
    ///
    /// assert_eq!(encoded[0], VERSION);
    /// let count = u32::from_le_bytes(encoded[1..5].try_into().unwrap());
    /// assert_eq!(count, 1);
    /// ```
    fn encode_tree<W: Write + Send>(&self, tree: &Tree, writer: &mut W) -> Result<(), VctrlError> {
        let entries = tree.entries();
        writer.write_all(&[VERSION]).map_err(VctrlError::from_io)?;
        let entry_count = u32::try_from(entries.len())
            .map_err(|e| VctrlError::SerializationError(format!("too many entries: {e}")))?;
        writer
            .write_all(&entry_count.to_le_bytes())
            .map_err(VctrlError::from_io)?;

        for entry in entries {
            let name = entry.name();
            let name_len = u8::try_from(name.len())
                .map_err(|e| VctrlError::SerializationError(format!("name too long: {e}")))?;
            writer.write_all(&[name_len]).map_err(VctrlError::from_io)?;
            writer
                .write_all(name.as_bytes())
                .map_err(VctrlError::from_io)?;

            let kind_byte = match entry.kind() {
                EntryKind::Blob => 0,
                EntryKind::Executable => 1,
                EntryKind::Symlink => 2,
                EntryKind::Tree => 3,
                EntryKind::Submodule => 4,
                _ => {
                    return Err(VctrlError::SerializationError("unknown entry kind".into()));
                }
            };
            writer
                .write_all(&[kind_byte])
                .map_err(VctrlError::from_io)?;
            writer
                .write_all(entry.hash().as_bytes())
                .map_err(VctrlError::from_io)?;
        }
        Ok(())
    }

    /// Encodes a [`Commit`] into the binary format.
    ///
    /// The output layout is fixed and ordered:
    ///
    /// | Field                 | Size          |
    /// |-----------------------|---------------|
    /// | Version               | 1             |
    /// | Tree hash             | 64            |
    /// | Parent count          | 2 (u16 LE)    |
    /// | Parent hashes         | 64 * count    |
    /// | Author name length    | 1             |
    /// | Author name           | length        |
    /// | Author email length   | 1             |
    /// | Author email          | length        |
    /// | Committer name length | 1             |
    /// | Committer name        | length        |
    /// | Committer email length| 1             |
    /// | Committer email       | length        |
    /// | Message length        | 4 (u32 LE)    |
    /// | Message               | length        |
    /// | Timestamp             | 8 (i64 LE)    |
    /// | Timezone offset       | 2 (i16 LE)    |
    /// | Encoding length       | 1             |
    /// | Encoding              | length or 0   |
    ///
    /// # Errors
    ///
    /// Returns [`VctrlError::SerializationError`] if:
    ///
    /// - the commit has more than `u16::MAX` parents,
    /// - any name or email is longer than `u8::MAX` bytes,
    /// - the message length cannot be represented as `u32`,
    /// - the message exceeds [`MAX_MESSAGE_LENGTH`],
    /// - the encoding string is longer than `u8::MAX` bytes.
    ///
    /// Returns [`VctrlError::IoError`] if the writer fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io::Cursor;
    /// # use libvctrl_handler::{Commit, Encoder, Hash, UserID};
    /// # use libvctrl_core::codec::{BinaryEncoder, VERSION};
    /// let tree = Hash::from_bytes(&[1u8; 64]).unwrap();
    /// let author = UserID::new("Alice".to_owned(), "alice@example.com".to_owned()).unwrap();
    /// let committer = UserID::new("Bob".to_owned(), "bob@example.com".to_owned()).unwrap();
    /// let commit = Commit::new(
    ///     tree,
    ///     vec![],
    ///     author,
    ///     committer,
    ///     "Initial commit".to_owned(),
    /// )
    /// .unwrap();
    ///
    /// let mut encoded = Vec::new();
    /// BinaryEncoder.encode_commit(&commit, &mut encoded).unwrap();
    ///
    /// assert_eq!(encoded[0], VERSION);
    /// assert!(encoded.len() > 1 + 64 + 2);
    /// ```
    fn encode_commit<W: Write + Send>(
        &self,
        commit: &Commit,
        writer: &mut W,
    ) -> Result<(), VctrlError> {
        writer.write_all(&[VERSION]).map_err(VctrlError::from_io)?;
        writer
            .write_all(commit.tree().as_bytes())
            .map_err(VctrlError::from_io)?;

        let parents = commit.parents();
        let parent_count = u16::try_from(parents.len())
            .map_err(|e| VctrlError::SerializationError(format!("too many parents: {e}")))?;
        writer
            .write_all(&parent_count.to_le_bytes())
            .map_err(VctrlError::from_io)?;

        for p in parents {
            writer
                .write_all(p.as_bytes())
                .map_err(VctrlError::from_io)?;
        }

        let author_name = commit.author().name();
        writer
            .write_all(&[u8::try_from(author_name.len()).map_err(|e| {
                VctrlError::SerializationError(format!("author name too long: {e}"))
            })?])
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(author_name.as_bytes())
            .map_err(VctrlError::from_io)?;

        let author_email = commit.author().email();
        writer
            .write_all(&[u8::try_from(author_email.len()).map_err(|e| {
                VctrlError::SerializationError(format!("author email too long: {e}"))
            })?])
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(author_email.as_bytes())
            .map_err(VctrlError::from_io)?;

        let committer_name = commit.committer().name();
        writer
            .write_all(&[u8::try_from(committer_name.len()).map_err(|e| {
                VctrlError::SerializationError(format!("committer name too long: {e}"))
            })?])
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(committer_name.as_bytes())
            .map_err(VctrlError::from_io)?;

        let committer_email = commit.committer().email();
        writer
            .write_all(&[u8::try_from(committer_email.len()).map_err(|e| {
                VctrlError::SerializationError(format!("committer email too long: {e}"))
            })?])
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(committer_email.as_bytes())
            .map_err(VctrlError::from_io)?;

        let msg = commit.message();
        let msg_len = u32::try_from(msg.len())
            .map_err(|e| VctrlError::SerializationError(format!("message too long: {e}")))?;
        if msg_len as usize > usize::try_from(MAX_MESSAGE_LENGTH).unwrap_or(usize::MAX) {
            return Err(VctrlError::SerializationError(
                "commit message exceeds size limit".into(),
            ));
        }
        writer
            .write_all(&msg_len.to_le_bytes())
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(msg.as_bytes())
            .map_err(VctrlError::from_io)?;

        writer
            .write_all(&commit.meta().timestamp().to_le_bytes())
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(&commit.meta().timezone_offset().to_le_bytes())
            .map_err(VctrlError::from_io)?;

        match commit.meta().encoding() {
            Some(enc) => {
                let len = u8::try_from(enc.len()).map_err(|e| {
                    VctrlError::SerializationError(format!("encoding too long: {e}"))
                })?;
                writer.write_all(&[len]).map_err(VctrlError::from_io)?;
                writer
                    .write_all(enc.as_bytes())
                    .map_err(VctrlError::from_io)?;
            }
            None => writer.write_all(&[0u8]).map_err(VctrlError::from_io)?,
        }
        Ok(())
    }

    /// Encodes a [`Tag`] into the binary format.
    ///
    /// The output layout is:
    ///
    /// | Field              | Size         |
    /// |--------------------|--------------|
    /// | Version            | 1            |
    /// | Name length        | 1            |
    /// | Name               | length       |
    /// | Target hash        | 64           |
    /// | Tagger presence    | 1            |
    /// | Tagger name length | 1 or omitted |
    /// | Tagger name        | length       |
    /// | Tagger email length| 1 or omitted |
    /// | Tagger email       | length       |
    /// | Message length     | 4 (u32 LE)   |
    /// | Message            | length       |
    /// | Timestamp          | 8 (i64 LE)   |
    /// | Timezone offset    | 2 (i16 LE)   |
    /// | Encoding length    | 1            |
    /// | Encoding           | length or 0  |
    ///
    /// # Errors
    ///
    /// Returns [`VctrlError::SerializationError`] if:
    ///
    /// - the tag name is longer than `u8::MAX` bytes,
    /// - a tagger name or email is longer than `u8::MAX` bytes,
    /// - the message cannot be represented as `u32`,
    /// - the message exceeds [`MAX_MESSAGE_LENGTH`],
    /// - the encoding string is longer than `u8::MAX` bytes.
    ///
    /// Returns [`VctrlError::IoError`] if the writer fails.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io::Cursor;
    /// # use libvctrl_handler::{Encoder, Hash, Tag, UserID};
    /// # use libvctrl_core::codec::{BinaryEncoder, VERSION};
    /// let target = Hash::from_bytes(&[2u8; 64]).unwrap();
    /// let tagger = UserID::new("Tagger".to_owned(), "tagger@example.com".to_owned()).unwrap();
    /// let tag = Tag::new(
    ///     "v1.0.0".to_owned(),
    ///     target,
    ///     Some(tagger),
    ///     "Release".to_owned(),
    /// )
    /// .unwrap();
    ///
    /// let mut encoded = Vec::new();
    /// BinaryEncoder.encode_tag(&tag, &mut encoded).unwrap();
    ///
    /// assert_eq!(encoded[0], VERSION);
    /// assert!(encoded.len() > 1 + 64 + 1);
    /// ```
    fn encode_tag<W: Write + Send>(&self, tag: &Tag, writer: &mut W) -> Result<(), VctrlError> {
        writer.write_all(&[VERSION]).map_err(VctrlError::from_io)?;

        let name = tag.name();
        let name_len = u8::try_from(name.len())
            .map_err(|e| VctrlError::SerializationError(format!("tag name too long: {e}")))?;
        writer.write_all(&[name_len]).map_err(VctrlError::from_io)?;
        writer
            .write_all(name.as_bytes())
            .map_err(VctrlError::from_io)?;

        writer
            .write_all(tag.target().as_bytes())
            .map_err(VctrlError::from_io)?;

        match tag.tagger() {
            Some(tagger) => {
                writer.write_all(&[1u8]).map_err(VctrlError::from_io)?;

                let tagger_name = tagger.name();
                writer
                    .write_all(&[u8::try_from(tagger_name.len()).map_err(|e| {
                        VctrlError::SerializationError(format!("tagger name too long: {e}"))
                    })?])
                    .map_err(VctrlError::from_io)?;
                writer
                    .write_all(tagger_name.as_bytes())
                    .map_err(VctrlError::from_io)?;

                let tagger_email = tagger.email();
                writer
                    .write_all(&[u8::try_from(tagger_email.len()).map_err(|e| {
                        VctrlError::SerializationError(format!("tagger email too long: {e}"))
                    })?])
                    .map_err(VctrlError::from_io)?;
                writer
                    .write_all(tagger_email.as_bytes())
                    .map_err(VctrlError::from_io)?;
            }
            None => writer.write_all(&[0u8]).map_err(VctrlError::from_io)?,
        }

        let msg = tag.message();
        let msg_len = u32::try_from(msg.len())
            .map_err(|e| VctrlError::SerializationError(format!("message too long: {e}")))?;
        if msg_len as usize > usize::try_from(MAX_MESSAGE_LENGTH).unwrap_or(usize::MAX) {
            return Err(VctrlError::SerializationError(
                "tag message exceeds size limit".into(),
            ));
        }
        writer
            .write_all(&msg_len.to_le_bytes())
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(msg.as_bytes())
            .map_err(VctrlError::from_io)?;

        writer
            .write_all(&tag.meta().timestamp().to_le_bytes())
            .map_err(VctrlError::from_io)?;
        writer
            .write_all(&tag.meta().timezone_offset().to_le_bytes())
            .map_err(VctrlError::from_io)?;

        match tag.meta().encoding() {
            Some(enc) => {
                let len = u8::try_from(enc.len()).map_err(|e| {
                    VctrlError::SerializationError(format!("encoding too long: {e}"))
                })?;
                writer.write_all(&[len]).map_err(VctrlError::from_io)?;
                writer
                    .write_all(enc.as_bytes())
                    .map_err(VctrlError::from_io)?;
            }
            None => writer.write_all(&[0u8]).map_err(VctrlError::from_io)?,
        }
        Ok(())
    }
}