/src/jsoncons/include/jsoncons/read_result.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /// Copyright 2013-2025 Daniel Parker |
2 | | // Distributed under the Boost license, Version 1.0. |
3 | | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
4 | | |
5 | | // See https://github.com/danielaparker/jsoncons2 for latest version |
6 | | |
7 | | #ifndef JSONCONS_READ_RESULT_HPP |
8 | | #define JSONCONS_READ_RESULT_HPP |
9 | | |
10 | | #include <system_error> |
11 | | #include <type_traits> |
12 | | #include <jsoncons/config/jsoncons_config.hpp> |
13 | | #include <jsoncons/json_exception.hpp> |
14 | | |
15 | | namespace jsoncons { |
16 | | |
17 | | class read_error |
18 | | { |
19 | | std::error_code ec_{}; |
20 | | std::string message_arg_; |
21 | | std::size_t line_{}; |
22 | | std::size_t column_{}; |
23 | | |
24 | | public: |
25 | | read_error(std::error_code ec, std::size_t line, std::size_t column) |
26 | 16.6k | : ec_{ec}, line_{line}, column_{column} |
27 | 16.6k | { |
28 | 16.6k | } |
29 | | |
30 | | read_error(std::error_code ec, const std::string& message_arg, std::size_t line, std::size_t column) |
31 | | : ec_{ec}, message_arg_(message_arg), line_{line}, column_{column} |
32 | 0 | { |
33 | 0 | } |
34 | | |
35 | | read_error(const read_error& other) = default; |
36 | | |
37 | 16.6k | read_error(read_error&& other) = default; |
38 | | |
39 | | read_error& operator=(const read_error& other) = default; |
40 | | |
41 | | read_error& operator=(read_error&& other) = default; |
42 | | |
43 | | const std::error_code& code() const noexcept |
44 | 16.6k | { |
45 | 16.6k | return ec_; |
46 | 16.6k | } |
47 | | const std::string& message_arg() const noexcept |
48 | 0 | { |
49 | 0 | return message_arg_; |
50 | 0 | } |
51 | | std::size_t line() const noexcept |
52 | 16.6k | { |
53 | 16.6k | return line_; |
54 | 16.6k | } |
55 | | std::size_t column() const noexcept |
56 | 16.6k | { |
57 | 16.6k | return column_; |
58 | 16.6k | } |
59 | | }; |
60 | | |
61 | | inline |
62 | | std::string to_string(const read_error& err) |
63 | 0 | { |
64 | 0 | std::string str(err.message_arg()); |
65 | 0 | if (!str.empty()) |
66 | 0 | { |
67 | 0 | str.append(": "); |
68 | 0 | } |
69 | 0 | str.append(err.code().message()); |
70 | 0 | if (err.line() != 0 && err.column() != 0) |
71 | 0 | { |
72 | 0 | str.append(" at line "); |
73 | 0 | str.append(std::to_string(err.line())); |
74 | 0 | str.append(" and column "); |
75 | 0 | str.append(std::to_string(err.column())); |
76 | 0 | } |
77 | 0 | else if (err.column() != 0) |
78 | 0 | { |
79 | 0 | str.append(" at position "); |
80 | 0 | str.append(std::to_string(err.column())); |
81 | 0 | } |
82 | 0 | return str; |
83 | 0 | } |
84 | | |
85 | | inline |
86 | | std::ostream& operator<<(std::ostream& os, const read_error& err) |
87 | 0 | { |
88 | 0 | os << to_string(err); |
89 | 0 | return os; |
90 | 0 | } |
91 | | |
92 | | template <typename T> |
93 | | using read_result = result<T,read_error>; |
94 | | |
95 | | } // jsoncons |
96 | | |
97 | | #endif // JSONCONS_READ_RESULT_HPP |