mirror of
https://github.com/microsoft/vcpkg.git
synced 2024-11-29 03:11:38 +08:00
expected -> Expected
This commit is contained in:
parent
ccbb2ebcda
commit
9e19213498
@ -7,9 +7,9 @@ namespace vcpkg
|
||||
{
|
||||
struct PackageSpec
|
||||
{
|
||||
static expected<PackageSpec> from_string(const std::string& spec_as_string, const Triplet& default_target_triplet);
|
||||
static Expected<PackageSpec> from_string(const std::string& spec_as_string, const Triplet& default_target_triplet);
|
||||
|
||||
static expected<PackageSpec> from_name_and_triplet(const std::string& name, const Triplet& target_triplet);
|
||||
static Expected<PackageSpec> from_name_and_triplet(const std::string& name, const Triplet& target_triplet);
|
||||
|
||||
const std::string& name() const;
|
||||
|
||||
|
@ -11,14 +11,14 @@ namespace vcpkg::Paragraphs
|
||||
{
|
||||
using ParagraphDataMap = std::unordered_map<std::string, std::string>;
|
||||
|
||||
expected<ParagraphDataMap> get_single_paragraph(const fs::path& control_path);
|
||||
expected<std::vector<ParagraphDataMap>> get_paragraphs(const fs::path& control_path);
|
||||
expected<ParagraphDataMap> parse_single_paragraph(const std::string& str);
|
||||
expected<std::vector<ParagraphDataMap>> parse_paragraphs(const std::string& str);
|
||||
Expected<ParagraphDataMap> get_single_paragraph(const fs::path& control_path);
|
||||
Expected<std::vector<ParagraphDataMap>> get_paragraphs(const fs::path& control_path);
|
||||
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 fs::path& control_path);
|
||||
Expected<SourceParagraph> try_load_port(const fs::path& control_path);
|
||||
|
||||
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const PackageSpec& spec);
|
||||
Expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const PackageSpec& spec);
|
||||
|
||||
std::vector<SourceParagraph> load_all_ports(const fs::path& ports_dir);
|
||||
|
||||
|
@ -10,9 +10,9 @@ namespace vcpkg::Files
|
||||
|
||||
bool has_invalid_chars_for_filesystem(const std::string& s);
|
||||
|
||||
expected<std::string> read_contents(const fs::path& file_path) noexcept;
|
||||
Expected<std::string> read_contents(const fs::path& file_path) noexcept;
|
||||
|
||||
expected<std::vector<std::string>> read_all_lines(const fs::path& file_path);
|
||||
Expected<std::vector<std::string>> read_all_lines(const fs::path& file_path);
|
||||
|
||||
void write_all_lines(const fs::path& file_path, const std::vector<std::string>& lines);
|
||||
|
||||
|
@ -6,34 +6,34 @@
|
||||
namespace vcpkg
|
||||
{
|
||||
template <class T>
|
||||
class expected
|
||||
class Expected
|
||||
{
|
||||
public:
|
||||
// Constructors are intentionally implicit
|
||||
expected(const std::error_code& ec) : m_error_code(ec), m_t()
|
||||
Expected(const std::error_code& ec) : m_error_code(ec), m_t()
|
||||
{
|
||||
}
|
||||
|
||||
expected(std::errc ec) : expected(std::make_error_code(ec))
|
||||
Expected(std::errc ec) : Expected(std::make_error_code(ec))
|
||||
{
|
||||
}
|
||||
|
||||
expected(const T& t) : m_error_code(), m_t(t)
|
||||
Expected(const T& t) : m_error_code(), m_t(t)
|
||||
{
|
||||
}
|
||||
|
||||
expected(T&& t) : m_error_code(), m_t(std::move(t))
|
||||
Expected(T&& t) : m_error_code(), m_t(std::move(t))
|
||||
{
|
||||
}
|
||||
|
||||
expected() : expected(std::error_code(), T())
|
||||
Expected() : Expected(std::error_code(), T())
|
||||
{
|
||||
}
|
||||
|
||||
expected(const expected&) = default;
|
||||
expected(expected&&) = default;
|
||||
expected& operator=(const expected&) = default;
|
||||
expected& operator=(expected&&) = default;
|
||||
Expected(const Expected&) = default;
|
||||
Expected(Expected&&) = default;
|
||||
Expected& operator=(const Expected&) = default;
|
||||
Expected& operator=(Expected&&) = default;
|
||||
|
||||
std::error_code error_code() const
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ namespace vcpkg
|
||||
|
||||
struct vcpkg_paths
|
||||
{
|
||||
static expected<vcpkg_paths> create(const fs::path& vcpkg_root_dir);
|
||||
static Expected<vcpkg_paths> create(const fs::path& vcpkg_root_dir);
|
||||
|
||||
fs::path package_dir(const PackageSpec& spec) const;
|
||||
fs::path port_dir(const PackageSpec& spec) const;
|
||||
|
@ -8,7 +8,7 @@ namespace vcpkg
|
||||
return (c == '-') || isdigit(c) || (isalpha(c) && islower(c));
|
||||
}
|
||||
|
||||
expected<PackageSpec> PackageSpec::from_string(const std::string& spec_as_string, const Triplet& default_target_triplet)
|
||||
Expected<PackageSpec> PackageSpec::from_string(const std::string& spec_as_string, const Triplet& default_target_triplet)
|
||||
{
|
||||
auto pos = spec_as_string.find(':');
|
||||
if (pos == std::string::npos)
|
||||
@ -27,7 +27,7 @@ namespace vcpkg
|
||||
return from_name_and_triplet(name, target_triplet);
|
||||
}
|
||||
|
||||
expected<PackageSpec> PackageSpec::from_name_and_triplet(const std::string& name, const Triplet& target_triplet)
|
||||
Expected<PackageSpec> PackageSpec::from_name_and_triplet(const std::string& name, const Triplet& target_triplet)
|
||||
{
|
||||
if (std::find_if_not(name.cbegin(), name.cend(), is_valid_package_spec_char) != name.end())
|
||||
{
|
||||
|
@ -172,9 +172,9 @@ namespace vcpkg::Paragraphs
|
||||
}
|
||||
};
|
||||
|
||||
expected<std::unordered_map<std::string, std::string>> get_single_paragraph(const fs::path& control_path)
|
||||
Expected<std::unordered_map<std::string, std::string>> get_single_paragraph(const fs::path& control_path)
|
||||
{
|
||||
const expected<std::string> contents = Files::read_contents(control_path);
|
||||
const Expected<std::string> contents = Files::read_contents(control_path);
|
||||
if (auto spgh = contents.get())
|
||||
{
|
||||
return parse_single_paragraph(*spgh);
|
||||
@ -183,9 +183,9 @@ namespace vcpkg::Paragraphs
|
||||
return contents.error_code();
|
||||
}
|
||||
|
||||
expected<std::vector<std::unordered_map<std::string, std::string>>> get_paragraphs(const fs::path& control_path)
|
||||
Expected<std::vector<std::unordered_map<std::string, std::string>>> get_paragraphs(const fs::path& control_path)
|
||||
{
|
||||
const expected<std::string> contents = Files::read_contents(control_path);
|
||||
const Expected<std::string> contents = Files::read_contents(control_path);
|
||||
if (auto spgh = contents.get())
|
||||
{
|
||||
return parse_paragraphs(*spgh);
|
||||
@ -194,7 +194,7 @@ namespace vcpkg::Paragraphs
|
||||
return contents.error_code();
|
||||
}
|
||||
|
||||
expected<std::unordered_map<std::string, std::string>> parse_single_paragraph(const std::string& str)
|
||||
Expected<std::unordered_map<std::string, std::string>> parse_single_paragraph(const std::string& str)
|
||||
{
|
||||
const std::vector<std::unordered_map<std::string, std::string>> p = Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs();
|
||||
|
||||
@ -206,14 +206,14 @@ namespace vcpkg::Paragraphs
|
||||
return std::error_code(ParagraphParseResult::EXPECTED_ONE_PARAGRAPH);
|
||||
}
|
||||
|
||||
expected<std::vector<std::unordered_map<std::string, std::string>>> parse_paragraphs(const std::string& str)
|
||||
Expected<std::vector<std::unordered_map<std::string, std::string>>> parse_paragraphs(const std::string& str)
|
||||
{
|
||||
return Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs();
|
||||
}
|
||||
|
||||
expected<SourceParagraph> try_load_port(const fs::path& path)
|
||||
Expected<SourceParagraph> try_load_port(const fs::path& path)
|
||||
{
|
||||
expected<std::unordered_map<std::string, std::string>> pghs = get_single_paragraph(path / "CONTROL");
|
||||
Expected<std::unordered_map<std::string, std::string>> pghs = get_single_paragraph(path / "CONTROL");
|
||||
if (auto p = pghs.get())
|
||||
{
|
||||
return SourceParagraph(*p);
|
||||
@ -222,9 +222,9 @@ namespace vcpkg::Paragraphs
|
||||
return pghs.error_code();
|
||||
}
|
||||
|
||||
expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const PackageSpec& spec)
|
||||
Expected<BinaryParagraph> try_load_cached_package(const vcpkg_paths& paths, const PackageSpec& spec)
|
||||
{
|
||||
expected<std::unordered_map<std::string, std::string>> pghs = get_single_paragraph(paths.package_dir(spec) / "CONTROL");
|
||||
Expected<std::unordered_map<std::string, std::string>> pghs = get_single_paragraph(paths.package_dir(spec) / "CONTROL");
|
||||
|
||||
if (auto p = pghs.get())
|
||||
{
|
||||
@ -240,7 +240,7 @@ namespace vcpkg::Paragraphs
|
||||
for (auto it = fs::directory_iterator(ports_dir); it != fs::directory_iterator(); ++it)
|
||||
{
|
||||
const fs::path& path = it->path();
|
||||
expected<SourceParagraph> source_paragraph = try_load_port(path);
|
||||
Expected<SourceParagraph> source_paragraph = try_load_port(path);
|
||||
if (auto srcpgh = source_paragraph.get())
|
||||
{
|
||||
output.emplace_back(std::move(*srcpgh));
|
||||
|
@ -39,7 +39,7 @@ namespace vcpkg::PostBuildLint
|
||||
|
||||
BuildInfo read_build_info(const fs::path& filepath)
|
||||
{
|
||||
const expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(filepath);
|
||||
const Expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(filepath);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, pghs.get() != nullptr, "Invalid BUILD_INFO file for package");
|
||||
return BuildInfo::create(*pghs.get());
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ namespace vcpkg::Commands::Build
|
||||
Checks::exit_success(VCPKG_LINE_INFO);
|
||||
}
|
||||
|
||||
const expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(port_dir);
|
||||
const Expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(port_dir);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, !maybe_spgh.error_code(), "Could not find package named %s: %s", spec, maybe_spgh.error_code().message());
|
||||
const SourceParagraph& spgh = *maybe_spgh.get();
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace vcpkg::Commands::Cache
|
||||
for (auto it = fs::directory_iterator(paths.packages); it != fs::directory_iterator(); ++it)
|
||||
{
|
||||
const fs::path& path = it->path();
|
||||
const expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(path / "CONTROL");
|
||||
const Expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(path / "CONTROL");
|
||||
if (auto p = pghs.get())
|
||||
{
|
||||
const BinaryParagraph binary_paragraph = BinaryParagraph(*p);
|
||||
|
@ -78,7 +78,7 @@ namespace vcpkg::Commands::Import
|
||||
const fs::path include_directory(args.command_arguments[1]);
|
||||
const fs::path project_directory(args.command_arguments[2]);
|
||||
|
||||
const expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(control_file_path);
|
||||
const Expected<std::unordered_map<std::string, std::string>> pghs = Paragraphs::get_single_paragraph(control_file_path);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, pghs.get() != nullptr, "Invalid control file %s for package", control_file_path.generic_string());
|
||||
|
||||
StatusParagraph spgh;
|
||||
|
@ -166,7 +166,7 @@ namespace vcpkg::Commands::Integrate
|
||||
fs::create_directory(tmp_dir);
|
||||
|
||||
bool should_install_system = true;
|
||||
const expected<std::string> system_wide_file_contents = Files::read_contents(system_wide_targets_file);
|
||||
const Expected<std::string> system_wide_file_contents = Files::read_contents(system_wide_targets_file);
|
||||
if (auto contents_data = system_wide_file_contents.get())
|
||||
{
|
||||
std::regex re(R"###(<!-- version (\d+) -->)###");
|
||||
|
@ -350,7 +350,7 @@ namespace UnitTest1
|
||||
|
||||
TEST_METHOD(package_spec_parse)
|
||||
{
|
||||
vcpkg::expected<vcpkg::PackageSpec> spec = vcpkg::PackageSpec::from_string("zlib", vcpkg::Triplet::X86_WINDOWS);
|
||||
vcpkg::Expected<vcpkg::PackageSpec> spec = vcpkg::PackageSpec::from_string("zlib", vcpkg::Triplet::X86_WINDOWS);
|
||||
Assert::AreEqual(vcpkg::PackageSpecParseResult::SUCCESS, vcpkg::to_package_spec_parse_result(spec.error_code()));
|
||||
Assert::AreEqual("zlib", spec.get()->name().c_str());
|
||||
Assert::AreEqual(vcpkg::Triplet::X86_WINDOWS.canonical_name(), spec.get()->target_triplet().canonical_name());
|
||||
@ -358,7 +358,7 @@ namespace UnitTest1
|
||||
|
||||
TEST_METHOD(package_spec_parse_with_arch)
|
||||
{
|
||||
vcpkg::expected<vcpkg::PackageSpec> spec = vcpkg::PackageSpec::from_string("zlib:x64-uwp", vcpkg::Triplet::X86_WINDOWS);
|
||||
vcpkg::Expected<vcpkg::PackageSpec> spec = vcpkg::PackageSpec::from_string("zlib:x64-uwp", vcpkg::Triplet::X86_WINDOWS);
|
||||
Assert::AreEqual(vcpkg::PackageSpecParseResult::SUCCESS, vcpkg::to_package_spec_parse_result(spec.error_code()));
|
||||
Assert::AreEqual("zlib", spec.get()->name().c_str());
|
||||
Assert::AreEqual(vcpkg::Triplet::X64_UWP.canonical_name(), spec.get()->target_triplet().canonical_name());
|
||||
|
@ -58,7 +58,7 @@ static void inner(const VcpkgCmdArguments& args)
|
||||
|
||||
Checks::check_exit(VCPKG_LINE_INFO, !vcpkg_root_dir.empty(), "Error: Could not detect vcpkg-root.");
|
||||
|
||||
const expected<vcpkg_paths> expected_paths = vcpkg_paths::create(vcpkg_root_dir);
|
||||
const Expected<vcpkg_paths> expected_paths = vcpkg_paths::create(vcpkg_root_dir);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, !expected_paths.error_code(), "Error: Invalid vcpkg root directory %s: %s", vcpkg_root_dir.string(), expected_paths.error_code().message());
|
||||
const vcpkg_paths paths = expected_paths.value_or_exit(VCPKG_LINE_INFO);
|
||||
int exit_code = _wchdir(paths.root.c_str());
|
||||
|
@ -72,7 +72,7 @@ namespace vcpkg::Dependencies
|
||||
continue;
|
||||
}
|
||||
|
||||
expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
|
||||
Expected<BinaryParagraph> maybe_bpgh = Paragraphs::try_load_cached_package(paths, spec);
|
||||
if (BinaryParagraph* bpgh = maybe_bpgh.get())
|
||||
{
|
||||
process_dependencies(bpgh->depends);
|
||||
@ -80,7 +80,7 @@ namespace vcpkg::Dependencies
|
||||
continue;
|
||||
}
|
||||
|
||||
expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(paths.port_dir(spec));
|
||||
Expected<SourceParagraph> maybe_spgh = Paragraphs::try_load_port(paths.port_dir(spec));
|
||||
if (auto spgh = maybe_spgh.get())
|
||||
{
|
||||
process_dependencies(filter_dependencies(spgh->depends, spec.target_triplet()));
|
||||
|
@ -11,7 +11,7 @@ namespace vcpkg::Files
|
||||
return std::regex_search(s, FILESYSTEM_INVALID_CHARACTERS_REGEX);
|
||||
}
|
||||
|
||||
expected<std::string> read_contents(const fs::path& file_path) noexcept
|
||||
Expected<std::string> read_contents(const fs::path& file_path) noexcept
|
||||
{
|
||||
std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
|
||||
if (file_stream.fail())
|
||||
@ -36,7 +36,7 @@ namespace vcpkg::Files
|
||||
return std::move(output);
|
||||
}
|
||||
|
||||
expected<std::vector<std::string>> read_all_lines(const fs::path& file_path)
|
||||
Expected<std::vector<std::string>> read_all_lines(const fs::path& file_path)
|
||||
{
|
||||
std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
|
||||
if (file_stream.fail())
|
||||
|
@ -9,7 +9,7 @@ namespace vcpkg::Input
|
||||
PackageSpec check_and_get_package_spec(const std::string& package_spec_as_string, const Triplet& default_target_triplet, CStringView example_text)
|
||||
{
|
||||
const std::string as_lowercase = Strings::ascii_to_lowercase(package_spec_as_string);
|
||||
expected<PackageSpec> expected_spec = PackageSpec::from_string(as_lowercase, default_target_triplet);
|
||||
Expected<PackageSpec> expected_spec = PackageSpec::from_string(as_lowercase, default_target_triplet);
|
||||
if (auto spec = expected_spec.get())
|
||||
{
|
||||
return *spec;
|
||||
|
@ -152,7 +152,7 @@ namespace vcpkg
|
||||
return fetch_dependency(scripts_folder, L"git", downloaded_copy);
|
||||
}
|
||||
|
||||
expected<vcpkg_paths> vcpkg_paths::create(const fs::path& vcpkg_root_dir)
|
||||
Expected<vcpkg_paths> vcpkg_paths::create(const fs::path& vcpkg_root_dir)
|
||||
{
|
||||
std::error_code ec;
|
||||
const fs::path canonical_vcpkg_root_dir = fs::canonical(vcpkg_root_dir, ec);
|
||||
|
Loading…
Reference in New Issue
Block a user