chromium/third_party/mediapipe/src/mediapipe/tasks/cc/common.h

/* Copyright 2022 The MediaPipe Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef MEDIAPIPE_TASKS_CC_COMMON_H_
#define MEDIAPIPE_TASKS_CC_COMMON_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"

namespace mediapipe {
namespace tasks {

// Name (aka type URL key) of the `absl::Status` payload which contains a
// stringified `MediaPipeTasksStatus` code (see below).
constexpr absl::string_view kMediaPipeTasksPayload =;

// Error codes for MediaPipe Tasks C++ APIs.
//
// At runtime, such codes are meant to be attached (where applicable) to a
// `absl::Status` in a key-value manner with `kMediaPipeTasksPayload` as key and
// stringified error code as value (aka payload). This logic is encapsulated in
// the `CreateStatusWithPayload` helper below for convenience.
//
// The returned status includes:
// 1. The canonical error code (INVALID_ARGUMENT)
// 2. The fine-grained error message ("Invalid metadata ...")
// 3. The specific status code as a payload (kMetadataInvalidSchemaVersionError)
enum class MediaPipeTasksStatus {};

// Convenience helper to create an `absl::Status` augmented with the
// fine-grained `mediapipe_tasks_code` attached as payload under the
// `kMediaPipeTasksPayload` type URL key.
//
// This should only be used for non-ok codes since otherwise it does nothing
// more than returning an object identical to an OK status. See `absl::Status`
// for more details.
absl::Status CreateStatusWithPayload(
    absl::StatusCode canonical_code, absl::string_view message,
    MediaPipeTasksStatus mediapipe_tasks_code = MediaPipeTasksStatus::kError);

// Attaches a new mediapipe tasks status payload to a non-ok status.
absl::Status AddPayload(
    absl::Status status, absl::string_view message,
    MediaPipeTasksStatus mediapipe_tasks_code = MediaPipeTasksStatus::kError);

}  // namespace tasks
}  // namespace mediapipe

#endif  // MEDIAPIPE_TASKS_CC_COMMON_H_