mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 23:42:43 +01:00
For example, instead of doing
#include "nix/store-config.hh"
#include "nix/derived-path.hh"
Now do
#include "nix/store/config.hh"
#include "nix/store/derived-path.hh"
This was originally planned in the issue, and also recent requested by
Eelco.
Most of the change is purely mechanical. There is just one small
additional issue. See how, in the example above, we took this
opportunity to also turn `<comp>-config.hh` into `<comp>/config.hh`.
Well, there was already a `nix/util/config.{cc,hh}`. Even though there
is not a public configuration header for libutil (which also would be
called `nix/util/config.{cc,hh}`) that's still confusing, To avoid any
such confusion, we renamed that to `nix/util/configuration.{cc,hh}`.
Finally, note that the libflake headers already did this, so we didn't
need to do anything to them. We wouldn't want to mistakenly get
`nix/flake/flake/flake.hh`!
Progress on #7876
116 lines
3.1 KiB
C++
116 lines
3.1 KiB
C++
#include <regex>
|
|
|
|
#include "nix/util/error.hh"
|
|
#include "nix/util/url.hh"
|
|
#include "nix/store/store-reference.hh"
|
|
#include "nix/util/file-system.hh"
|
|
#include "nix/util/util.hh"
|
|
|
|
namespace nix {
|
|
|
|
static bool isNonUriPath(const std::string & spec)
|
|
{
|
|
return
|
|
// is not a URL
|
|
spec.find("://") == std::string::npos
|
|
// Has at least one path separator, and so isn't a single word that
|
|
// might be special like "auto"
|
|
&& spec.find("/") != std::string::npos;
|
|
}
|
|
|
|
std::string StoreReference::render() const
|
|
{
|
|
std::string res;
|
|
|
|
std::visit(
|
|
overloaded{
|
|
[&](const StoreReference::Auto &) { res = "auto"; },
|
|
[&](const StoreReference::Specified & g) {
|
|
res = g.scheme;
|
|
res += "://";
|
|
res += g.authority;
|
|
},
|
|
},
|
|
variant);
|
|
|
|
if (!params.empty()) {
|
|
res += "?";
|
|
res += encodeQuery(params);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
StoreReference StoreReference::parse(const std::string & uri, const StoreReference::Params & extraParams)
|
|
{
|
|
auto params = extraParams;
|
|
try {
|
|
auto parsedUri = parseURL(uri);
|
|
params.insert(parsedUri.query.begin(), parsedUri.query.end());
|
|
|
|
auto baseURI = parsedUri.authority.value_or("") + parsedUri.path;
|
|
|
|
return {
|
|
.variant =
|
|
Specified{
|
|
.scheme = std::move(parsedUri.scheme),
|
|
.authority = std::move(baseURI),
|
|
},
|
|
.params = std::move(params),
|
|
};
|
|
} catch (BadURL &) {
|
|
auto [baseURI, uriParams] = splitUriAndParams(uri);
|
|
params.insert(uriParams.begin(), uriParams.end());
|
|
|
|
if (baseURI == "" || baseURI == "auto") {
|
|
return {
|
|
.variant = Auto{},
|
|
.params = std::move(params),
|
|
};
|
|
} else if (baseURI == "daemon") {
|
|
return {
|
|
.variant =
|
|
Specified{
|
|
.scheme = "unix",
|
|
.authority = "",
|
|
},
|
|
.params = std::move(params),
|
|
};
|
|
} else if (baseURI == "local") {
|
|
return {
|
|
.variant =
|
|
Specified{
|
|
.scheme = "local",
|
|
.authority = "",
|
|
},
|
|
.params = std::move(params),
|
|
};
|
|
} else if (isNonUriPath(baseURI)) {
|
|
return {
|
|
.variant =
|
|
Specified{
|
|
.scheme = "local",
|
|
.authority = absPath(baseURI),
|
|
},
|
|
.params = std::move(params),
|
|
};
|
|
}
|
|
}
|
|
|
|
throw UsageError("Cannot parse Nix store '%s'", uri);
|
|
}
|
|
|
|
/* Split URI into protocol+hierarchy part and its parameter set. */
|
|
std::pair<std::string, StoreReference::Params> splitUriAndParams(const std::string & uri_)
|
|
{
|
|
auto uri(uri_);
|
|
StoreReference::Params params;
|
|
auto q = uri.find('?');
|
|
if (q != std::string::npos) {
|
|
params = decodeQuery(uri.substr(q + 1));
|
|
uri = uri_.substr(0, q);
|
|
}
|
|
return {uri, params};
|
|
}
|
|
|
|
}
|