blob: 03f9fe4b2a60a06a2be8709f72e256dad257c242 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/fit/function.h>
#include <functional>
#include <string>
#include "src/ledger/bin/fidl/include/types.h"
#include "src/lib/fxl/strings/string_view.h"
namespace ledger {
namespace internal {
// Internal class to be able to take both kind of status.
class StatusTranslater {
// Implicit to be able to take both status in |QuitOnError| and
// |QuitOnErrorCallback|.
StatusTranslater(Status status);
StatusTranslater(CreateReferenceStatus status);
StatusTranslater(zx_status_t status);
StatusTranslater(const fuchsia::ledger::PageSnapshot_Get_Result& result);
const fuchsia::ledger::PageSnapshot_GetInline_Result& result);
StatusTranslater(const fuchsia::ledger::PageSnapshot_Fetch_Result& result);
const fuchsia::ledger::PageSnapshot_FetchPartial_Result& result);
bool ok() { return ok_; };
const std::string& description() { return description_; }
bool ok_;
std::string description_;
} // namespace internal
// Logs an error and calls |quit_callback| which quits a related message loop if
// the given ledger status is not Status::OK. Returns true if the loop
// was quit .
bool QuitOnError(fit::closure quit_callback, internal::StatusTranslater status,
fxl::StringView description);
inline auto QuitOnErrorCallback(fit::closure quit_callback,
std::string description) {
return [quit_callback = std::move(quit_callback),
description = std::move(description)](
internal::StatusTranslater status) mutable {
QuitOnError(quit_callback.share(), status, description);
} // namespace ledger