98 lines
3.7 KiB
C++
98 lines
3.7 KiB
C++
#pragma once
|
|
|
|
#include <Void/Build.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace Void::Build::Package {
|
|
|
|
struct SourceOrigin {
|
|
std::string file;
|
|
std::size_t line{0};
|
|
std::size_t column{0};
|
|
|
|
[[nodiscard]] std::string format() const;
|
|
};
|
|
|
|
struct TomlValue;
|
|
using TomlArray = std::vector<TomlValue>;
|
|
using TomlTable = std::map<std::string, TomlValue, std::less<>>;
|
|
|
|
// TOML 1.0 value with source origin metadata (VL-002 adapter; swappable when Void::Data::Toml
|
|
// lands).
|
|
struct TomlValue {
|
|
using Storage = std::variant<std::string, bool, std::int64_t, double,
|
|
std::shared_ptr<TomlArray>, std::shared_ptr<TomlTable>>;
|
|
Storage value;
|
|
SourceOrigin origin;
|
|
|
|
[[nodiscard]] bool is_string() const noexcept {
|
|
return std::holds_alternative<std::string>(value);
|
|
}
|
|
[[nodiscard]] bool is_bool() const noexcept { return std::holds_alternative<bool>(value); }
|
|
[[nodiscard]] bool is_int() const noexcept {
|
|
return std::holds_alternative<std::int64_t>(value);
|
|
}
|
|
[[nodiscard]] bool is_double() const noexcept { return std::holds_alternative<double>(value); }
|
|
[[nodiscard]] bool is_array() const noexcept {
|
|
return std::holds_alternative<std::shared_ptr<TomlArray>>(value);
|
|
}
|
|
[[nodiscard]] bool is_table() const noexcept {
|
|
return std::holds_alternative<std::shared_ptr<TomlTable>>(value);
|
|
}
|
|
|
|
[[nodiscard]] const std::string &as_string() const { return std::get<std::string>(value); }
|
|
[[nodiscard]] bool as_bool() const { return std::get<bool>(value); }
|
|
[[nodiscard]] std::int64_t as_int() const { return std::get<std::int64_t>(value); }
|
|
[[nodiscard]] double as_double() const { return std::get<double>(value); }
|
|
[[nodiscard]] const TomlArray &as_array() const {
|
|
return *std::get<std::shared_ptr<TomlArray>>(value);
|
|
}
|
|
[[nodiscard]] const TomlTable &as_table() const {
|
|
return *std::get<std::shared_ptr<TomlTable>>(value);
|
|
}
|
|
[[nodiscard]] TomlTable &as_table_mut() { return *std::get<std::shared_ptr<TomlTable>>(value); }
|
|
[[nodiscard]] TomlArray &as_array_mut() { return *std::get<std::shared_ptr<TomlArray>>(value); }
|
|
|
|
static TomlValue make_table(SourceOrigin origin = {});
|
|
static TomlValue make_array(SourceOrigin origin = {});
|
|
};
|
|
|
|
// Parses TOML 1.0 text with exact file:line:col diagnostics and duplicate-key rejection.
|
|
[[nodiscard]] Void::Result::Result<TomlTable, Void::Result::Error>
|
|
parse_toml(std::string_view text, std::string_view source_name = "<memory>");
|
|
|
|
[[nodiscard]] Void::Result::Result<TomlTable, Void::Result::Error>
|
|
parse_toml_file(const std::filesystem::path &path);
|
|
|
|
// Canonical deterministic TOML 1.0 serializer.
|
|
[[nodiscard]] std::string serialize_toml(const TomlTable &table);
|
|
|
|
struct NormalizedDependency {
|
|
DependencyRecord record;
|
|
SourceOrigin origin;
|
|
};
|
|
|
|
// Loads [dependencies.<alias>] tables from a TOML manifest into the exact same
|
|
// DependencyRecord model used by C++ project.dependency(...) declarations (Plan 11).
|
|
[[nodiscard]] Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
|
load_dependencies_toml(const std::filesystem::path &toml_path);
|
|
|
|
// Merges C++ inline declarations and TOML-loaded declarations. Rejects contradictory
|
|
// duplicate aliases/sources/options with both origins clearly diagnosed.
|
|
[[nodiscard]] Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
|
merge_dependencies(const std::vector<DependencyRecord> &cpp_deps,
|
|
const std::vector<NormalizedDependency> &toml_deps);
|
|
|
|
} // namespace Void::Build::Package
|