aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/value/context.cc
blob: f76fc76e48d2efa884eb5e32d108a07ef496b42b (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
#include "value/context.hh"

#include <optional>

namespace nix {

NixStringContextElem NixStringContextElem::parse(std::string_view s0)
{
    std::string_view s = s0;

    if (s.size() == 0) {
        throw BadNixStringContextElem(s0,
            "String context element should never be an empty string");
    }
    switch (s.at(0)) {
    case '!': {
        s = s.substr(1); // advance string to parse after first !
        size_t index = s.find("!");
        // This makes index + 1 safe. Index can be the length (one after index
        // of last character), so given any valid character index --- a
        // successful find --- we can add one.
        if (index == std::string_view::npos) {
            throw BadNixStringContextElem(s0,
                "String content element beginning with '!' should have a second '!'");
        }
        return NixStringContextElem::Built {
            .drvPath = StorePath { s.substr(index + 1) },
            .output = std::string(s.substr(0, index)),
        };
    }
    case '=': {
        return NixStringContextElem::DrvDeep {
            .drvPath = StorePath { s.substr(1) },
        };
    }
    default: {
        return NixStringContextElem::Opaque {
            .path = StorePath { s },
        };
    }
    }
}

std::string NixStringContextElem::to_string() const {
    return std::visit(overloaded {
        [&](const NixStringContextElem::Built & b) {
            std::string res;
            res += '!';
            res += b.output;
            res += '!';
            res += b.drvPath.to_string();
            return res;
        },
        [&](const NixStringContextElem::DrvDeep & d) {
            std::string res;
            res += '=';
            res += d.drvPath.to_string();
            return res;
        },
        [&](const NixStringContextElem::Opaque & o) {
            return std::string { o.path.to_string() };
        },
    }, raw());
}

}