sapling/eden/fs/model/RootId.h
Chad Austin 0081eb6c26 fix the custom PathFuncs and ObjectId formatters
Summary:
I don't know the difference between our prior magic incantations and
the new magic incantations, but the old ones crazy errors when writing
variadic wrappers that eventually call into fmt::format or fmt::join.

Reviewed By: vitaut

Differential Revision: D39991902

fbshipit-source-id: 8ca3215267912b3bb0ddf586681d3eb5294f52ff
2022-10-04 21:42:44 -07:00

92 lines
2.3 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <string>
#include "eden/fs/model/Hash.h"
namespace facebook::eden {
/**
* Each BackingStore implementation defines the meaning of its root. For
* example, for Mercurial, that's a 20-byte commit hash. Roots may have a
* different representation than tree IDs, so allow the BackingStore to define
* the semantics.
*
* RootId should generally be human-readable (e.g. hex strings) because it is
* printed to logs with C escaping rules.
*/
class RootId {
public:
RootId() = default;
explicit RootId(std::string id) : id_{std::move(id)} {}
RootId(const RootId&) = default;
RootId(RootId&&) = default;
RootId& operator=(const RootId&) = default;
RootId& operator=(RootId&&) = default;
const std::string& value() const {
return id_;
}
friend bool operator==(const RootId& lhs, const RootId& rhs) {
return lhs.id_ == rhs.id_;
}
friend bool operator!=(const RootId& lhs, const RootId& rhs) {
return lhs.id_ != rhs.id_;
}
friend bool operator<(const RootId& lhs, const RootId& rhs) {
return lhs.id_ < rhs.id_;
}
private:
std::string id_;
};
std::ostream& operator<<(std::ostream& os, const RootId& rootId);
// Make folly::to<string>(RootId) work.
void toAppend(const RootId&, std::string* result);
/**
* The meaning of a RootId is defined by the BackingStore implementation. Allow
* it to also define how root IDs are parsed and rendered at API boundaries such
* as Thrift.
*/
class RootIdCodec {
public:
virtual ~RootIdCodec() = default;
virtual RootId parseRootId(folly::StringPiece rootId) = 0;
virtual std::string renderRootId(const RootId& rootId) = 0;
};
} // namespace facebook::eden
namespace std {
template <>
struct hash<facebook::eden::RootId> {
std::size_t operator()(const facebook::eden::RootId& rootId) const {
return std::hash<std::string>{}(rootId.value());
}
};
} // namespace std
template <>
struct fmt::formatter<facebook::eden::RootId> : formatter<std::string> {
template <typename Context>
auto format(const facebook::eden::RootId& id, Context& ctx) const {
return formatter<std::string>::format(id.value(), ctx);
}
};