Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toml: enable colorization #1138

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ operator<<(std::ostream& os, VarType type) {

Result<BuildConfig>
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);
Expand Down
2 changes: 2 additions & 0 deletions src/Cmd/Add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Cmd/Test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Test::compileTestTargets() {

Result<void>
Test::runTestTargets() {
using std::string_view_literals::operator""sv;

const auto start = std::chrono::steady_clock::now();

std::size_t numPassed = 0;
Expand Down
16 changes: 10 additions & 6 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ static const fs::path GIT_SRC_DIR(GIT_DIR / "src");
static const std::unordered_set<char> ALLOWED_CHARS = {
'-', '_', '/', '.', '+' // allowed in the dependency name
};
static const std::unordered_set<std::string_view> KEYWORDS = {
#include "Keywords.def"
};

Result<Edition>
Edition::tryFromString(std::string str) noexcept {
Expand Down Expand Up @@ -227,8 +224,8 @@ static Result<std::unordered_map<std::string, Profile>>
parseProfiles(const toml::value& val) noexcept {
std::unordered_map<std::string, Profile> 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);
}

Expand Down Expand Up @@ -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<std::string_view> keywords = {
#include "Keywords.def"
};
Ensure(!keywords.contains(name), "package name must not be a C++ keyword");

return Ok();
}
Expand All @@ -560,6 +561,7 @@ validatePackageName(const std::string_view name) noexcept {
#ifdef CABIN_TEST

# include "Rustify/Tests.hpp"
# include "TermColor.hpp"

# include <climits>
# include <fmt/ranges.h>
Expand Down Expand Up @@ -1076,6 +1078,8 @@ testValidateDepName() {

int
main() {
cabin::setColorMode("never");

tests::testEditionTryFromString();
tests::testEditionComparison();
tests::testPackageTryFromToml();
Expand Down
1 change: 0 additions & 1 deletion src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace cabin {

namespace fs = std::filesystem;
using std::string_view_literals::operator""sv;

struct Edition {
enum class Year : uint16_t {
Expand Down
25 changes: 20 additions & 5 deletions src/Rustify/Result.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <concepts>
#include "../TermColor.hpp"

#include <exception>
#include <fmt/core.h>
#include <memory>
#include <mitama/anyhow/anyhow.hpp>
#include <mitama/result/result.hpp>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -57,16 +59,29 @@ namespace toml {
template <typename T, typename... U>
inline Result<T>
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<T>(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.
}
Expand Down
Loading