diff --git a/lib/Manifest.cc b/lib/Manifest.cc index 482718593..6ea065261 100644 --- a/lib/Manifest.cc +++ b/lib/Manifest.cc @@ -4,6 +4,7 @@ #include "Builder/Compiler.hpp" #include "Rustify/Result.hpp" #include "Semver.hpp" +#include "TermColor.hpp" #include "VersionReq.hpp" #include @@ -23,6 +24,43 @@ #include #include +namespace toml { + +template +// NOLINTNEXTLINE(readability-identifier-naming,cppcoreguidelines-macro-usage) +inline Result try_find(const toml::value& v, const U&... u) noexcept { + using std::string_view_literals::operator""sv; + + if (cabin::shouldColorStderr()) { + color::enable(); + } else { + color::disable(); + } + + try { + return Ok(toml::find(v, u...)); + } catch (const std::exception& e) { + std::string what = e.what(); + + static constexpr std::size_t errorPrefixSize = "[error] "sv.size(); + static constexpr std::size_t colorErrorPrefixSize = + "\033[31m\033[01m[error]\033[00m "sv.size(); + + if (cabin::shouldColorStderr()) { + what = what.substr(colorErrorPrefixSize); + } else { + what = what.substr(errorPrefixSize); + } + + if (what.back() == '\n') { + what.pop_back(); // remove the last '\n' since Diag::error adds one. + } + return Err(anyhow::anyhow(what)); + } +} + +} // namespace toml + namespace cabin { static const std::unordered_set ALLOWED_CHARS = { @@ -637,7 +675,6 @@ Result validatePackageName(const std::string_view name) noexcept { #ifdef CABIN_TEST # include "Rustify/Tests.hpp" -# include "TermColor.hpp" # include # include diff --git a/rustify/include/Rustify/Result.hpp b/rustify/include/Rustify/Result.hpp index 8898c6231..84fc67443 100644 --- a/rustify/include/Rustify/Result.hpp +++ b/rustify/include/Rustify/Result.hpp @@ -1,7 +1,5 @@ #pragma once -#include "TermColor.hpp" - #include #include #include @@ -49,46 +47,4 @@ inline constexpr auto to_anyhow = [](auto... xs) { return anyhow::anyhow(std::forward(xs)...); }; -#if __has_include() - -# include - -namespace toml { - -template -inline Result try_find(const toml::value& v, const U&... u) noexcept { - using std::string_view_literals::operator""sv; - - if (cabin::shouldColorStderr()) { - color::enable(); - } else { - color::disable(); - } - - try { - return Ok(toml::find(v, u...)); - } catch (const std::exception& e) { - std::string what = e.what(); - - static constexpr std::size_t errorPrefixSize = "[error] "sv.size(); - static constexpr std::size_t colorErrorPrefixSize = - "\033[31m\033[01m[error]\033[00m "sv.size(); - - if (cabin::shouldColorStderr()) { - what = what.substr(colorErrorPrefixSize); - } else { - what = what.substr(errorPrefixSize); - } - - if (what.back() == '\n') { - what.pop_back(); // remove the last '\n' since Diag::error adds one. - } - return Err(anyhow::anyhow(what)); - } -} - -} // namespace toml - -#endif - // NOLINTEND(readability-identifier-naming,cppcoreguidelines-macro-usage)