-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdependency_selector.cppm
More file actions
222 lines (202 loc) · 7.78 KB
/
Copy pathdependency_selector.cppm
File metadata and controls
222 lines (202 loc) · 7.78 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
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
// mcpp.pm.dependency_selector — parse user dependency selectors into one
// canonical package coordinate.
export module mcpp.pm.dependency_selector;
import std;
import mcpp.pm.dep_spec;
export namespace mcpp::pm {
struct DependencySelector {
std::vector<DependencyCoordinate> candidates;
std::string stableMapKey;
// The spelling carried no namespace, so `candidates.front().namespace_` is
// the substituted default. Consumers that offer the one-release bare-name
// fallback need to tell "the author omitted it" from "the author wrote
// mcpplibs"; the coordinate alone cannot.
bool namespaceOmitted = false;
};
struct PackageSelector {
std::optional<std::string> namespace_;
std::string name;
std::string spelling;
};
struct SelectorError {
std::string message;
};
inline std::vector<std::string> split_dependency_selector(std::string_view selector)
{
std::vector<std::string> segments;
std::size_t start = 0;
for (std::size_t i = 0; i <= selector.size(); ++i) {
if (i == selector.size() || selector[i] == '.') {
segments.emplace_back(selector.substr(start, i - start));
start = i + 1;
}
}
return segments;
}
inline std::string join_dependency_segments(const std::vector<std::string>& segments,
std::size_t first,
std::size_t last)
{
std::string out;
for (std::size_t i = first; i < last && i < segments.size(); ++i) {
if (!out.empty()) out += ".";
out += segments[i];
}
return out;
}
inline DependencySelector make_direct_dependency_selector(
std::string_view ns,
std::string_view shortName,
std::string_view stableMapKey,
bool namespaceOmitted = false)
{
DependencySelector out;
out.stableMapKey = std::string(stableMapKey);
out.namespaceOmitted = namespaceOmitted;
out.candidates.push_back(DependencyCoordinate{
.namespace_ = std::string(ns),
.shortName = std::string(shortName),
});
return out;
}
inline std::expected<PackageSelector, SelectorError>
parse_package_selector(std::string_view spelling)
{
if (spelling.empty()) {
return std::unexpected(SelectorError{
.message = "package selector is empty",
});
}
auto segments = split_dependency_selector(spelling);
for (auto const& segment : segments) {
if (segment.empty()) {
return std::unexpected(SelectorError{
.message = std::format(
"invalid package selector '{}': empty dotted segment",
spelling),
});
}
for (unsigned char ch : segment) {
const bool bareKeyChar = (ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9') || ch == '_' || ch == '-';
if (!bareKeyChar) {
return std::unexpected(SelectorError{
.message = std::format(
"invalid package selector '{}': segment '{}' may only "
"contain ASCII letters, digits, '-' and '_'",
spelling, segment),
});
}
}
}
PackageSelector out{
.name = segments.back(),
.spelling = std::string(spelling),
};
if (segments.size() > 1) {
out.namespace_ = join_dependency_segments(
segments, 0, segments.size() - 1);
}
return out;
}
inline DependencyCoordinate normalize_package_selector(
const PackageSelector& selector,
std::string_view defaultNamespace = kDefaultNamespace)
{
return DependencyCoordinate{
.namespace_ = selector.namespace_.value_or(
std::string(defaultNamespace)),
.shortName = selector.name,
};
}
inline std::string
format_package_selector(const DependencyCoordinate& coordinate)
{
if (coordinate.namespace_.empty()
|| coordinate.namespace_ == kDefaultNamespace) {
return coordinate.shortName;
}
return std::format("{}.{}", coordinate.namespace_,
coordinate.shortName);
}
// The rungs a pre-exact-identity mcpp searched when the namespace was omitted:
// `gtest` reached `compat.gtest`, and a descriptor that declares no namespace
// at all was reachable by its bare name. Both are how every published
// `compat.*` package and every existing user manifest spell their dependency,
// so removing them outright turns an mcpp upgrade into a broken build for data
// that is already published and cannot be edited retroactively.
//
// These are NEVER part of the exact candidate list. They are consulted only
// after the exact coordinate has missed, the hit is announced with a
// deprecation warning naming the canonical selector, and what gets recorded
// downstream (spec, lockfile, install) is the canonical identity — so the
// ambiguous spelling exists in exactly one place, the user's manifest, and
// only until they run `mcpp add` or take the warning's advice.
//
// Removed in kBareNameFallbackRemovedIn.
inline std::vector<DependencyCoordinate>
legacy_bare_candidates(const DependencyCoordinate& exact)
{
if (exact.namespace_ != kDefaultNamespace) return {};
return {
DependencyCoordinate{
.namespace_ = std::string(kCompatNamespace),
.shortName = exact.shortName,
},
// The "upstream package that declares no namespace" rung. The caller
// must still reject a descriptor that DOES declare one, or this
// becomes the cross-namespace wildcard #278 removed.
DependencyCoordinate{
.namespace_ = {},
.shortName = exact.shortName,
},
};
}
// During the one-release exact-selector migration, this is the coordinate an
// older mcpp would have tried first for a compact dotted selector. It is
// diagnostic/lock-compatibility data only; normal resolution never appends it
// to the exact candidate list.
inline std::optional<DependencyCoordinate>
legacy_prefixed_coordinate(const DependencyCoordinate& exact)
{
if (exact.namespace_.empty()
|| exact.namespace_ == kDefaultNamespace
|| exact.namespace_.starts_with(
std::string(kDefaultNamespace) + ".")) {
return std::nullopt;
}
return DependencyCoordinate{
.namespace_ = std::format("{}.{}", kDefaultNamespace,
exact.namespace_),
.shortName = exact.shortName,
};
}
// #243 Cargo dep/feat forwarding. Split a `[features]` implied-feature token:
// a token containing '/' means "when this feature is active, request
// <depFeature> from dependency <depKey>" (Cargo `[features] F = ["dep/feat"]`).
// <depKey> is returned verbatim so it lands in the same keyspace as the
// `dependencies` / `featureDeps` maps (both keyed by the raw selector string,
// == stableMapKey). Split on the FIRST '/' (feature names contain no '/').
// A token with no '/', or with an empty dep/feature half, is a plain local
// implied feature → nullopt.
inline std::optional<std::pair<std::string, std::string>>
split_feature_forward_token(std::string_view token)
{
auto slash = token.find('/');
if (slash == std::string_view::npos) return std::nullopt;
auto depKey = token.substr(0, slash);
auto depFeat = token.substr(slash + 1);
if (depKey.empty() || depFeat.empty()) return std::nullopt;
return std::pair{std::string(depKey), std::string(depFeat)};
}
inline DependencySelector resolve_dependency_selector(std::string_view selector)
{
DependencySelector out;
out.stableMapKey = std::string(selector);
auto parsed = parse_package_selector(selector);
if (!parsed) return out;
out.candidates.push_back(normalize_package_selector(*parsed));
return out;
}
} // namespace mcpp::pm