From 635fbe4b37d3cfeb7073df6fee4619cd0dff9782 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Fri, 27 Jan 2023 11:46:16 +0100 Subject: remote/config.hpp: make ParsePort re-usable for execution-service --- src/buildtool/execution_api/remote/config.hpp | 44 +++++++++++++++++---------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/buildtool/execution_api/remote/config.hpp b/src/buildtool/execution_api/remote/config.hpp index 683ac12e..42d0be80 100644 --- a/src/buildtool/execution_api/remote/config.hpp +++ b/src/buildtool/execution_api/remote/config.hpp @@ -17,12 +17,10 @@ #include #include -#include #include #include #include #include -#include #include #include "gsl-lite/gsl-lite.hpp" @@ -33,6 +31,31 @@ struct PortTag : type_safe_arithmetic_tag {}; using Port = type_safe_arithmetic; +[[nodiscard]] static auto ParsePort(int const port_num) noexcept + -> std::optional { + try { + static constexpr int kMaxPortNumber{ + std::numeric_limits::max()}; + if (port_num >= 0 and port_num <= kMaxPortNumber) { + return gsl::narrow_cast(port_num); + } + + } catch (std::out_of_range const& e) { + Logger::Log(LogLevel::Error, "Port raised out_of_range exception."); + } + return std::nullopt; +} + +[[nodiscard]] static auto ParsePort(std::string const& port) noexcept + -> std::optional { + try { + auto port_num = std::stoi(port); + return ParsePort(port_num); + } catch (std::invalid_argument const& e) { + Logger::Log(LogLevel::Error, "Port raised invalid_argument exception."); + } + return std::nullopt; +} class RemoteExecutionConfig { public: struct ServerAddress { @@ -108,20 +131,9 @@ class RemoteExecutionConfig { not std::getline(iss, port, ':')) { return std::nullopt; } - try { - static constexpr int kMaxPortNumber{ - std::numeric_limits::max()}; - auto port_num = std::stoi(port); - if (not host.empty() and port_num >= 0 and - port_num <= kMaxPortNumber) { - return ServerAddress{std::move(host), - gsl::narrow_cast(port_num)}; - } - } catch (std::out_of_range const& e) { - Logger::Log(LogLevel::Error, "Port raised out_of_range exception."); - } catch (std::invalid_argument const& e) { - Logger::Log(LogLevel::Error, - "Port raised invalid_argument exception."); + auto port_num = ParsePort(port); + if (not host.empty() and port_num) { + return ServerAddress{std::move(host), *port_num}; } return std::nullopt; } -- cgit v1.2.3