diff --git a/src/BuildConfig.cc b/src/BuildConfig.cc index b5cd9bb92..464f3a882 100644 --- a/src/BuildConfig.cc +++ b/src/BuildConfig.cc @@ -60,6 +60,8 @@ operator<<(std::ostream& os, VarType type) { Result BuildConfig::init(const Manifest& manifest, const bool isDebug) { + using std::string_view_literals::operator""sv; + std::string libName; if (manifest.package.name.starts_with("lib")) { libName = fmt::format("{}.a", manifest.package.name); diff --git a/src/Cmd/Add.cc b/src/Cmd/Add.cc index 899b6d640..1b846a273 100644 --- a/src/Cmd/Add.cc +++ b/src/Cmd/Add.cc @@ -81,6 +81,8 @@ getDependencyGitUrl(const std::string_view dep) { static std::string getDependencyName(const std::string_view dep) { + using std::string_view_literals::operator""sv; + std::string name; if (dep.find("://") == std::string_view::npos) { name = dep.substr(dep.find_last_of('/') + 1); diff --git a/src/Cmd/Test.cc b/src/Cmd/Test.cc index 2c56c18a0..ff9f2ae40 100644 --- a/src/Cmd/Test.cc +++ b/src/Cmd/Test.cc @@ -164,6 +164,8 @@ Test::compileTestTargets() { Result Test::runTestTargets() { + using std::string_view_literals::operator""sv; + const auto start = std::chrono::steady_clock::now(); std::size_t numPassed = 0; diff --git a/src/Manifest.cc b/src/Manifest.cc index 407c543ad..8d6653eda 100644 --- a/src/Manifest.cc +++ b/src/Manifest.cc @@ -40,9 +40,6 @@ static const fs::path GIT_SRC_DIR(GIT_DIR / "src"); static const std::unordered_set ALLOWED_CHARS = { '-', '_', '/', '.', '+' // allowed in the dependency name }; -static const std::unordered_set KEYWORDS = { -#include "Keywords.def" -}; Result Edition::tryFromString(std::string str) noexcept { @@ -227,8 +224,8 @@ static Result> parseProfiles(const toml::value& val) noexcept { std::unordered_map profiles; const BaseProfile baseProfile = Try(parseBaseProfile(val)); - profiles.insert({ "dev", Try(parseDevProfile(val, baseProfile)) }); - profiles.insert({ "release", Try(parseReleaseProfile(val, baseProfile)) }); + profiles.emplace("dev", Try(parseDevProfile(val, baseProfile))); + profiles.emplace("release", Try(parseReleaseProfile(val, baseProfile))); return Ok(profiles); } @@ -550,7 +547,11 @@ validatePackageName(const std::string_view name) noexcept { std::isalnum(name[name.size() - 1]), "package name must end with a letter or digit" ); - Ensure(!KEYWORDS.contains(name), "package name must not be a C++ keyword"); + + static const std::unordered_set keywords = { +#include "Keywords.def" + }; + Ensure(!keywords.contains(name), "package name must not be a C++ keyword"); return Ok(); } @@ -560,6 +561,7 @@ validatePackageName(const std::string_view name) noexcept { #ifdef CABIN_TEST # include "Rustify/Tests.hpp" +# include "TermColor.hpp" # include # include @@ -1076,6 +1078,8 @@ testValidateDepName() { int main() { + cabin::setColorMode("never"); + tests::testEditionTryFromString(); tests::testEditionComparison(); tests::testPackageTryFromToml(); diff --git a/src/Manifest.hpp b/src/Manifest.hpp index b3dc06ced..1bd64800d 100644 --- a/src/Manifest.hpp +++ b/src/Manifest.hpp @@ -21,7 +21,6 @@ namespace cabin { namespace fs = std::filesystem; -using std::string_view_literals::operator""sv; struct Edition { enum class Year : uint16_t { diff --git a/src/Rustify/Result.hpp b/src/Rustify/Result.hpp index 279ea9930..d8172253f 100644 --- a/src/Rustify/Result.hpp +++ b/src/Rustify/Result.hpp @@ -1,12 +1,14 @@ #pragma once -#include +#include "../TermColor.hpp" + #include #include #include #include #include #include +#include #include #include @@ -57,16 +59,29 @@ namespace toml { template inline Result try_find(const toml::value& v, const U&... u) noexcept { - using namespace std::string_view_literals; // NOLINT + 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(); - // TODO: make the same fix on upstream - if (what.starts_with("[error] ")) { - what = what.substr("[error] "sv.size()); + + 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 logger::error adds one. }