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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! Fundamental data types that serve as the building blocks of a version control system.
//!
//! These types enforce their invariants at construction time.
//! Fields are private and can only be set through validated constructors.
//! Once created, an instance is **guaranteed** to be valid.
//!
//! # Design
//!
//! Every type is immutable after construction. If you need to change a value,
//! you must create a new instance (e.g., a new commit with different parents).
//! This immutability is a cornerstone of content‑addressable storage and
//! cryptographic integrity.
//!
//! All types implement `Debug`, `Clone`, and `PartialEq + Eq` for easy
//! comparison and display. Hashes also implement `Ord`, `Copy`, and `Hash`
//! so they can be used as keys in collections.
//!
//! # Validation at construction
//!
//! Constructors that accept strings (names, emails) validate their inputs:
//! - Names must not be empty.
//! - Names must not exceed [`MAX_NAME_LENGTH`](crate::constants::MAX_NAME_LENGTH).
//! - Emails must not be empty (for `UserID`).
//!
//! If validation fails, an [`VctrlError`](crate::VctrlError) is returned.
//! This ensures that invalid data never exists in your system.
//!
//! # Metadata
//!
//! [`CommitMeta`] bundles optional timestamp, timezone offset, and text encoding
//! for objects that carry these attributes ([`Commit`], [`Tag`]).
//!
//! # Examples
//!
//! ```rust
//! use libvctrl_handler::*;
//!
//! // Build a valid hash from known bytes.
//! let hash = Hash::from_bytes(&[0xAB; HASH_LENGTH]).unwrap();
//!
//! // Create a tree entry (a file named "README.md").
//! let entry = TreeEntry::new("README.md".into(), EntryKind::Blob, hash)
//! .expect("valid entry");
//!
//! // Build a tree containing that entry.
//! let tree = Tree::new(vec![entry]).expect("entries are sorted");
//!
//! // Create an author identity.
//! let alice = UserID::new("Alice".into(), "alice@example.com".into())
//! .expect("valid user");
//!
//! // Make a commit (no parents – initial commit) with default metadata.
//! let commit = Commit::new(
//! hash, // tree
//! vec![], // no parents
//! alice.clone(),// author
//! alice.clone(),// committer ← tambahkan .clone()
//! "Initial import".into(),
//! );
//!
//! // Make a commit with explicit metadata.
//! let meta = CommitMeta {
//! timestamp: 1672531200, // 2023-01-01T00:00:00 UTC
//! timezone_offset: 0,
//! encoding: Some("UTF-8".into()),
//! };
//! let commit2 = Commit::with_meta(
//! hash,
//! vec![],
//! alice.clone(),
//! alice.clone(),
//! "Another commit".into(),
//! meta,
//! );
//!
//! // Create an annotated tag.
//! let tag = Tag::new("v0.1.0".into(), hash, None, "First release".into())
//! .expect("valid tag name");
//!
//! assert_eq!(commit.message(), "Initial import");
//! assert_eq!(tag.name(), "v0.1.0");
//! ```
use crate;
use crateEntryKind;
use crateVctrlError;
use fmt;
// ---------------------------------------------------------------------------
// Helper for name validation
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Hash
// ---------------------------------------------------------------------------
/// A content hash – a fixed‑size array of 64 bytes (SHA‑512).
///
/// This is the fundamental identifier for all objects in the system.
/// A `Hash` is **always** 64 bytes; any attempt to create one with
/// a different length will fail with [`VctrlError::InvalidHashLength`].
///
/// # Construction
///
/// Use [`Hash::from_bytes`] to convert a byte slice. This function validates
/// the length and returns `Err` if it does not match [`HASH_LENGTH`].
///
/// ```rust
/// use libvctrl_handler::{Hash, HASH_LENGTH};
///
/// // Correct length → succeeds.
/// let h = Hash::from_bytes(&[0x00; HASH_LENGTH]).unwrap();
///
/// // Wrong length → fails.
/// assert!(Hash::from_bytes(&[0; 10]).is_err());
/// ```
///
/// # Display and Debug
///
/// - [`Display`] prints the full 64‑byte hex string (128 characters).
/// - [`Debug`] prints only the first 8 bytes followed by `…` for brevity.
///
/// ```rust
/// use libvctrl_handler::{Hash, HASH_LENGTH};
///
/// let h = Hash::from_bytes(&[0xAB; HASH_LENGTH]).unwrap();
///
/// // Display: "abababababababababab..."
/// // Debug: "Hash(abababababababab…)"
/// ```
;
// ---------------------------------------------------------------------------
// TreeEntry
// ---------------------------------------------------------------------------
/// A single entry inside a [`Tree`].
///
/// An entry is the basic building block of a directory listing. It pairs
/// a **name** with a **kind** (blob or subtree) and a **hash** that points to
/// the actual content.
///
/// # Validation
/// The name must be non‑empty and ≤ [`MAX_NAME_LENGTH`](crate::constants::MAX_NAME_LENGTH).
///
/// # Example
///
/// ```rust
/// use libvctrl_handler::{Hash, TreeEntry, EntryKind};
///
/// let hash = Hash::from_bytes(&[0x11; 64]).unwrap();
///
/// // A file entry.
/// let file = TreeEntry::new("src/main.rs".into(), EntryKind::Blob, hash)
/// .expect("valid entry");
/// assert_eq!(file.name(), "src/main.rs");
/// assert_eq!(file.kind(), EntryKind::Blob);
/// assert_eq!(file.hash().as_bytes().len(), 64);
///
/// // An empty name is rejected.
/// assert!(TreeEntry::new("".into(), EntryKind::Blob, hash).is_err());
/// ```
/// A blob object – raw, uninterpreted data.
///
/// Represents the contents of a file. No encoding, compression, or metadata
/// is stored – just the raw bytes.
///
/// # Empty blobs
/// An empty blob (`Blob::new(vec![])`) is perfectly valid and represents
/// an empty file.
///
/// # Size limits
/// There is **no** size limit enforced at the type level. A `Blob` can hold
/// any amount of data that fits in memory. However, decoders that process
/// untrusted input **should** respect [`MAX_BLOB_SIZE`](crate::constants::MAX_BLOB_SIZE)
/// to prevent memory‑exhaustion attacks. The reference decoder in
/// `libvctrl_core` enforces this limit.
///
/// # Example
///
/// ```rust
/// use libvctrl_handler::Blob;
///
/// let data = b"Hello, world!".to_vec();
/// let blob = Blob::new(data.clone());
/// assert_eq!(blob.data(), b"Hello, world!");
/// assert_eq!(blob.size(), 13);
/// assert!(!blob.is_empty());
///
/// // Empty blob
/// let empty = Blob::new(vec![]);
/// assert!(empty.is_empty());
/// assert_eq!(empty.size(), 0);
/// ```
// ---------------------------------------------------------------------------
// Tree
// ---------------------------------------------------------------------------
/// A tree object – a virtual directory listing.
///
/// A tree contains a **sorted** list of [`TreeEntry`] items. Entries are
/// ordered lexicographically by name, and duplicate names are forbidden.
/// These invariants are enforced at construction time.
///
/// # Errors
/// [`Tree::new`] will return an error if:
/// - Entries are not in sorted order.
/// - Two entries share the same name.
///
/// # Example
///
/// ```rust
/// use libvctrl_handler::{Hash, Tree, TreeEntry, EntryKind};
///
/// let hash = Hash::from_bytes(&[0x22; 64]).unwrap();
///
/// // Create sorted entries.
/// let file = TreeEntry::new("a.txt".into(), EntryKind::Blob, hash).unwrap();
/// let dir = TreeEntry::new("sub".into(), EntryKind::Tree, hash).unwrap();
///
/// // Build the tree – entries must be in order.
/// let tree = Tree::new(vec![file, dir]).expect("sorted entries");
/// assert_eq!(tree.entries().len(), 2);
///
/// // Duplicate names are rejected.
/// let dup1 = TreeEntry::new("x".into(), EntryKind::Blob, hash).unwrap();
/// let dup2 = TreeEntry::new("x".into(), EntryKind::Blob, hash).unwrap();
/// assert!(Tree::new(vec![dup1, dup2]).is_err());
/// ```
// ---------------------------------------------------------------------------
// UserID
// ---------------------------------------------------------------------------
/// Identity of a user (author or committer).
///
/// Contains a **name** and an **email**. Both are required to be non‑empty.
/// The name is also validated against [`MAX_NAME_LENGTH`].
///
/// # Example
///
/// ```rust
/// use libvctrl_handler::UserID;
///
/// let user = UserID::new("Alice".into(), "alice@example.com".into())
/// .expect("valid user");
/// assert_eq!(user.name(), "Alice");
/// assert_eq!(user.email(), "alice@example.com");
///
/// // Empty fields are rejected.
/// assert!(UserID::new("".into(), "x@y".into()).is_err());
/// assert!(UserID::new("Alice".into(), "".into()).is_err());
/// ```
// ---------------------------------------------------------------------------
// CommitMeta
// ---------------------------------------------------------------------------
/// Optional metadata for [`Commit`] and [`Tag`] objects.
///
/// Bundles timestamp, timezone offset, and text encoding so that constructors
/// can accept a single metadata argument instead of many individual parameters.
///
/// # Default
///
/// `CommitMeta::default()` returns `timestamp: 0`, `timezone_offset: 0`,
/// `encoding: None`. This is what `Commit::new` and `Tag::new` use internally.
///
/// # Example
///
/// ```rust
/// use libvctrl_handler::CommitMeta;
///
/// let meta = CommitMeta {
/// timestamp: 1672531200, // 2023-01-01T00:00:00 UTC
/// timezone_offset: 0,
/// encoding: Some("UTF-8".into()),
/// };
///
/// let default_meta = CommitMeta::default();
/// assert_eq!(default_meta.timestamp, 0);
/// assert!(default_meta.encoding.is_none());
/// ```
// ---------------------------------------------------------------------------
// Commit
// ---------------------------------------------------------------------------
/// A commit object – a snapshot of the repository at a point in time.
///
/// Records the root tree, parent commit(s), author, committer, a
/// human‑readable message, and optional metadata ([`CommitMeta`]).
///
/// # Construction
///
/// - [`Commit::new`] creates a commit with default metadata.
/// - [`Commit::with_meta`] accepts explicit [`CommitMeta`].
///
/// # Example (single‑parent commit)
///
/// ```rust
/// use libvctrl_handler::{Commit, Hash, UserID, HASH_LENGTH, CommitMeta};
///
/// let tree_hash = Hash::from_bytes(&[0x33; HASH_LENGTH]).unwrap();
/// let parent_hash = Hash::from_bytes(&[0x44; HASH_LENGTH]).unwrap();
/// let author = UserID::new("Bob".into(), "bob@example.com".into()).unwrap();
///
/// let commit = Commit::new(
/// tree_hash,
/// vec![parent_hash],
/// author.clone(),
/// author.clone(),
/// "Fix bug #42".into(),
/// );
///
/// // With metadata
/// let meta = CommitMeta {
/// timestamp: 1672531200,
/// timezone_offset: 0,
/// encoding: Some("UTF-8".into()),
/// };
/// let commit2 = Commit::with_meta(
/// tree_hash,
/// vec![parent_hash],
/// author.clone(),
/// author.clone(),
/// "Fix bug #42".into(),
/// meta,
/// );
/// ```
///
/// # Example (initial commit)
///
/// ```rust
/// # use libvctrl_handler::*;
/// let tree = Hash::from_bytes(&[0x55; 64]).unwrap();
/// let user = UserID::new("Alice".into(), "alice@e.com".into()).unwrap();
///
/// let initial = Commit::new(tree, vec![], user.clone(), user, "init".into());
/// assert!(initial.parents().is_empty());
/// ```
// ---------------------------------------------------------------------------
// Tag
// ---------------------------------------------------------------------------
/// A tag object – a named pointer to another object, usually a commit.
///
/// Tags can optionally include a **tagger** identity, a message,
/// and metadata ([`CommitMeta`]).
///
/// # Construction
///
/// - [`Tag::new`] creates a tag with default metadata.
/// - [`Tag::with_meta`] accepts explicit [`CommitMeta`].
///
/// # Example (annotated tag)
///
/// ```rust
/// use libvctrl_handler::{Hash, Tag, UserID, CommitMeta};
///
/// let commit_hash = Hash::from_bytes(&[0x66; 64]).unwrap();
/// let tagger = UserID::new("Release Bot".into(), "release@example.com".into()).unwrap();
///
/// let tag = Tag::new(
/// "v1.0.0".into(),
/// commit_hash,
/// Some(tagger.clone()),
/// "Stable release".into(),
/// ).expect("valid tag name");
///
/// // With metadata
/// let meta = CommitMeta {
/// timestamp: 1672531200,
/// timezone_offset: 0,
/// encoding: Some("UTF-8".into()),
/// };
/// let tag2 = Tag::with_meta(
/// "v1.0.1".into(),
/// commit_hash,
/// Some(tagger.clone()),
/// "Patch release".into(),
/// meta,
/// ).unwrap();
/// ```
///
/// # Example (lightweight tag)
///
/// ```rust
/// # use libvctrl_handler::*;
/// let hash = Hash::from_bytes(&[0x77; 64]).unwrap();
/// let tag = Tag::new("temp".into(), hash, None, "".into()).unwrap();
/// assert!(tag.tagger().is_none());
/// ```