Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
yaml_error.h
1#ifndef SOURCEMETA_CORE_YAML_ERROR_H_
2#define SOURCEMETA_CORE_YAML_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_YAML_EXPORT
5#include <sourcemeta/core/yaml_export.h>
6#endif
7
8#include <sourcemeta/core/error.h>
9
10#include <cstdint> // std::uint64_t
11#include <filesystem> // std::filesystem::path
12#include <string> // std::string
13#include <string_view> // std::string_view
14#include <utility> // std::move
15
16namespace sourcemeta::core {
17
18SOURCEMETA_CORE_ERROR_WARNINGS_PUSH
19
22class SOURCEMETA_CORE_YAML_EXPORT YAMLError : public Error {
23public:
26};
27
30class SOURCEMETA_CORE_YAML_EXPORT YAMLParseError : public Error {
31public:
33 YAMLParseError(const std::uint64_t line, const std::uint64_t column) noexcept
34 : Error{"Failed to parse the YAML document"}, line_{line},
35 column_{column} {}
36
38 YAMLParseError(const StaticString message, const std::uint64_t line,
39 const std::uint64_t column) noexcept
40 : Error{message}, line_{line}, column_{column} {}
41
43 [[nodiscard]] auto line() const noexcept -> std::uint64_t {
44 return this->line_;
45 }
46
48 [[nodiscard]] auto column() const noexcept -> std::uint64_t {
49 return this->column_;
50 }
51
52private:
53 std::uint64_t line_;
54 std::uint64_t column_;
55};
56
59class SOURCEMETA_CORE_YAML_EXPORT YAMLFileParseError : public YAMLParseError {
60public:
62 YAMLFileParseError(const StaticString message, std::filesystem::path path,
63 const std::uint64_t line, const std::uint64_t column)
64 : YAMLParseError{message, line, column}, path_{std::move(path)} {}
65
67 YAMLFileParseError(std::filesystem::path path, const YAMLParseError &parent)
68 : YAMLParseError{parent.message(), parent.line(), parent.column()},
69 path_{std::move(path)} {}
70
72 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
73 return this->path_;
74 }
75
76private:
77 std::filesystem::path path_;
78};
79
82class SOURCEMETA_CORE_YAML_EXPORT YAMLUnknownAnchorError
83 : public YAMLParseError {
84public:
86 YAMLUnknownAnchorError(const std::string_view anchor_name,
87 const std::uint64_t line, const std::uint64_t column)
88 : YAMLParseError{"YAML alias references undefined anchor", line, column},
89 anchor_name_{anchor_name} {}
90
92 [[nodiscard]] auto anchor() const noexcept -> std::string_view {
93 return this->anchor_name_;
94 }
95
96private:
97 std::string anchor_name_;
98};
99
104class SOURCEMETA_CORE_YAML_EXPORT YAMLDuplicateKeyError
105 : public YAMLParseError {
106public:
108 YAMLDuplicateKeyError(const std::string_view key_name,
109 const std::uint64_t line, const std::uint64_t column)
110 : YAMLParseError{"Duplicate key in YAML mapping", line, column},
111 key_name_{key_name} {}
112
114 [[nodiscard]] auto key() const noexcept -> std::string_view {
115 return this->key_name_;
116 }
117
118private:
119 std::string key_name_;
120};
121
122SOURCEMETA_CORE_ERROR_WARNINGS_POP
123
124} // namespace sourcemeta::core
125
126#endif
auto message() const noexcept -> StaticString
Definition error_base.h:78
Error(const StaticString message) noexcept
Construct the error given its message.
Definition error_base.h:84
Definition text_static_string.h:20
auto line() const noexcept -> std::uint64_t
Get the line number of the error.
Definition yaml_error.h:43
YAMLFileParseError(std::filesystem::path path, const YAMLParseError &parent)
Create a file parsing error from a parse error.
Definition yaml_error.h:67
YAMLFileParseError(const StaticString message, std::filesystem::path path, const std::uint64_t line, const std::uint64_t column)
Create a file parsing error.
Definition yaml_error.h:62
YAMLUnknownAnchorError(const std::string_view anchor_name, const std::uint64_t line, const std::uint64_t column)
Create an unknown anchor error.
Definition yaml_error.h:86
YAMLDuplicateKeyError(const std::string_view key_name, const std::uint64_t line, const std::uint64_t column)
Create a duplicate key error.
Definition yaml_error.h:108
auto anchor() const noexcept -> std::string_view
Get the anchor name of the error.
Definition yaml_error.h:92
YAMLError(const StaticString message) noexcept
Create a general error.
Definition yaml_error.h:25
YAMLParseError(const StaticString message, const std::uint64_t line, const std::uint64_t column) noexcept
Create a parsing error with a custom message.
Definition yaml_error.h:38
auto path() const noexcept -> const std::filesystem::path &
Get the file path of the error.
Definition yaml_error.h:72
YAMLParseError(const std::uint64_t line, const std::uint64_t column) noexcept
Create a parsing error.
Definition yaml_error.h:33
auto key() const noexcept -> std::string_view
Get the key name of the error.
Definition yaml_error.h:114
auto column() const noexcept -> std::uint64_t
Get the column number of the error.
Definition yaml_error.h:48