1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-17 07:52:43 +01:00

Improve OutputsSpec slightly

A few little changes preparing for the rest.
This commit is contained in:
John Ericson 2023-01-11 01:51:14 -05:00
parent a3ba80357d
commit a8f45b5e5a
7 changed files with 46 additions and 32 deletions

View file

@ -1,3 +1,4 @@
#include "util.hh"
#include "outputs-spec.hh"
#include "nlohmann/json.hpp"
@ -5,7 +6,7 @@
namespace nix {
std::pair<std::string, OutputsSpec> parseOutputsSpec(const std::string & s)
std::pair<std::string, OutputsSpec> OutputsSpec::parse(std::string s)
{
static std::regex regex(R"((.*)\^((\*)|([a-z]+(,[a-z]+)*)))");
@ -19,18 +20,19 @@ std::pair<std::string, OutputsSpec> parseOutputsSpec(const std::string & s)
return {match[1], tokenizeString<OutputNames>(match[4].str(), ",")};
}
std::string printOutputsSpec(const OutputsSpec & outputsSpec)
std::string OutputsSpec::to_string() const
{
if (std::get_if<DefaultOutputs>(&outputsSpec))
return "";
if (std::get_if<AllOutputs>(&outputsSpec))
return "^*";
if (auto outputNames = std::get_if<OutputNames>(&outputsSpec))
return "^" + concatStringsSep(",", *outputNames);
assert(false);
return std::visit(overloaded {
[&](const OutputsSpec::Default &) -> std::string {
return "";
},
[&](const OutputsSpec::All &) -> std::string {
return "*";
},
[&](const OutputsSpec::Names & outputNames) -> std::string {
return "^" + concatStringsSep(",", outputNames);
},
}, raw());
}
void to_json(nlohmann::json & json, const OutputsSpec & outputsSpec)

View file

@ -1,9 +1,8 @@
#pragma once
#include <set>
#include <variant>
#include "util.hh"
#include "nlohmann/json_fwd.hpp"
namespace nix {
@ -18,13 +17,26 @@ struct DefaultOutputs {
bool operator < (const DefaultOutputs & _) const { return false; }
};
typedef std::variant<DefaultOutputs, AllOutputs, OutputNames> OutputsSpec;
typedef std::variant<DefaultOutputs, AllOutputs, OutputNames> _OutputsSpecRaw;
/* Parse a string of the form 'prefix^output1,...outputN' or
'prefix^*', returning the prefix and the outputs spec. */
std::pair<std::string, OutputsSpec> parseOutputsSpec(const std::string & s);
struct OutputsSpec : _OutputsSpecRaw {
using Raw = _OutputsSpecRaw;
using Raw::Raw;
std::string printOutputsSpec(const OutputsSpec & outputsSpec);
using Names = OutputNames;
using All = AllOutputs;
using Default = DefaultOutputs;
inline const Raw & raw() const {
return static_cast<const Raw &>(*this);
}
/* Parse a string of the form 'prefix^output1,...outputN' or
'prefix^*', returning the prefix and the outputs spec. */
static std::pair<std::string, OutputsSpec> parse(std::string s);
std::string to_string() const;
};
void to_json(nlohmann::json &, const OutputsSpec &);
void from_json(const nlohmann::json &, OutputsSpec &);

View file

@ -2,7 +2,6 @@
#include "path.hh"
#include "derived-path.hh"
#include "nlohmann/json_fwd.hpp"
namespace nix {

View file

@ -4,40 +4,40 @@
namespace nix {
TEST(parseOutputsSpec, basic)
TEST(OutputsSpec_parse, basic)
{
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo");
ASSERT_EQ(prefix, "foo");
ASSERT_TRUE(std::get_if<DefaultOutputs>(&outputsSpec));
}
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo^*");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo^*");
ASSERT_EQ(prefix, "foo");
ASSERT_TRUE(std::get_if<AllOutputs>(&outputsSpec));
}
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo^out");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo^out");
ASSERT_EQ(prefix, "foo");
ASSERT_TRUE(std::get<OutputNames>(outputsSpec) == OutputNames({"out"}));
}
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo^out,bin");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo^out,bin");
ASSERT_EQ(prefix, "foo");
ASSERT_TRUE(std::get<OutputNames>(outputsSpec) == OutputNames({"out", "bin"}));
}
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo^bar^out,bin");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo^bar^out,bin");
ASSERT_EQ(prefix, "foo^bar");
ASSERT_TRUE(std::get<OutputNames>(outputsSpec) == OutputNames({"out", "bin"}));
}
{
auto [prefix, outputsSpec] = parseOutputsSpec("foo^&*()");
auto [prefix, outputsSpec] = OutputsSpec::parse("foo^&*()");
ASSERT_EQ(prefix, "foo^&*()");
ASSERT_TRUE(std::get_if<DefaultOutputs>(&outputsSpec));
}