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
//! # In-Memory Reference Store
//!
//! This module provides [`MemoryRefStore`], a lightweight implementation of the
//! [`RefStore`](libvctrl_handler::RefStore) trait backed by a
//! [`std::collections::HashMap`].
//!
//! The store is intended for testing, prototyping, and scenarios where
//! persistence is not required. It stores references in memory only and loses
//! all data when dropped.
//!
//! ## Why this exists
//!
//! The [`RefStore`](libvctrl_handler::RefStore) trait defines the contract for
//! managing named references such as branches and tags. A concrete in-memory
//! implementation is essential for unit tests, examples, and as a reference
//! backend. It also demonstrates the expected behavior of the trait without
//! any disk or network dependencies.
//!
//! ## How it works
//!
//! References are stored in a private `HashMap<String, Hash>`. The `set_ref`
//! method validates the reference name using
//! [`validate_ref_name`](libvctrl_handler::validate_ref_name) before inserting.
//! The `list_refs` method collects and sorts all keys to provide deterministic
//! iteration order.
use ;
use HashMap;
/// An in-memory implementation of [`RefStore`].
///
/// `MemoryRefStore` stores named references such as branches and tags in a
/// `HashMap<String, Hash>`. It is suitable for ephemeral use cases and testing.
///
/// # Why this struct exists
///
/// The [`RefStore`] trait requires an implementation to be useful. This struct
/// provides a minimal, safe, and deterministic reference store that can be
/// embedded in applications or used as a baseline for tests.
///
/// # How it works
///
/// Internally, references are keyed by name and mapped to their target
/// [`Hash`]. The store validates names on insertion and returns errors when
/// lookups fail.
///
/// # Examples
///
/// ```
/// # use libvctrl_core::store::MemoryRefStore;
/// # use libvctrl_handler::{Hash, RefStore};
/// let mut store = MemoryRefStore::new();
/// let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
///
/// store.set_ref("refs/heads/main", &hash).unwrap();
/// assert_eq!(store.get_ref("refs/heads/main").unwrap(), hash);
/// ```