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
//! Directory tree representation for version control systems.
//!
//! A [`Tree`] is an ordered collection of [`TreeEntry`] items, each mapping a
//! file or sub‑directory name to its hash and kind. Trees are the backbone of
//! the repository object model, connecting blobs and subtrees into a single
//! snapshot.
use crateEntryKind;
use crateVctrlError;
use crateHash;
use validate_name;
/// A single entry in a directory tree.
///
/// Each entry associates a `name` with a `hash` of the object it points to
/// and a `kind` indicating whether the object is a blob, tree, or other.
/// Entries are immutable once constructed.
///
/// # Design
///
/// Fields are private to enforce consistency: the name is validated at
/// construction time, and the hash and kind cannot be changed afterwards.
/// The struct is [`Clone`], [`Debug`], [`PartialEq`], and [`Eq`] so that
/// trees can be compared and duplicated efficiently.
///
/// # Examples
///
/// Creating a blob entry:
///
/// ```
/// # use libvctrl_handler::{EntryKind, Hash, TreeEntry};
/// # fn make_hash() -> Hash {
/// # let bytes = [0xABu8; 64];
/// # Hash::from_bytes(&bytes).unwrap()
/// # }
/// let hash = make_hash();
/// let entry = TreeEntry::new("README.md".into(), EntryKind::Blob, hash).unwrap();
///
/// assert_eq!(entry.name(), "README.md");
/// assert_eq!(entry.kind(), EntryKind::Blob);
/// ```
///
/// Attempting to create an entry with an empty name fails:
///
/// ```
/// # use libvctrl_handler::{EntryKind, Hash, TreeEntry};
/// # fn make_hash() -> Hash {
/// # let bytes = [0x00u8; 64];
/// # Hash::from_bytes(&bytes).unwrap()
/// # }
/// let hash = make_hash();
/// assert!(TreeEntry::new("".into(), EntryKind::Blob, hash).is_err());
/// ```
/// An ordered collection of directory entries.
///
/// A `Tree` represents the contents of a single directory in the repository.
/// Entries are stored in a sorted order and duplicates are not allowed. The
/// sorting must be in ascending byte‑lexicographic order of the entry
/// names, as is conventional in many version control systems. This ordering
/// ensures deterministic tree hashes.
///
/// # Design
///
/// The entries are owned by the tree and cannot be modified after
/// construction. The constructor [`Tree::new`] validates that the provided
/// entries are strictly increasing in name order and that no name exceeds
/// the maximum length. If validation fails, an [`VctrlError::InvalidName`]
/// is returned.
///
/// # Examples
///
/// Building a simple tree with two entries:
///
/// ```
/// # use libvctrl_handler::{EntryKind, Hash, Tree, TreeEntry};
/// # fn make_hash() -> Hash {
/// # let bytes = [0xCCu8; 64];
/// # Hash::from_bytes(&bytes).unwrap()
/// # }
/// let hash = make_hash();
/// let entry1 = TreeEntry::new("file.txt".into(), EntryKind::Blob, hash).unwrap();
/// let entry2 = TreeEntry::new("subdir".into(), EntryKind::Tree, hash).unwrap();
/// let tree = Tree::new(vec![entry1, entry2]).unwrap();
///
/// assert_eq!(tree.entries().len(), 2);
/// assert_eq!(tree.entries()[0].name(), "file.txt");
/// assert_eq!(tree.entries()[1].name(), "subdir");
/// ```
///
/// Attempting to create a tree with unsorted or duplicate entries fails:
///
/// ```
/// # use libvctrl_handler::{EntryKind, Hash, Tree, TreeEntry};
/// # fn make_hash() -> Hash {
/// # let bytes = [0xDDu8; 64];
/// # Hash::from_bytes(&bytes).unwrap()
/// # }
/// let hash = make_hash();
/// let entry1 = TreeEntry::new("b".into(), EntryKind::Blob, hash).unwrap();
/// let entry2 = TreeEntry::new("a".into(), EntryKind::Blob, hash).unwrap();
/// assert!(Tree::new(vec![entry1, entry2]).is_err()); // "b" > "a"
/// ```