summaryrefslogtreecommitdiff
path: root/src/buildtool/file_system/precomputed_root.hpp
blob: a405d680e9cefac4388429c64416b032ad62497e (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
// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_PRECOMPUTED_ROOT_HPP
#define INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_PRECOMPUTED_ROOT_HPP

#include <cstddef>
#include <exception>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include <variant>

#include "nlohmann/json.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/utils/cpp/expected.hpp"

struct ComputedRoot final {
    static constexpr auto kMarker = "computed";
    static constexpr std::size_t kSchemeLength = 5;

    std::string repository;
    std::string target_module;
    std::string target_name;
    nlohmann::json config;
    bool absent;

    [[nodiscard]] auto operator==(ComputedRoot const& other) const noexcept
        -> bool;
    [[nodiscard]] auto operator<(ComputedRoot const& other) const noexcept
        -> bool;
    [[nodiscard]] auto ToString() const -> std::string;
    [[nodiscard]] auto ComputeHash() const -> std::size_t;
};

struct TreeStructureRoot final {
    static constexpr auto kMarker = "tree structure";
    static constexpr std::size_t kSchemeLength = 2;
    static constexpr std::size_t kSchemePragmaLength = 3;

    std::string repository;
    bool absent;

    [[nodiscard]] auto operator==(TreeStructureRoot const& other) const noexcept
        -> bool;

    [[nodiscard]] auto operator<(TreeStructureRoot const& other) const noexcept
        -> bool;

    [[nodiscard]] auto ToString() const -> std::string;
    [[nodiscard]] auto ComputeHash() const -> std::size_t;
};

namespace std {
template <typename>
struct hash;
}

/// \brief Generalized representation of roots that must be evaluated before the
/// real build starts.
class PrecomputedRoot final {
  public:
    using root_t = std::variant<ComputedRoot, TreeStructureRoot>;
    explicit PrecomputedRoot() : PrecomputedRoot(ComputedRoot{}) {}
    explicit PrecomputedRoot(root_t root)
        : root_{std::move(root)}, hash_{ComputeHash(root_)} {}

    [[nodiscard]] static auto Parse(nlohmann::json const& root) noexcept
        -> expected<PrecomputedRoot, std::string>;

    [[nodiscard]] static auto IsPrecomputedMarker(
        std::string const& marker) noexcept -> bool {
        return marker == ComputedRoot::kMarker or
               marker == TreeStructureRoot::kMarker;
    }

    [[nodiscard]] auto operator==(PrecomputedRoot const& other) const noexcept
        -> bool {
        try {
            return root_ == other.root_;
        } catch (std::exception const& e) {
            try {
                Logger::Log(
                    LogLevel::Error, "Unexpected excpetion: {}", e.what());
                std::terminate();
            } catch (...) {
                std::terminate();
            }
        } catch (...) {
            std::terminate();
        }
    }
    [[nodiscard]] auto operator<(PrecomputedRoot const& other) const noexcept
        -> bool {
        try {
            return root_ < other.root_;
        } catch (std::exception const& e) {
            try {
                Logger::Log(
                    LogLevel::Error, "Unexpected excpetion: {}", e.what());
                std::terminate();
            } catch (...) {
                std::terminate();
            }
        } catch (...) {
            std::terminate();
        }
    }

    [[nodiscard]] auto ToString() const noexcept -> std::string;
    [[nodiscard]] auto GetReferencedRepository() const noexcept -> std::string;

    [[nodiscard]] auto IsComputed() const noexcept -> bool {
        return std::holds_alternative<ComputedRoot>(root_);
    }
    [[nodiscard]] auto AsComputed() const -> std::optional<ComputedRoot> {
        if (auto const* computed = std::get_if<ComputedRoot>(&root_)) {
            return *computed;
        }
        return std::nullopt;
    }

    [[nodiscard]] auto IsTreeStructure() const noexcept -> bool {
        return std::holds_alternative<TreeStructureRoot>(root_);
    }
    [[nodiscard]] auto AsTreeStructure() const noexcept
        -> std::optional<TreeStructureRoot> {
        if (auto const* root = std::get_if<TreeStructureRoot>(&root_)) {
            return *root;
        }
        return std::nullopt;
    }

  private:
    root_t root_;
    std::size_t hash_;

    [[nodiscard]] static auto ComputeHash(root_t const& root) -> std::size_t;

    friend struct std::hash<PrecomputedRoot>;
};

namespace std {
template <>
struct hash<PrecomputedRoot> {
    [[nodiscard]] auto operator()(PrecomputedRoot const& root) const
        -> std::size_t {
        return root.hash_;
    }
};
}  // namespace std

#endif  // INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_PRECOMPUTED_ROOT_HPP