aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/outputs-spec.hh
blob: 9c477ee2b99acf1b07ec0af200c117b137688f40 (plain)
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
#pragma once

#include <optional>
#include <set>
#include <variant>

#include "nlohmann/json_fwd.hpp"

namespace nix {

struct OutputNames : std::set<std::string> {
    using std::set<std::string>::set;

    // These need to be "inherited manually"
    OutputNames(const std::set<std::string> & s)
        : std::set<std::string>(s)
    { }
    OutputNames(std::set<std::string> && s)
        : std::set<std::string>(s)
    { }

    /* This set should always be non-empty, so we delete this
       constructor in order make creating empty ones by mistake harder.
       */
    OutputNames() = delete;
};

struct AllOutputs {
    bool operator < (const AllOutputs & _) const { return false; }
};

typedef std::variant<AllOutputs, OutputNames> _OutputsSpecRaw;

struct OutputsSpec : _OutputsSpecRaw {
    using Raw = _OutputsSpecRaw;
    using Raw::Raw;

    using Names = OutputNames;
    using All = AllOutputs;

    inline const Raw & raw() const {
        return static_cast<const Raw &>(*this);
    }

    inline Raw & raw() {
        return static_cast<Raw &>(*this);
    }

    bool contains(const std::string & output) const;

    /* Modify the receiver outputs spec so it is the union of it's old value
       and the argument. Return whether the output spec needed to be modified
       --- if it didn't it was already "large enough". */
    bool merge(const OutputsSpec & outputs);

    /* Parse a string of the form 'output1,...outputN' or
       '*', returning the outputs spec. */
    static OutputsSpec parse(std::string_view s);
    static std::optional<OutputsSpec> parseOpt(std::string_view s);

    std::string to_string() const;
};

struct DefaultOutputs {
    bool operator < (const DefaultOutputs & _) const { return false; }
};

typedef std::variant<DefaultOutputs, OutputsSpec> _ExtendedOutputsSpecRaw;

struct ExtendedOutputsSpec : _ExtendedOutputsSpecRaw {
    using Raw = _ExtendedOutputsSpecRaw;
    using Raw::Raw;

    using Default = DefaultOutputs;
    using Explicit = OutputsSpec;

    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 extended outputs spec. */
    static std::pair<std::string_view, ExtendedOutputsSpec> parse(std::string_view s);

    std::string to_string() const;
};


void to_json(nlohmann::json &, const OutputsSpec &);
void from_json(const nlohmann::json &, OutputsSpec &);

void to_json(nlohmann::json &, const ExtendedOutputsSpec &);
void from_json(const nlohmann::json &, ExtendedOutputsSpec &);

}