aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/outputs-spec.cc
blob: 0d740c324b561fed9b1de30c047fb97e2630672d (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
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
#include <regex>
#include <nlohmann/json.hpp>

#include "regex-combinators.hh"
#include "outputs-spec.hh"
#include "path-regex.hh"
#include "strings.hh"

namespace nix {

bool OutputsSpec::contains(const std::string & outputName) const
{
    return std::visit(overloaded {
        [&](const OutputsSpec::All &) {
            return true;
        },
        [&](const OutputsSpec::Names & outputNames) {
            return outputNames.count(outputName) > 0;
        },
    }, raw);
}

static std::string outputSpecRegexStr =
    regex::either(
        regex::group(R"(\*)"),
        regex::group(regex::list(nameRegexStr)));

std::optional<OutputsSpec> OutputsSpec::parseOpt(std::string_view s)
{
    static std::regex regex(std::string { outputSpecRegexStr });

    std::smatch match;
    std::string s2 { s }; // until some improves std::regex
    if (!std::regex_match(s2, match, regex))
        return std::nullopt;

    if (match[1].matched)
        return { OutputsSpec::All {} };

    if (match[2].matched)
        return OutputsSpec::Names { tokenizeString<StringSet>(match[2].str(), ",") };

    assert(false);
}


OutputsSpec OutputsSpec::parse(std::string_view s)
{
    std::optional spec = parseOpt(s);
    if (!spec)
        throw Error("invalid outputs specifier '%s'", s);
    return std::move(*spec);
}


std::optional<std::pair<std::string_view, ExtendedOutputsSpec>> ExtendedOutputsSpec::parseOpt(std::string_view s)
{
    auto found = s.rfind('^');

    if (found == std::string::npos)
        return std::pair { s, ExtendedOutputsSpec::Default {} };

    auto specOpt = OutputsSpec::parseOpt(s.substr(found + 1));
    if (!specOpt)
        return std::nullopt;
    return std::pair { s.substr(0, found), ExtendedOutputsSpec::Explicit { std::move(*specOpt) } };
}


std::pair<std::string_view, ExtendedOutputsSpec> ExtendedOutputsSpec::parse(std::string_view s)
{
    std::optional spec = parseOpt(s);
    if (!spec)
        throw Error("invalid extended outputs specifier '%s'", s);
    return *spec;
}


std::string OutputsSpec::to_string() const
{
    return std::visit(overloaded {
        [&](const OutputsSpec::All &) -> std::string {
            return "*";
        },
        [&](const OutputsSpec::Names & outputNames) -> std::string {
            return concatStringsSep(",", outputNames);
        },
    }, raw);
}


std::string ExtendedOutputsSpec::to_string() const
{
    return std::visit(overloaded {
        [&](const ExtendedOutputsSpec::Default &) -> std::string {
            return "";
        },
        [&](const ExtendedOutputsSpec::Explicit & outputSpec) -> std::string {
            return "^" + outputSpec.to_string();
        },
    }, raw);
}


OutputsSpec OutputsSpec::union_(const OutputsSpec & that) const
{
    return std::visit(overloaded {
        [&](const OutputsSpec::All &) -> OutputsSpec {
            return OutputsSpec::All { };
        },
        [&](const OutputsSpec::Names & theseNames) -> OutputsSpec {
            return std::visit(overloaded {
                [&](const OutputsSpec::All &) -> OutputsSpec {
                    return OutputsSpec::All {};
                },
                [&](const OutputsSpec::Names & thoseNames) -> OutputsSpec {
                    OutputsSpec::Names ret = theseNames;
                    ret.insert(thoseNames.begin(), thoseNames.end());
                    return ret;
                },
            }, that.raw);
        },
    }, raw);
}


bool OutputsSpec::isSubsetOf(const OutputsSpec & that) const
{
    return std::visit(overloaded {
        [&](const OutputsSpec::All &) {
            return true;
        },
        [&](const OutputsSpec::Names & thoseNames) {
            return std::visit(overloaded {
                [&](const OutputsSpec::All &) {
                    return false;
                },
                [&](const OutputsSpec::Names & theseNames) {
                    bool ret = true;
                    for (auto & o : theseNames)
                        if (thoseNames.count(o) == 0)
                            ret = false;
                    return ret;
                },
            }, raw);
        },
    }, that.raw);
}

}

namespace nlohmann {

using namespace nix;

OutputsSpec adl_serializer<OutputsSpec>::from_json(const json & json) {
    auto names = json.get<StringSet>();
    if (names == StringSet({"*"}))
        return OutputsSpec::All {};
    else
        return OutputsSpec::Names { std::move(names) };
}

void adl_serializer<OutputsSpec>::to_json(json & json, OutputsSpec t) {
    std::visit(overloaded {
        [&](const OutputsSpec::All &) {
            json = std::vector<std::string>({"*"});
        },
        [&](const OutputsSpec::Names & names) {
            json = names;
        },
    }, t.raw);
}


ExtendedOutputsSpec adl_serializer<ExtendedOutputsSpec>::from_json(const json & json) {
    if (json.is_null())
        return ExtendedOutputsSpec::Default {};
    else {
        return ExtendedOutputsSpec::Explicit { json.get<OutputsSpec>() };
    }
}

void adl_serializer<ExtendedOutputsSpec>::to_json(json & json, ExtendedOutputsSpec t) {
    std::visit(overloaded {
        [&](const ExtendedOutputsSpec::Default &) {
            json = nullptr;
        },
        [&](const ExtendedOutputsSpec::Explicit & e) {
            adl_serializer<OutputsSpec>::to_json(json, e);
        },
    }, t.raw);
}

}