From d2bab3c370fca9fa5216d97495810faaeeaebc2d Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Fri, 24 Mar 2017 10:46:49 -0700 Subject: Rework Paragraph parsing error handling. Add single paragraph functions --- toolsrc/src/Paragraphs.cpp | 57 +++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) (limited to 'toolsrc/src/Paragraphs.cpp') diff --git a/toolsrc/src/Paragraphs.cpp b/toolsrc/src/Paragraphs.cpp index f1505698c..2bdddb58c 100644 --- a/toolsrc/src/Paragraphs.cpp +++ b/toolsrc/src/Paragraphs.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "Paragraphs.h" #include "vcpkg_Files.h" +#include "paragraph_parse_result.h" namespace vcpkg::Paragraphs { @@ -150,7 +151,18 @@ namespace vcpkg::Paragraphs } }; - std::vector> get_paragraphs(const fs::path& control_path) + expected> get_single_paragraph(const fs::path& control_path) + { + const expected contents = Files::read_contents(control_path); + if (auto spgh = contents.get()) + { + return parse_single_paragraph(*spgh); + } + + return contents.error_code(); + } + + expected>> get_paragraphs(const fs::path& control_path) { const expected contents = Files::read_contents(control_path); if (auto spgh = contents.get()) @@ -158,44 +170,47 @@ namespace vcpkg::Paragraphs return parse_paragraphs(*spgh); } - Checks::exit_with_message(VCPKG_LINE_INFO, "Error while reading %s: %s", control_path.generic_string(), contents.error_code().message()); + return contents.error_code(); + } + + expected> parse_single_paragraph(const std::string& str) + { + const std::vector> p = Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs(); + + if (p.size() == 1) + { + return p.at(0); + } + + return std::error_code(paragraph_parse_result::EXPECTED_ONE_PARAGRAPH); } - std::vector> parse_paragraphs(const std::string& str) + expected>> parse_paragraphs(const std::string& str) { return Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs(); } expected try_load_port(const fs::path& path) { - try + expected> pghs = get_single_paragraph(path / "CONTROL"); + if (auto p = pghs.get()) { - auto pghs = get_paragraphs(path / "CONTROL"); - Checks::check_exit(VCPKG_LINE_INFO, pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string()); - return SourceParagraph(pghs[0]); + return SourceParagraph(*p); } - catch (std::runtime_error const&) {} - return std::errc::no_such_file_or_directory; + return pghs.error_code(); } expected try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec) { - const fs::path path = paths.package_dir(spec) / "CONTROL"; + expected> pghs = get_single_paragraph(paths.package_dir(spec) / "CONTROL"); - auto control_contents_maybe = Files::read_contents(path); - if (auto control_contents = control_contents_maybe.get()) + if (auto p = pghs.get()) { - std::vector> pghs; - try - { - pghs = parse_paragraphs(*control_contents); - } - catch (std::runtime_error) {} - Checks::check_exit(VCPKG_LINE_INFO, pghs.size() == 1, "Invalid control file at %s", path.string()); - return BinaryParagraph(pghs[0]); + return BinaryParagraph(*p); } - return control_contents_maybe.error_code(); + + return pghs.error_code(); } std::vector load_all_ports(const fs::path& ports_dir) -- cgit v1.2.3