1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-03 06:11:46 +02:00

Add StoreReference::render

This will be needed for the next step.

Also allows us to write round trip tests.
This commit is contained in:
John Ericson 2024-01-23 15:36:44 -05:00
parent c036d75f9e
commit b59a7a14c4
15 changed files with 233 additions and 3 deletions

View file

@ -4,6 +4,7 @@
#include "url.hh"
#include "store-reference.hh"
#include "file-system.hh"
#include "util.hh"
namespace nix {
@ -17,6 +18,29 @@ static bool isNonUriPath(const std::string & spec)
&& 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;

View file

@ -58,7 +58,7 @@ struct StoreReference
struct Specified
{
std::string scheme;
std::string authority;
std::string authority = "";
bool operator==(const Specified & rhs) const = default;
auto operator<=>(const Specified & rhs) const = default;
@ -73,6 +73,14 @@ struct StoreReference
bool operator==(const StoreReference & rhs) const = default;
auto operator<=>(const StoreReference & rhs) const = default;
/**
* Render the whole store reference as a URI, including parameters.
*/
std::string render() const;
/**
* Parse a URI into a store reference.
*/
static StoreReference parse(const std::string & uri, const Params & extraParams = Params{});
};