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

#include <optional>

namespace nix {

NixStringContextElem NixStringContextElem::parse(const Store & store, 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 = store.parseStorePath(s.substr(index + 1)),
            .output = std::string(s.substr(0, index)),
        };
    }
    case '=': {
        return NixStringContextElem::DrvDeep {
            .drvPath = store.parseStorePath(s.substr(1)),
        };
    }
    default: {
        return NixStringContextElem::Opaque {
            .path = store.parseStorePath(s),
        };
    }
    }
}

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

}