11[[nodiscard]]
inline std::string
located_message(uint64_t location, std::string_view message)
13 return "row " + std::to_string(location) +
": " + std::string(message);
37template <
typename Source,
typename Access = RecordAccess<Source>>
51 bool claim(
const Source& source, std::string& error)
53 const auto value = Access::attribute(source,
m_attribute);
54 if (!value || value->empty()) {
56 "<" + std::string(Access::name(source)) +
"> needs a non-empty " +
m_attribute);
59 const auto [slot, inserted] =
m_claimed.emplace(std::string(*value), Access::location(source));
62 m_attribute +
" " + std::string(*value) +
" is already used at row "
63 + std::to_string(slot->second));
69 [[nodiscard]]
bool contains(std::string_view value)
const
75 [[nodiscard]] std::optional<uint64_t>
location_of(std::string_view value)
const
87 std::map<std::string, uint64_t, std::less<>>
m_claimed;
100template <
typename Source,
typename Access = RecordAccess<Source>,
typename Exists>
102 std::string_view attribute, Exists&& exists, std::string& error)
104 const auto value = Access::attribute(source, attribute);
107 "<" + std::string(Access::name(source)) +
"> needs a " + std::string(attribute) +
" attribute");
110 if (!std::invoke(std::forward<Exists>(exists), *value)) {
112 "<" + std::string(Access::name(source)) +
"> refers to unknown " + std::string(attribute)
113 +
" " + std::string(*value));
AttributeIndex(std::string attribute)
bool contains(std::string_view value) const
std::map< std::string, uint64_t, std::less<> > m_claimed
std::optional< uint64_t > location_of(std::string_view value) const
The location of the element that holds a value, or nullopt.
const std::string & attribute() const noexcept
bool claim(const Source &source, std::string &error)
Claim the attribute's value for an element.
std::string located_message(uint64_t location, std::string_view message)
A diagnostic message prefixed with the location of the element it concerns.
std::optional< std::string_view > check_reference(const Source &source, std::string_view attribute, Exists &&exists, std::string &error)
Check that an element's attribute names a value that exists.