9#include <grpcpp/support/status.h>
11#include <userver/utils/assert.hpp>
13USERVER_NAMESPACE_BEGIN
20template <
typename Response>
24 Result(Response&& response) : result_{std::move(response)} {}
27 Result(grpc::Status&& status) : result_{std::move(status)} {
28 UINVARIANT(!GetErrorStatus().ok(),
"Only error status allowed");
32 Result(
const grpc::Status& status) : result_{status} {
33 UINVARIANT(!GetErrorStatus().ok(),
"Only error status allowed");
37 bool IsSuccess()
const {
return std::holds_alternative<Response>(result_); }
39 Response&& ExtractResponse() && {
40 UINVARIANT(IsSuccess(),
"ExtractResponse allowed only in success state");
41 return std::get<Response>(std::move(result_));
44 const Response& GetResponse()
const {
45 UINVARIANT(IsSuccess(),
"GetResponse allowed only in success state");
46 return std::get<Response>(result_);
49 const grpc::Status& GetErrorStatus()
const {
50 UINVARIANT(!IsSuccess(),
"GetErrorStatus allowed only in error state");
51 return std::get<grpc::Status>(result_);
56 std::variant<Response, grpc::Status> result_;
60template <
typename Response>
61class StreamingResult final {
73 StreamingResult(Response&& last_response) : last_response_(std::move(last_response)) {}
76 bool HasLastResponse()
const {
return last_response_.has_value(); }
78 Response&& ExtractLastResponse() && {
return std::move(last_response_).value(); }
80 const Response& GetLastResponse()
const {
return last_response_.value(); }
82 const grpc::Status& GetStatus()
const {
return status_; }
86 std::optional<Response> last_response_;
87 grpc::Status status_{grpc::Status::OK};