-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlock_io.cppm
More file actions
111 lines (93 loc) · 3.97 KB
/
Copy pathlock_io.cppm
File metadata and controls
111 lines (93 loc) · 3.97 KB
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
// mcpp.pm.lock_io — read & write mcpp.lock (TOML).
//
// Part of the package-management subsystem refactor; see
// `.agents/docs/2026-05-08-pm-subsystem-architecture.md`.
// Body unchanged from the previous `mcpp.lockfile` module — only the
// namespace + module name moved. The old `mcpp.lockfile` module is
// kept as a thin shim that re-exports the same names so existing
// callers compile unchanged. A later PR will migrate call sites to
// `mcpp::pm::` directly and the shim will be removed.
export module mcpp.pm.lock_io;
import std;
import mcpp.libs.toml;
export namespace mcpp::pm {
struct LockedPackage {
std::string name;
std::string version;
std::string source; // e.g. "mcpp-index+https://..." (M2 placeholder)
std::string hash; // "sha256:..." or "fnv1a:..."
};
struct Lockfile {
int schemaVersion = 1;
std::vector<LockedPackage> packages;
};
struct LockError {
std::string message;
};
std::expected<Lockfile, LockError> load(const std::filesystem::path& path);
std::expected<void, LockError> write(const Lockfile& lock, const std::filesystem::path& path);
std::string serialize(const Lockfile& lock);
std::string compute_hash(const Lockfile& lock);
} // namespace mcpp::pm
namespace mcpp::pm {
namespace t = mcpp::libs::toml;
std::expected<Lockfile, LockError> load(const std::filesystem::path& path) {
if (!std::filesystem::exists(path)) {
return Lockfile{}; // no lock yet
}
auto doc = t::parse_file(path);
if (!doc) return std::unexpected(LockError{
std::format("{}:{}:{}: {}", path.string(), doc.error().where.line,
doc.error().where.column, doc.error().message)});
Lockfile lock;
if (auto v = doc->get_int("version")) lock.schemaVersion = static_cast<int>(*v);
// [[package]] arrays are not in our minimal parser. We use [package.<name>] instead.
// Or just iterate the root looking for top-level "package" table that contains a list.
// For simplicity in M2: accept either format with top-level array of tables described
// as [package.X] sections.
auto* pkgs = doc->get_table("package");
if (pkgs) {
for (auto& [k, v] : *pkgs) {
if (!v.is_table()) continue;
auto& tt = v.as_table();
LockedPackage lp;
lp.name = k;
if (auto it = tt.find("version"); it != tt.end() && it->second.is_string()) lp.version = it->second.as_string();
if (auto it = tt.find("source"); it != tt.end() && it->second.is_string()) lp.source = it->second.as_string();
if (auto it = tt.find("hash"); it != tt.end() && it->second.is_string()) lp.hash = it->second.as_string();
lock.packages.push_back(std::move(lp));
}
}
return lock;
}
std::string serialize(const Lockfile& lock) {
std::string out;
out += "# Auto-generated by mcpp. Do not edit by hand.\n";
out += std::format("version = {}\n\n", lock.schemaVersion);
for (auto& p : lock.packages) {
out += std::format("[package.\"{}\"]\n", p.name);
out += std::format("version = {}\n", t::escape_string(p.version));
out += std::format("source = {}\n", t::escape_string(p.source));
out += std::format("hash = {}\n\n", t::escape_string(p.hash));
}
return out;
}
std::expected<void, LockError> write(const Lockfile& lock, const std::filesystem::path& path) {
std::error_code ec;
std::filesystem::create_directories(path.parent_path(), ec);
std::ofstream os(path);
if (!os) return std::unexpected(LockError{std::format("cannot write '{}'", path.string())});
os << serialize(lock);
return {};
}
std::string compute_hash(const Lockfile& lock) {
// FNV-1a over the canonical serialized form.
auto s = serialize(lock);
std::uint64_t h = 0xcbf29ce484222325ull;
for (unsigned char c : s) {
h ^= c;
h *= 0x100000001b3ull;
}
return std::format("{:016x}", h);
}
} // namespace mcpp::pm