Cppinecone
A C++ client for the Pinecone vector database
curl_result.hpp
Go to the documentation of this file.
1 #pragma once
7 #include <functional>
8 #include <string>
9 #include <variant>
10 
11 #include <curl/curl.h>
12 
13 #include "pinecone/util/visit.hpp"
14 
15 namespace pinecone::util
16 {
20 struct [[nodiscard]] curl_result {
21  using error_type = std::variant<CURLcode, curl_slist*>;
22 
23  constexpr curl_result() noexcept = default;
24 
29  // NOLINTNEXTLINE
30  constexpr curl_result(CURLcode code) noexcept
31  {
32  if (code != CURLE_OK) {
33  _value = code;
34  } else {
35  _value = {};
36  }
37  }
38 
43  // NOLINTNEXTLINE
44  constexpr curl_result(curl_slist* list) noexcept
45  {
46  if (list == nullptr) {
47  _value = list;
48  } else {
49  _value = {};
50  }
51  }
52 
56  [[nodiscard]] constexpr auto is_success() const noexcept -> bool { return _value.index() == 0; }
57 
61  [[nodiscard]] constexpr auto is_error() const noexcept -> bool { return !is_success(); }
62 
69  [[nodiscard]] constexpr auto error() const noexcept -> error_type
70  {
71  return std::get<error_type>(_value);
72  }
73 
80  constexpr auto and_then(std::function<curl_result()> const& func) const noexcept -> curl_result
81  {
82  if (is_error()) {
83  return *this;
84  }
85 
86  return func();
87  }
88 
95  static auto to_string(error_type const& err) noexcept -> std::string
96  {
97  return std::visit(
98  util::overloaded{[](CURLcode arg) { return std::to_string(arg); },
99  [](curl_slist*) -> std::string { return "Bad header list"; }},
100  err);
101  }
102 
110  [[nodiscard]] auto to_string() const noexcept -> std::string
111  {
112  switch (_value.index()) {
113  case 0:
114  return "Success";
115  case 1:
116  return to_string(error());
117  default:
118  return "Unknown";
119  }
120  }
121 
122  private:
123  std::variant<std::monostate, error_type> _value;
124 };
125 
126 } // namespace pinecone::util
STL namespace.
Models the possibly of failure for remote HTTP operations.
Definition: curl_result.hpp:20
constexpr auto and_then(std::function< curl_result()> const &func) const noexcept -> curl_result
Runs a transformation function on a successful result; does nothing on a failed result.
Definition: curl_result.hpp:80
constexpr auto error() const noexcept -> error_type
Retrieves the error type associated with a failed operation. Cannot be called on a successful result.
Definition: curl_result.hpp:69
constexpr curl_result(CURLcode code) noexcept
Constructs a result from a CURL error code.
Definition: curl_result.hpp:30
constexpr auto is_error() const noexcept -> bool
Definition: curl_result.hpp:61
constexpr auto is_success() const noexcept -> bool
Definition: curl_result.hpp:56
auto to_string() const noexcept -> std::string
Retrieves the string representation of the current instance.
constexpr curl_result(curl_slist *list) noexcept
Constructs a result from a CURL header list.
Definition: curl_result.hpp:44
static auto to_string(error_type const &err) noexcept -> std::string
Retrieves the string representation of a failed operation.
Definition: curl_result.hpp:95
Basic visitor operations.
Definition: visit.hpp:15
T to_string(T... args)
Simple visitor implementation.