diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt new file mode 100644 index 0000000..ad11fbc --- /dev/null +++ b/core/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 4.1) + +project(config-core LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +add_library(config-core + src/error.cpp) + +target_include_directories(config-core PUBLIC include) +target_compile_features(config-core PUBLIC cxx_std_20) + +if (MSVC) + target_compile_options(config-core PRIVATE /W4) +else() + target_compile_options(config-core PRIVATE + -Wall + -Wextra + -Wpedantic + ) +endif() \ No newline at end of file diff --git a/core/include/config/error.h b/core/include/config/error.h new file mode 100644 index 0000000..ad7ac5a --- /dev/null +++ b/core/include/config/error.h @@ -0,0 +1,54 @@ +#pragma once + +#include +#include + +namespace config { + + enum class error_reason { + InvalidSyntax, + InvalidType, + MissingField, + UnknownField, + InvalidValue, + ValidationFailed, + InternalError + }; + + enum class error_category { + Parse, + Type, + Validation, + Internal + }; + + struct error_location { + std::size_t line = 0; + std::size_t column = 0; + }; + + class error { + public: + + error(error_reason reason, error_category category, const std::string &message, error_location location) noexcept; + ~error() noexcept = default; + + [[nodiscard]] + error_reason reason() const noexcept; + + [[nodiscard]] + error_category category() const noexcept; + + [[nodiscard]] + std::string message() const noexcept; + + [[nodiscard]] + error_location location() const noexcept; + + protected: + error_reason reason_; + error_category category_; + std::string message_; + error_location location_; + }; +} diff --git a/core/include/config/exception.hpp b/core/include/config/exception.hpp new file mode 100644 index 0000000..4983527 --- /dev/null +++ b/core/include/config/exception.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include "error.h" + +namespace config { + + class config_exception : public std::runtime_error { + public: + + explicit config_exception(error error) + : std::runtime_error(error.message().data()), error_(std::move(error)) {} + + [[nodiscard]] + const error& original_error() const noexcept { + return error_; + } + + private: + error error_; + }; +} \ No newline at end of file diff --git a/core/src/error.cpp b/core/src/error.cpp new file mode 100644 index 0000000..7f766ff --- /dev/null +++ b/core/src/error.cpp @@ -0,0 +1,27 @@ +#include "config/error.h" + +namespace config { + + error::error(const error_reason reason, const error_category category, const std::string &message, const error_location location) noexcept { + reason_ = reason; + category_ = category; + message_ = message; + location_ = location; + } + + error_reason error::reason() const noexcept { + return reason_; + } + + error_category error::category() const noexcept { + return category_; + } + + std::string error::message() const noexcept { + return message_; + } + + error_location error::location() const noexcept { + return location_; + } +}