Action Engine
Loading...
Searching...
No Matches
conversion.h
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ACTIONENGINE_DATA_CONVERSION_H_
16#define ACTIONENGINE_DATA_CONVERSION_H_
17
18#include <absl/base/nullability.h>
19#include <absl/log/log.h>
20#include <absl/status/status.h>
21#include <absl/status/statusor.h>
22#include <absl/strings/str_cat.h>
23
24namespace act {
25
26template <typename T>
27absl::Status EgltAssignInto(T&& from, std::string* absl_nonnull to) {
28 *to = absl::StrCat(std::forward<T>(from));
29 return absl::OkStatus();
30}
31
32template <typename Dst, typename Src>
33absl::Status Assign(Src&& from, Dst* absl_nonnull to) {
34 return EgltAssignInto(std::forward<Src>(from), to);
35}
36
37template <typename Dst, typename Src>
38void AssignOrDie(Src&& from, Dst* absl_nonnull to) {
39 if (const absl::Status status = Assign(std::forward<Src>(from), to);
40 !status.ok()) {
41 LOG(FATAL) << "Conversion failed: " << status;
42 ABSL_ASSUME(false);
43 }
44}
45
46template <typename Dst, typename Src>
47absl::StatusOr<Dst> ConvertTo(Src&& from) {
48 Dst result;
49 if (auto status = Assign(std::forward<Src>(from), &result); !status.ok()) {
50 return status;
51 }
52 return result;
53}
54
55template <typename Dst, typename Src>
56Dst ConvertToOrDie(Src&& from) {
57 if (auto result = ConvertTo<Dst>(std::forward<Src>(from)); !result.ok()) {
58 LOG(FATAL) << "Conversion failed: " << result.status();
59 ABSL_ASSUME(false);
60 } else {
61 return *result;
62 }
63}
64
65} // namespace act
66
67#endif // ACTIONENGINE_DATA_CONVERSION_H_