[M1] add util::Error, Result<T>, format()
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import cargoxx;
|
||||
import std;
|
||||
|
||||
int main(int /*argc*/, char** /*argv*/) {
|
||||
std::println("Hello, cargoxx!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
29
src/util/error.cpp
Normal file
29
src/util/error.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
module cargoxx.util;
|
||||
|
||||
import std;
|
||||
|
||||
namespace cargoxx::util {
|
||||
|
||||
auto format(const Error& e) -> std::string {
|
||||
std::string out;
|
||||
out += std::format("error[E{:04}]: {}\n", static_cast<int>(e.code), e.message);
|
||||
|
||||
if (e.location) {
|
||||
if (e.line_col) {
|
||||
out += std::format(" --> {}:{}:{}\n",
|
||||
e.location->string(),
|
||||
e.line_col->first,
|
||||
e.line_col->second);
|
||||
} else {
|
||||
out += std::format(" --> {}\n", e.location->string());
|
||||
}
|
||||
}
|
||||
|
||||
if (!e.hint.empty()) {
|
||||
out += std::format(" hint: {}\n", e.hint);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace cargoxx::util
|
||||
@@ -1 +1,50 @@
|
||||
export module cargoxx.util;
|
||||
|
||||
import std;
|
||||
|
||||
export namespace cargoxx::util {
|
||||
|
||||
// Error code numbering follows TECH_SPEC.md §4.
|
||||
enum class ErrorCode {
|
||||
ManifestNotFound = 1,
|
||||
ManifestParseError,
|
||||
ManifestInvalidField,
|
||||
ManifestUnknownField,
|
||||
ManifestVersionInvalid,
|
||||
|
||||
LayoutNoTarget = 20,
|
||||
LayoutAmbiguousLib,
|
||||
LayoutInvalidName,
|
||||
|
||||
ResolutionUnknownPackage = 40,
|
||||
ResolutionNetworkError,
|
||||
ResolutionUnsatisfiable,
|
||||
ResolutionVersionNotFound,
|
||||
|
||||
LinkdbUnknownPackage = 60,
|
||||
LinkdbCorrupt,
|
||||
LinkdbComponentNotSupported,
|
||||
|
||||
ExecCommandFailed = 80,
|
||||
ExecToolNotFound,
|
||||
BuildCmakeFailed,
|
||||
BuildNixFailed,
|
||||
|
||||
Internal = 100,
|
||||
NotImplemented,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
ErrorCode code;
|
||||
std::string message;
|
||||
std::string hint;
|
||||
std::optional<std::filesystem::path> location;
|
||||
std::optional<std::pair<int, int>> line_col;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using Result = std::expected<T, Error>;
|
||||
|
||||
auto format(const Error& e) -> std::string;
|
||||
|
||||
} // namespace cargoxx::util
|
||||
|
||||
Reference in New Issue
Block a user