cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <string>
6#include <string_view>
7#include <tl/expected.hpp>
8#include <type_traits>
9#include <utility>
10#include <variant>
11
19
20namespace mcp::core {
21
23template <class E>
24constexpr auto unexpected(E&& value) {
25 return tl::unexpected<std::decay_t<E> >(std::forward<E>(value));
26}
27
35struct Error {
37 int code = 0;
38
40 std::string message;
41
43 std::string detail;
44
48 std::string category;
49};
50
55using Unit = std::monostate;
56
63template <typename T>
64using Result = tl::expected<T, Error>;
65
67inline bool starts_with(std::string_view value, std::string_view prefix) {
68 return value.size() >= prefix.size() &&
69 value.substr(0, prefix.size()) == prefix;
70}
71
73inline bool starts_with(std::string_view value, char prefix) {
74 return !value.empty() && value.front() == prefix;
75}
76
78inline bool ends_with(std::string_view value, std::string_view suffix) {
79 return value.size() >= suffix.size() &&
80 value.substr(value.size() - suffix.size()) == suffix;
81}
82
84inline bool ends_with(std::string_view value, char suffix) {
85 return !value.empty() && value.back() == suffix;
86}
87
88} // namespace mcp::core
bool starts_with(std::string_view value, std::string_view prefix)
Compatibility helper matching std::string_view::starts_with.
Definition result.hpp:67
std::monostate Unit
Success value for operations that only need to report failure.
Definition result.hpp:55
bool ends_with(std::string_view value, std::string_view suffix)
Compatibility helper matching std::string_view::ends_with.
Definition result.hpp:78
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
constexpr auto unexpected(E &&value)
Creates an unexpected result value for the active expected backend.
Definition result.hpp:24
Structured error returned by fallible SDK operations.
Definition result.hpp:35
std::string detail
Optional extended diagnostic information.
Definition result.hpp:43
std::string category
Stable SDK error category such as "protocol", "transport", "handler", "timeout", or "cancellation".
Definition result.hpp:48
std::string message
Short human-readable explanation of the failure.
Definition result.hpp:40
int code
Numeric protocol, transport, or component-specific error code.
Definition result.hpp:37