Skip to content
Merged
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
27 changes: 27 additions & 0 deletions base/cvd/cuttlefish/pretty/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("//cuttlefish/bazel:rules.bzl", "cf_cc_library", "cf_cc_test")

package(
default_visibility = ["//:android_cuttlefish"],
)

cf_cc_library(
name = "struct",
srcs = ["struct.cc"],
hdrs = ["struct.h"],
deps = [
"@abseil-cpp//absl/strings",
"@abseil-cpp//absl/strings:str_format",
],
)

cf_cc_test(
name = "struct_test",
srcs = ["struct_test.cc"],
deps = [
"//cuttlefish/pretty:struct",
"@abseil-cpp//absl/strings",
"@fmt",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)
73 changes: 73 additions & 0 deletions base/cvd/cuttlefish/pretty/struct.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Copyright (C) 2026 The Android Open Source Project
//
// 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 "cuttlefish/pretty/struct.h"

#include <ostream>
#include <string_view>

#include "absl/strings/str_format.h"
#include "absl/strings/str_replace.h"

namespace cuttlefish {

PrettyStruct::PrettyStruct(std::string_view name) : name_(name) {}

PrettyStruct& PrettyStruct::Member(std::string_view name,
std::string_view value) & {
MemberInternal(absl::StrCat(name, ": \"", value, "\""));
return *this;
}

PrettyStruct PrettyStruct::Member(std::string_view name,
std::string_view value) && {
Member(name, value);
return *this;
}

PrettyStruct& PrettyStruct::Member(std::string_view name, const char* value) & {
Member(name, std::string_view(value));
return *this;
}

PrettyStruct PrettyStruct::Member(std::string_view name, const char* value) && {
Member(name, std::string_view(value));
return *this;
}

PrettyStruct& PrettyStruct::Member(std::string_view name,
const std::string& value) & {
Member(name, std::string_view(value));
return *this;
}

PrettyStruct PrettyStruct::Member(std::string_view name,
const std::string& value) && {
Member(name, std::string_view(value));
return *this;
}

void PrettyStruct::MemberInternal(std::string_view line) {
members_.emplace_back(absl::StrReplaceAll(line, {{"\n", "\n "}}));
}

std::ostream& operator<<(std::ostream& out, const PrettyStruct& ps) {
return out << absl::StreamFormat("%v", ps);
}

// For libfmt
std::string format_as(const PrettyStruct& ps) { return absl::StrCat(ps); }

} // namespace cuttlefish
102 changes: 102 additions & 0 deletions base/cvd/cuttlefish/pretty/struct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Copyright (C) 2026 The Android Open Source Project
//
// 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.

#pragma once

#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"

namespace cuttlefish {

/**
* Creates a "formatted struct", comparable to Rust's std::fmt::DebugStruct.
*
* Supports abseil string formatting, libfmt, and ostreams.
*
* Example usage:
* ```
* const PrettyStruct inner =
* PrettyStruct("Inner").Member("i1", 1).Member("i2", 2);
* const PrettyStruct outer =
* PrettyStruct("Outer").Member("o1", inner).Member("o2", inner);
* ```
* formats as
* ```
* Outer {
* o1: Inner {
* i1: 1,
* i2: 2
* },
* o2: Inner {
* i1: 1,
* i2: 2
* }
* }
* ```
*/
class PrettyStruct {
public:
explicit PrettyStruct(std::string_view name);

template <typename T>
PrettyStruct& Member(std::string_view name, const T& value) & {
MemberInternal(absl::StrCat(name, ": ", value));
return *this;
}

template <typename T>
PrettyStruct Member(std::string_view name, const T& value) && {
this->Member(name, value);
return std::move(*this);
}

// String members are quoted
PrettyStruct& Member(std::string_view name, std::string_view value) &;
PrettyStruct Member(std::string_view name, std::string_view value) &&;
PrettyStruct& Member(std::string_view name, const char* value) &;
PrettyStruct Member(std::string_view name, const char* value) &&;
PrettyStruct& Member(std::string_view name, const std::string& value) &;
PrettyStruct Member(std::string_view name, const std::string& value) &&;

template <typename Sink>
friend void AbslStringify(Sink& sink, const PrettyStruct& ps) {
if (ps.members_.empty()) {
absl::Format(&sink, "%v {}", ps.name_);
} else {
absl::Format(&sink, "%v {\n %v\n}", ps.name_,
absl::StrJoin(ps.members_, ",\n "));
}
}

private:
void MemberInternal(std::string_view);

std::string name_;
std::vector<std::string> members_;
};

std::ostream& operator<<(std::ostream&, const PrettyStruct&);

// For libfmt
std::string format_as(const PrettyStruct&);

} // namespace cuttlefish
89 changes: 89 additions & 0 deletions base/cvd/cuttlefish/pretty/struct_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* 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 "cuttlefish/pretty/struct.h"

#include <string_view>

#include "absl/strings/ascii.h"
#include "fmt/format.h"
#include "gtest/gtest.h"

namespace cuttlefish {
namespace {

void ExpectFormatsTo(const PrettyStruct& ps, const std::string_view expected) {
const std::string_view trimmed = absl::StripAsciiWhitespace(expected);

EXPECT_EQ(absl::StrCat(ps), trimmed);
EXPECT_EQ(fmt::format("{}", ps), trimmed);

std::stringstream sstream;
sstream << ps;
EXPECT_EQ(sstream.str(), trimmed);
}

} // namespace

TEST(PrettyStruct, Empty) {
ExpectFormatsTo(PrettyStruct("Empty"), "Empty {}");
}

TEST(PrettyStruct, OneMember) {
ExpectFormatsTo(PrettyStruct("Pretty").Member("member", 5), R"(
Pretty {
member: 5
}
)");
}

TEST(PrettyStruct, StringMember) {
ExpectFormatsTo(PrettyStruct("Pretty").Member("member", "value"), R"(
Pretty {
member: "value"
}
)");
}

TEST(PrettyStruct, TwoMembers) {
ExpectFormatsTo(
PrettyStruct("Pretty").Member("member_a", 5).Member("member_b", 6), R"(
Pretty {
member_a: 5,
member_b: 6
}
)");
}

TEST(PrettyStruct, NestedMembers) {
const PrettyStruct inner =
PrettyStruct("Inner").Member("i1", 1).Member("i2", 2);
ExpectFormatsTo(PrettyStruct("Outer").Member("o1", inner).Member("o2", inner),
R"(
Outer {
o1: Inner {
i1: 1,
i2: 2
},
o2: Inner {
i1: 1,
i2: 2
}
}
)");
}

} // namespace cuttlefish
Loading