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
#pragma once
///@file

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

#include "lix/libutil/comparator.hh"
#include "lix/libutil/comparator.hh"
#include "lix/libutil/json-fwd.hh"
#include "lix/libutil/variant-wrapper.hh"

namespace nix {

/**
 * An (owned) output name. Just a type alias used to make code more
 * readible.
 */
typedef std::string OutputName;

/**
 * A borrowed output name. Just a type alias used to make code more
 * readible.
 */
typedef std::string_view OutputNameView;

struct OutputsSpec {
    /**
     * A non-empty set of outputs, specified by name
     */
    struct Names : std::set<OutputName> {
        using std::set<OutputName>::set;

        /* These need to be "inherited manually" */

        Names(const std::set<OutputName> & s)
            : std::set<OutputName>(s)
        { assert(!empty()); }

        /**
         * Needs to be "inherited manually"
         */
        Names(std::set<OutputName> && s)
            : std::set<OutputName>(s)
        { assert(!empty()); }

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

    /**
     * The set of all outputs, without needing to name them explicitly
     */
    struct All : std::monostate { };

    typedef std::variant<All, Names> Raw;

    Raw raw;

    GENERATE_CMP(OutputsSpec, me->raw);

    MAKE_WRAPPER_CONSTRUCTOR(OutputsSpec);

    /**
     * Force choosing a variant
     */
    OutputsSpec() = delete;

    bool contains(const OutputName & output) const;

    /**
     * Create a new OutputsSpec which is the union of this and that.
     */
    OutputsSpec union_(const OutputsSpec & that) const;

    /**
     * Whether this OutputsSpec is a subset of that.
     */
    bool isSubsetOf(const OutputsSpec & outputs) const;

    /**
     * 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;

    static void to_json(JSON & json, const OutputsSpec & t);
    static OutputsSpec from_json(const JSON & json);
};

struct ExtendedOutputsSpec {
    struct Default : std::monostate { };
    using Explicit = OutputsSpec;

    typedef std::variant<Default, Explicit> Raw;

    Raw raw;

    GENERATE_CMP(ExtendedOutputsSpec, me->raw);

    MAKE_WRAPPER_CONSTRUCTOR(ExtendedOutputsSpec);

    /**
     * Force choosing a variant
     */
    ExtendedOutputsSpec() = delete;

    /**
     * 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);
    static std::optional<std::pair<std::string_view, ExtendedOutputsSpec>> parseOpt(std::string_view s);

    std::string to_string() const;

    static void to_json(JSON & json, const ExtendedOutputsSpec & spec);
    static ExtendedOutputsSpec from_json(const JSON & t);
};

}