From f29682753c3b54465ce13bc6025dfffecb336c7c Mon Sep 17 00:00:00 2001 From: Albin Johansson Date: Tue, 8 Oct 2024 17:25:40 +0200 Subject: [PATCH] Add error code enum --- .../lib/inc/tactile/base/container/result.hpp | 9 +- .../lib/inc/tactile/base/debug/error_code.hpp | 88 +++++++++++++++++++ .../lib/src/gd3_exporter.cpp | 4 +- .../lib/src/godot_scene_format.cpp | 4 +- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 source/base/lib/inc/tactile/base/debug/error_code.hpp diff --git a/source/base/lib/inc/tactile/base/container/result.hpp b/source/base/lib/inc/tactile/base/container/result.hpp index 0634a44877..b0dcece58b 100644 --- a/source/base/lib/inc/tactile/base/container/result.hpp +++ b/source/base/lib/inc/tactile/base/container/result.hpp @@ -2,14 +2,15 @@ #pragma once -#include // expected -#include // error_code +#include // expected + +#include "tactile/base/debug/error_code.hpp" namespace tactile { template -using Result = std::expected; +using Result = std::expected; -inline const Result kOK {}; +inline constexpr Result kOK {}; } // namespace tactile diff --git a/source/base/lib/inc/tactile/base/debug/error_code.hpp b/source/base/lib/inc/tactile/base/debug/error_code.hpp new file mode 100644 index 0000000000..1ef9c78b65 --- /dev/null +++ b/source/base/lib/inc/tactile/base/debug/error_code.hpp @@ -0,0 +1,88 @@ +// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) + +#pragma once + +#include // string_view + +namespace tactile { + +/** + * Provides common error codes. + */ +enum class ErrorCode : int +{ + /** An unknown error occurred. */ + kUnknown, + + /** An operation or feature isn't supported. */ + kNotSupported, + + /** Not enough memory. */ + kOutOfMemory, + + /** A stack overflow was detected. */ + kStackOverflow, + + /** A stack underflow was detected. */ + kStackUnderflow, + + /** Initialization failed. */ + kBadInit, + + /** A given parameter is invalid. */ + kBadParam, + + /** An invalid state was detected. */ + kBadState, + + /** An invalid operation was attempted. */ + kBadOperation, + + /** A file doesn't exist. */ + kNoSuchFile, + + /** A file stream couldn't be created. */ + kBadFileStream, + + /** A file couldn't be copied. */ + kBadFileCopy, + + /** An invalid image was detected. */ + kBadImage, + + /** An invalid save file was detected. */ + kBadSaveFile, + + /** A compression operation failed. */ + kCouldNotCompress, + + /** A decompression operation failed. */ + kCouldNotDecompress, +}; + +[[nodiscard]] +constexpr auto to_string(const ErrorCode errc) noexcept -> std::string_view +{ + switch (errc) { + case ErrorCode::kUnknown: return "unknown"; + case ErrorCode::kNotSupported: return "not supported"; + case ErrorCode::kOutOfMemory: return "out of memory"; + case ErrorCode::kStackOverflow: return "stack overflow"; + case ErrorCode::kStackUnderflow: return "stack underflow"; + case ErrorCode::kBadInit: return "initialization error"; + case ErrorCode::kBadParam: return "invalid parameter"; + case ErrorCode::kBadState: return "invalid state"; + case ErrorCode::kBadOperation: return "invalid operation"; + case ErrorCode::kNoSuchFile: return "no such file"; + case ErrorCode::kBadFileStream: return "file stream error"; + case ErrorCode::kBadFileCopy: return "file copy error"; + case ErrorCode::kBadImage: return "invalid image"; + case ErrorCode::kBadSaveFile: return "invalid save file"; + case ErrorCode::kCouldNotCompress: return "could not compress"; + case ErrorCode::kCouldNotDecompress: return "could not decompress"; + } + + return "?"; +} + +} // namespace tactile diff --git a/source/godot_tscn_format/lib/src/gd3_exporter.cpp b/source/godot_tscn_format/lib/src/gd3_exporter.cpp index 196da91d11..d4953b380d 100644 --- a/source/godot_tscn_format/lib/src/gd3_exporter.cpp +++ b/source/godot_tscn_format/lib/src/gd3_exporter.cpp @@ -439,7 +439,7 @@ auto _emit_map_file(const Gd3Map& map, const SaveFormatWriteOptions& options) -> std::ofstream stream {path, std::ios::out | std::ios::trunc}; if (!stream.good()) { - return std::unexpected {std::make_error_code(std::errc::io_error)}; + return std::unexpected {ErrorCode::kBadFileStream}; } Gd3SceneWriter writer {stream}; @@ -492,7 +492,7 @@ auto _save_tileset_images(const Gd3Tileset& tileset, const SaveFormatWriteOption copy_error); if (copy_error) { - return std::unexpected {copy_error}; + return std::unexpected {ErrorCode::kBadFileCopy}; } } diff --git a/source/godot_tscn_format/lib/src/godot_scene_format.cpp b/source/godot_tscn_format/lib/src/godot_scene_format.cpp index a2b3d7434f..96910652d8 100644 --- a/source/godot_tscn_format/lib/src/godot_scene_format.cpp +++ b/source/godot_tscn_format/lib/src/godot_scene_format.cpp @@ -73,7 +73,9 @@ auto GodotSceneFormat::save_map(const IMapView& map, map.accept(converter); const auto& gd_map = converter.get_map(); - return save_godot3_scene(gd_map, options); + return save_godot3_scene(gd_map, options).transform_error([](const ErrorCode) { + return std::make_error_code(std::errc::io_error); + }); } return std::unexpected {std::make_error_code(std::errc::not_supported)};