Skip to content

Commit

Permalink
Add error code enum
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Oct 8, 2024
1 parent 97a48f4 commit f296827
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
9 changes: 5 additions & 4 deletions source/base/lib/inc/tactile/base/container/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#pragma once

#include <expected> // expected
#include <system_error> // error_code
#include <expected> // expected

#include "tactile/base/debug/error_code.hpp"

namespace tactile {

template <typename T>
using Result = std::expected<T, std::error_code>;
using Result = std::expected<T, ErrorCode>;

inline const Result<void> kOK {};
inline constexpr Result<void> kOK {};

} // namespace tactile
88 changes: 88 additions & 0 deletions source/base/lib/inc/tactile/base/debug/error_code.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include <string_view> // 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
4 changes: 2 additions & 2 deletions source/godot_tscn_format/lib/src/gd3_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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};
}
}

Expand Down
4 changes: 3 additions & 1 deletion source/godot_tscn_format/lib/src/godot_scene_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
Expand Down

0 comments on commit f296827

Please sign in to comment.