diff --git a/include/sled/sled.h b/include/sled/sled.h index b034422..50273f8 100644 --- a/include/sled/sled.h +++ b/include/sled/sled.h @@ -10,6 +10,9 @@ #include "sled/filesystem/path.h" #include "sled/filesystem/temporary_file.h" +// lang +#include "lang/attributes.h" + // log #include "sled/log/log.h" diff --git a/include/sled/status.h b/include/sled/status.h index 9b62687..a157e8f 100644 --- a/include/sled/status.h +++ b/include/sled/status.h @@ -49,9 +49,7 @@ class ErrorInfo { public: ErrorInfo() = default; - explicit ErrorInfo(std::string reason, - std::string domain, - std::unordered_map metadata) + explicit ErrorInfo(std::string reason, std::string domain, std::unordered_map metadata) : reason_(std::move(reason)), domain_(std::move(domain)), metadata_(std::move(metadata)) @@ -61,18 +59,13 @@ public: std::string const &domain() const { return domain_; } - std::unordered_map const &metadata() const - { - return metadata_; - } + std::unordered_map const &metadata() const { return metadata_; } friend bool operator==(ErrorInfo const &, ErrorInfo const &); friend bool operator!=(ErrorInfo const &, ErrorInfo const &); private: - friend void internal::AddMetadata(ErrorInfo &, - std::string const &key, - std::string value); + friend void internal::AddMetadata(ErrorInfo &, std::string const &key, std::string value); std::string reason_; std::string domain_; std::unordered_map metadata_; diff --git a/include/sled/status_or.h b/include/sled/status_or.h index 2fb8d83..1b3caec 100644 --- a/include/sled/status_or.h +++ b/include/sled/status_or.h @@ -17,8 +17,7 @@ namespace sled { template class StatusOr final { public: - static_assert(!std::is_reference::value, - "StatusOr requires T to **not** be a reference type"); + static_assert(!std::is_reference::value, "StatusOr requires T to **not** be a reference type"); using value_type = T; StatusOr() : StatusOr(MakeDefaultStatus()) {} @@ -26,9 +25,7 @@ public: StatusOr(StatusOr const &) = default; StatusOr &operator=(StatusOr const &) = default; - StatusOr(StatusOr &&other) - : status_(std::move(other.status_)), - value_(std::move(other.value_)) + StatusOr(StatusOr &&other) : status_(std::move(other.status_)), value_(std::move(other.value_)) { other.status_ = MakeDefaultStatus(); } @@ -43,10 +40,7 @@ public: StatusOr(Status rhs) : status_(std::move(rhs)) { - if (status_.ok()) { - throw std::invalid_argument( - "Status::OK is not a valid argument to StatusOr"); - } + if (status_.ok()) { throw std::invalid_argument("Status::OK is not a valid argument to StatusOr"); } } StatusOr &operator=(Status status) @@ -57,9 +51,7 @@ public: template::type>::value, - int>::type = 0 + typename std::enable_if::type>::value, int>::type = 0 /// @code end > StatusOr &operator=(U &&rhs) @@ -138,10 +130,7 @@ public: Status &&status() && { return std::move(status_); } private: - static Status MakeDefaultStatus() - { - return Status{StatusCode::kUnknown, "default"}; - } + static Status MakeDefaultStatus() { return Status{StatusCode::kUnknown, "default"}; } void CheckHasValue() const & { diff --git a/src/status.cc b/src/status.cc index 04ec0a3..8186fd9 100644 --- a/src/status.cc +++ b/src/status.cc @@ -44,8 +44,7 @@ StatusCodeToString(StatusCode code) case StatusCode::kDataLoss: return "DATA_LOSS"; default: - return "UNEXPECTED_STATUS_CODE=" - + std::to_string(static_cast(code)); + return "UNEXPECTED_STATUS_CODE=" + std::to_string(static_cast(code)); } } }// namespace internal @@ -59,8 +58,7 @@ operator<<(std::ostream &os, StatusCode code) bool operator==(ErrorInfo const &a, ErrorInfo const &b) { - return a.reason_ == b.reason_ && a.domain_ == b.domain_ - && a.metadata_ == b.metadata_; + return a.reason_ == b.reason_ && a.domain_ == b.domain_ && a.metadata_ == b.metadata_; } bool @@ -73,10 +71,7 @@ class Status::Impl { public: using PayloadType = std::unordered_map; - explicit Impl(StatusCode code, - std::string message, - ErrorInfo error_info, - PayloadType payload) + explicit Impl(StatusCode code, std::string message, ErrorInfo error_info, PayloadType payload) : code_(code), message_(std::move(message)), error_info_(std::move(error_info)), @@ -95,14 +90,11 @@ public: friend inline bool operator==(Impl const &a, Impl const &b) { - return a.code_ == b.code_ && a.message_ == b.message_ - && a.error_info_ == b.error_info_ && a.payload_ == b.payload_; + return a.code_ == b.code_ && a.message_ == b.message_ && a.error_info_ == b.error_info_ + && a.payload_ == b.payload_; } - friend inline bool operator!=(Impl const &a, Impl const &b) - { - return !(a == b); - } + friend inline bool operator!=(Impl const &a, Impl const &b) { return !(a == b); } private: StatusCode code_; @@ -118,9 +110,7 @@ Status::Status(Status &&) noexcept = default; Status &Status::operator=(Status &&) noexcept = default; // Deep copy -Status::Status(Status const &other) - : impl_(other.ok() ? nullptr : new auto(*other.impl_)) -{} +Status::Status(Status const &other) : impl_(other.ok() ? nullptr : new auto(*other.impl_)) {} // Deep copy Status & @@ -131,11 +121,7 @@ Status::operator=(Status const &other) } Status::Status(StatusCode code, std::string message, ErrorInfo error_info) - : impl_(code == StatusCode::kOk ? nullptr - : new Status::Impl(code, - std::move(message), - std::move(error_info), - {})) + : impl_(code == StatusCode::kOk ? nullptr : new Status::Impl(code, std::move(message), std::move(error_info), {})) {} StatusCode @@ -170,18 +156,30 @@ operator<<(std::ostream &os, const Status &s) if (s.ok()) return os << StatusCode::kOk; os << s.code() << ": " << s.message(); auto const &e = s.error_info(); - if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { - return os; + if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { return os; } + + os << "error_info={"; + bool first_field = true; + if (!e.reason().empty()) { + os << "reason=" << e.reason(); + first_field = false; } - os << "error_info={reason" << e.reason(); - os << ", domain=" << e.domain(); - os << ", metadata={"; - char const *sep = ""; - for (auto const &item : e.metadata()) { - os << sep << item.first << "=" << item.second; - sep = ", "; + if (!e.domain().empty()) { + if (first_field) os << ", "; + os << ", domain=" << e.domain(); + first_field = false; } - return os << "}}"; + if (!e.metadata().empty()) { + if (first_field) os << ", "; + os << "metadata={"; + char const *sep = ""; + for (auto const &item : e.metadata()) { + os << item.first << "=" << item.second << ", "; + sep = ", "; + } + os << "}"; + } + return os << "}"; } namespace internal {