Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions openfst/compat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ cc_test(

cc_library(
name = "status_builder",
srcs = ["status_builder.cc"],
hdrs = ["status_builder.h"],
visibility = ["//visibility:private"],
deps = [
Expand All @@ -73,6 +74,20 @@ cc_library(
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:source_location",
],
)

cc_test(
name = "status_builder_test",
srcs = ["status_builder_test.cc"],
linkstatic = True,
deps = [
":status_builder",
"//openfst/test:test_main",
"@com_google_absl//absl/status",
"@com_google_absl//absl/types:source_location",
"@com_google_googletest//:gtest_main",
],
)

Expand All @@ -83,6 +98,8 @@ cc_library(
":status_builder",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/types:source_location",
],
)

Expand Down
1 change: 1 addition & 0 deletions openfst/compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if(OPENFST_BUILD_TESTS)
set(COMPAT_TESTS
compat_memory_test
file_path_test
status_builder_test
status_macros_test
status_matchers_test
init_test
Expand Down
148 changes: 148 additions & 0 deletions openfst/compat/status_builder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2026 The OpenFst 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.

#include "openfst/compat/status_builder.h"

#include <memory>
#include <sstream>
#include <utility>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/types/source_location.h"

namespace fst {

StatusBuilder::StatusBuilder(const StatusBuilder& sb)
: impl_(sb.impl_ ? std::make_unique<Impl>(*sb.impl_) : nullptr) {}

StatusBuilder& StatusBuilder::operator=(const StatusBuilder& sb) {
if (!sb.impl_) {
impl_ = nullptr;
return *this;
}
if (impl_) {
*impl_ = *sb.impl_;
return *this;
}
impl_ = std::make_unique<Impl>(*sb.impl_);

return *this;
}

StatusBuilder& StatusBuilder::SetAppend() & {
if (!impl_) return *this;
impl_->join_style = Impl::MessageJoinStyle::kAppend;
return *this;
}

StatusBuilder&& StatusBuilder::SetAppend() && { return std::move(SetAppend()); }

StatusBuilder& StatusBuilder::SetPrepend() & {
if (!impl_) return *this;
impl_->join_style = Impl::MessageJoinStyle::kPrepend;
return *this;
}

StatusBuilder&& StatusBuilder::SetPrepend() && {
return std::move(SetPrepend());
}

StatusBuilder& StatusBuilder::SetNoLogging() & {
if (!impl_) return *this;
impl_->no_logging = true;
return *this;
}

StatusBuilder&& StatusBuilder::SetNoLogging() && {
return std::move(SetNoLogging());
}

StatusBuilder&& StatusBuilder::SetCode(absl::StatusCode code) && {
return std::move(SetCode(code));
}

StatusBuilder& StatusBuilder::SetCode(absl::StatusCode code) & {
if (!impl_) return *this;
impl_->status = absl::Status(code, impl_->status.message());
return *this;
}

StatusBuilder::operator absl::Status() const& {
return StatusBuilder(*this).JoinMessageToStatus();
}

StatusBuilder::operator absl::Status() && { return JoinMessageToStatus(); }

absl::Status StatusBuilder::JoinMessageToStatus() {
if (!impl_) {
return absl::OkStatus();
}
return impl_->JoinMessageToStatus();
}

absl::Status StatusBuilder::Impl::JoinMessageToStatus() {
if (stream.str().empty() || no_logging) {
return status;
}
::absl::Status result;
if (status.message().empty()) {
result = absl::Status(status.code(), stream.str());
} else {
switch (join_style) {
case MessageJoinStyle::kAnnotate:
result = absl::Status(status.code(), absl::StrCat(status.message(),
"; ", stream.str()));
break;
case MessageJoinStyle::kAppend:
result = absl::Status(status.code(),
absl::StrCat(status.message(), stream.str()));
break;
case MessageJoinStyle::kPrepend:
result = absl::Status(status.code(),
absl::StrCat(stream.str(), status.message()));
break;
}
}
status.ForEachPayload([&](auto type_url, auto payload) {
result.SetPayload(std::move(type_url), std::move(payload));
});
return result;
}

StatusBuilder::Impl::Impl(const absl::Status& status,
absl::SourceLocation location)
: status(status), location(location), stream() {}

StatusBuilder::Impl::Impl(absl::Status&& status, absl::SourceLocation location)
: status(std::move(status)), location(location), stream() {}

StatusBuilder::Impl::Impl(const Impl& other)
: status(other.status),
location(other.location),
no_logging(other.no_logging),
stream(other.stream.str()),
join_style(other.join_style) {}

StatusBuilder::Impl& StatusBuilder::Impl::operator=(const Impl& other) {
status = other.status;
location = other.location;
no_logging = other.no_logging;
stream = std::ostringstream(other.stream.str());
join_style = other.join_style;

return *this;
}

} // namespace fst
Loading
Loading