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
//! In‑memory reference store – the simplest [`RefStore`] implementation.
//!
//! [`MemoryRefStore`] implements [`RefStore`] using a `HashMap<String, Hash>`.
//! It validates reference names before storing them, as required by the
//! trait contract.
//!
//! # Design
//!
//! References are mutable named pointers to objects. They are used to
//! track branches, tags, and special refs like `HEAD`. This store
//! implements the full [`RefStore`] trait with a simple `HashMap`.
//!
//! # Name validation
//!
//! Names are validated on every call to [`set_ref`](MemoryRefStore::set_ref):
//! - Must not be empty.
//! - Must not exceed [`MAX_NAME_LENGTH`](libvctrl_handler::MAX_NAME_LENGTH).
//! - No additional character restrictions are applied (this is a
//! reference implementation; production code may add path traversal
//! checks or other policies).
//!
//! # Performance
//!
//! - **`set_ref`**: O(1) average.
//! - **`get_ref`**: O(1) average.
//! - **`delete_ref`**: O(1) average.
//! - **`list_ref`s**: O(n) where n is the number of references.
//!
//! # Examples
//!
//! ```rust
//! use libvctrl_core::store::MemoryRefStore;
//! use libvctrl_handler::{Hash, RefStore, HASH_LENGTH};
//!
//! let mut refs = MemoryRefStore::new();
//! let hash = Hash::from_bytes(&[0u8; HASH_LENGTH]).unwrap();
//!
//! // Set a reference
//! refs.set_ref("refs/heads/main", &hash).unwrap();
//!
//! // Look up a reference
//! assert_eq!(refs.get_ref("refs/heads/main").unwrap(), hash);
//!
//! // List all references
//! let all = refs.list_refs().unwrap();
//! assert!(all.contains(&"refs/heads/main".to_string()));
//!
//! // Delete a reference
//! refs.delete_ref("refs/heads/main").unwrap();
//! assert!(refs.get_ref("refs/heads/main").is_err());
//! ```
use ;
use HashMap;
/// An in‑memory reference store.
///
/// Validates names before storing them, as required by the trait contract.
///
/// # Characteristics
/// - **Not thread‑safe**: wrap in `Arc<Mutex<…>>` for shared access.
/// - **Not persistent**: all data is lost when the store is dropped.
///
/// # Examples
/// ```
/// use libvctrl_core::store::MemoryRefStore;
/// use libvctrl_handler::{Hash, RefStore, HASH_LENGTH};
///
/// let mut refs = MemoryRefStore::new();
/// let hash = Hash::from_bytes(&[0u8; HASH_LENGTH]).unwrap();
/// refs.set_ref("HEAD", &hash).unwrap();
/// assert_eq!(refs.get_ref("HEAD").unwrap(), hash);
/// ```