Skip to content
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
39 changes: 38 additions & 1 deletion lib/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Builder/Compiler.hpp"
#include "Rustify/Result.hpp"
#include "Semver.hpp"
#include "TermColor.hpp"
#include "VersionReq.hpp"

#include <cctype>
Expand All @@ -23,6 +24,43 @@
#include <utility>
#include <vector>

namespace toml {

template <typename T, typename... U>
// NOLINTNEXTLINE(readability-identifier-naming,cppcoreguidelines-macro-usage)
inline Result<T> 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<T>(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<char> ALLOWED_CHARS = {
Expand Down Expand Up @@ -637,7 +675,6 @@ Result<void> 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
44 changes: 0 additions & 44 deletions rustify/include/Rustify/Result.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "TermColor.hpp"

#include <exception>
#include <fmt/core.h>
#include <memory>
Expand Down Expand Up @@ -49,46 +47,4 @@ inline constexpr auto to_anyhow = [](auto... xs) {
return anyhow::anyhow(std::forward<decltype(xs)>(xs)...);
};

#if __has_include(<toml.hpp>)

# include <toml.hpp>

namespace toml {

template <typename T, typename... U>
inline Result<T> 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<T>(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)
Loading