diff options
Diffstat (limited to 'toolsrc/include')
| -rw-r--r-- | toolsrc/include/LineInfo.h | 2 | ||||
| -rw-r--r-- | toolsrc/include/PackageSpec.h | 6 | ||||
| -rw-r--r-- | toolsrc/include/PackageSpecParseResult.h | 31 | ||||
| -rw-r--r-- | toolsrc/include/Paragraphs.h | 3 | ||||
| -rw-r--r-- | toolsrc/include/SourceParagraph.h | 17 | ||||
| -rw-r--r-- | toolsrc/include/vcpkg_expected.h | 79 |
6 files changed, 97 insertions, 41 deletions
diff --git a/toolsrc/include/LineInfo.h b/toolsrc/include/LineInfo.h index 66d91c520..62973462a 100644 --- a/toolsrc/include/LineInfo.h +++ b/toolsrc/include/LineInfo.h @@ -1,5 +1,7 @@ #pragma once +#include <string> + namespace vcpkg { struct LineInfo diff --git a/toolsrc/include/PackageSpec.h b/toolsrc/include/PackageSpec.h index 050d9d079..58edb8274 100644 --- a/toolsrc/include/PackageSpec.h +++ b/toolsrc/include/PackageSpec.h @@ -7,9 +7,11 @@ namespace vcpkg { struct PackageSpec { - static Expected<PackageSpec> from_string(const std::string& spec_as_string, const Triplet& default_triplet); + static ExpectedT<PackageSpec, PackageSpecParseResult> from_string(const std::string& spec_as_string, + const Triplet& default_triplet); static std::string to_string(const std::string& name, const Triplet& triplet); - static Expected<PackageSpec> from_name_and_triplet(const std::string& name, const Triplet& triplet); + static ExpectedT<PackageSpec, PackageSpecParseResult> from_name_and_triplet(const std::string& name, + const Triplet& triplet); const std::string& name() const; diff --git a/toolsrc/include/PackageSpecParseResult.h b/toolsrc/include/PackageSpecParseResult.h index b72c534c2..17d151626 100644 --- a/toolsrc/include/PackageSpecParseResult.h +++ b/toolsrc/include/PackageSpecParseResult.h @@ -1,5 +1,6 @@ #pragma once -#include <system_error> + +#include "vcpkg_expected.h" namespace vcpkg { @@ -10,27 +11,21 @@ namespace vcpkg INVALID_CHARACTERS }; - struct PackageSpecParseResultCategoryImpl final : std::error_category - { - virtual const char* name() const noexcept override; + CStringView to_string(PackageSpecParseResult ev) noexcept; - virtual std::string message(int ev) const noexcept override; - }; - - const std::error_category& package_spec_parse_result_category(); + template<> + struct ErrorHolder<PackageSpecParseResult> + { + ErrorHolder() : m_err(PackageSpecParseResult::SUCCESS) {} + ErrorHolder(PackageSpecParseResult err) : m_err(err) {} - std::error_code make_error_code(PackageSpecParseResult e); + constexpr bool has_error() const { return m_err != PackageSpecParseResult::SUCCESS; } - PackageSpecParseResult to_package_spec_parse_result(int i); + PackageSpecParseResult error() const { return m_err; } - PackageSpecParseResult to_package_spec_parse_result(std::error_code ec); -} + CStringView to_string() const { return vcpkg::to_string(m_err); } -// Enable implicit conversion to std::error_code -namespace std -{ - template<> - struct is_error_code_enum<vcpkg::PackageSpecParseResult> : ::std::true_type - { + private: + PackageSpecParseResult m_err; }; } diff --git a/toolsrc/include/Paragraphs.h b/toolsrc/include/Paragraphs.h index 66f6bd2e4..59f0eefc8 100644 --- a/toolsrc/include/Paragraphs.h +++ b/toolsrc/include/Paragraphs.h @@ -16,7 +16,8 @@ namespace vcpkg::Paragraphs Expected<ParagraphDataMap> parse_single_paragraph(const std::string& str); Expected<std::vector<ParagraphDataMap>> parse_paragraphs(const std::string& str); - Expected<SourceParagraph> try_load_port(const Files::Filesystem& fs, const fs::path& control_path); + ExpectedT<SourceParagraph, ParseControlErrorInfo> try_load_port(const Files::Filesystem& fs, + const fs::path& control_path); Expected<BinaryParagraph> try_load_cached_package(const VcpkgPaths& paths, const PackageSpec& spec); diff --git a/toolsrc/include/SourceParagraph.h b/toolsrc/include/SourceParagraph.h index 19f558170..9874eb3a9 100644 --- a/toolsrc/include/SourceParagraph.h +++ b/toolsrc/include/SourceParagraph.h @@ -1,5 +1,6 @@ #pragma once +#include "vcpkg_expected.h" #include <unordered_map> #include <vector> @@ -15,14 +16,23 @@ namespace vcpkg const std::string& to_string(const Dependency& dep); + struct ParseControlErrorInfo + { + std::string name; + std::string remaining_fields_as_string; + std::string valid_fields_as_string; + std::error_code error; + }; + /// <summary> /// Port metadata (CONTROL file) /// </summary> struct SourceParagraph { - SourceParagraph(); + static ExpectedT<SourceParagraph, ParseControlErrorInfo> parse_control_file( + std::unordered_map<std::string, std::string> fields); - explicit SourceParagraph(std::unordered_map<std::string, std::string> fields); + SourceParagraph(); std::string name; std::string version; @@ -31,6 +41,9 @@ namespace vcpkg std::vector<Dependency> depends; }; + void print_error_message(const ParseControlErrorInfo& info); + void print_error_message(std::vector<ParseControlErrorInfo> error_info_list); + std::vector<std::string> filter_dependencies(const std::vector<Dependency>& deps, const Triplet& t); std::vector<Dependency> expand_qualified_dependencies(const std::vector<std::string>& depends); diff --git a/toolsrc/include/vcpkg_expected.h b/toolsrc/include/vcpkg_expected.h index 3a920022b..15dbf5e79 100644 --- a/toolsrc/include/vcpkg_expected.h +++ b/toolsrc/include/vcpkg_expected.h @@ -5,27 +5,63 @@ namespace vcpkg { - template<class T> - class Expected + template<class Err> + struct ErrorHolder { - public: - // Constructors are intentionally implicit - Expected(const std::error_code& ec) : m_error_code(ec), m_t() {} + ErrorHolder() : m_is_error(false) {} + ErrorHolder(const Err& err) : m_is_error(true), m_err(err) {} + ErrorHolder(Err&& err) : m_is_error(true), m_err(std::move(err)) {} + + constexpr bool has_error() const { return m_is_error; } + + const Err& error() const { return m_err; } + Err& error() { return m_err; } + + CStringView to_string() const { return "value was error"; } + + private: + bool m_is_error; + Err m_err; + }; + + template<> + struct ErrorHolder<std::error_code> + { + ErrorHolder() = default; + ErrorHolder(const std::error_code& err) : m_err(err) {} + + constexpr bool has_error() const { return bool(m_err); } - Expected(std::errc ec) : Expected(std::make_error_code(ec)) {} + const std::error_code& error() const { return m_err; } + std::error_code& error() { return m_err; } - Expected(const T& t) : m_error_code(), m_t(t) {} + CStringView to_string() const { return "value was error"; } - Expected(T&& t) : m_error_code(), m_t(std::move(t)) {} + private: + std::error_code m_err; + }; + + template<class T, class S> + class ExpectedT + { + public: + constexpr ExpectedT() = default; + + // Constructors are intentionally implicit + + ExpectedT(const S& s) : m_s(s) {} + ExpectedT(S&& s) : m_s(std::move(s)) {} - Expected() : Expected(std::error_code(), T()) {} + ExpectedT(const T& t) : m_t(t) {} + ExpectedT(T&& t) : m_t(std::move(t)) {} - Expected(const Expected&) = default; - Expected(Expected&&) = default; - Expected& operator=(const Expected&) = default; - Expected& operator=(Expected&&) = default; + ExpectedT(const ExpectedT&) = default; + ExpectedT(ExpectedT&&) = default; + ExpectedT& operator=(const ExpectedT&) = default; + ExpectedT& operator=(ExpectedT&&) = default; - std::error_code error_code() const { return this->m_error_code; } + explicit constexpr operator bool() const noexcept { return !m_s.has_error(); } + constexpr bool has_value() const noexcept { return !m_s.has_error(); } T&& value_or_exit(const LineInfo& line_info) && { @@ -39,9 +75,13 @@ namespace vcpkg return this->m_t; } + const S& error() const & { return this->m_s.error(); } + + S&& error() && { return std::move(this->m_s.error()); } + const T* get() const { - if (m_error_code) + if (!this->has_value()) { return nullptr; } @@ -50,7 +90,7 @@ namespace vcpkg T* get() { - if (m_error_code) + if (!this->has_value()) { return nullptr; } @@ -60,10 +100,13 @@ namespace vcpkg private: void exit_if_error(const LineInfo& line_info) const { - Checks::check_exit(line_info, !this->m_error_code, this->m_error_code.message()); + Checks::check_exit(line_info, !m_s.has_error(), m_s.to_string()); } - std::error_code m_error_code; + ErrorHolder<S> m_s; T m_t; }; + + template<class T> + using Expected = ExpectedT<T, std::error_code>; } |
