From 753fc1661d9b5275ee7048353872f10bcd9c2953 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 9 May 2023 13:05:38 -0400 Subject: [PATCH] Cleanups to content address types --- src/libstore/content-address.cc | 14 +++++++++----- src/libstore/content-address.hh | 23 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/libstore/content-address.cc b/src/libstore/content-address.cc index 167f7af6c..b62818e11 100644 --- a/src/libstore/content-address.cc +++ b/src/libstore/content-address.cc @@ -21,7 +21,8 @@ std::string makeFileIngestionPrefix(FileIngestionMethod m) } } -std::string ContentAddressMethod::renderPrefix() const { +std::string ContentAddressMethod::renderPrefix() const +{ return std::visit(overloaded { [](TextIngestionMethod) -> std::string { return "text:"; }, [](FileIngestionMethod m2) { @@ -113,7 +114,8 @@ static std::pair parseContentAddressMethodPrefix throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix); } -ContentAddress ContentAddress::parse(std::string_view rawCa) { +ContentAddress ContentAddress::parse(std::string_view rawCa) +{ auto rest = rawCa; auto [caMethod, hashType_] = parseContentAddressMethodPrefix(rest); @@ -155,7 +157,7 @@ std::string renderContentAddress(std::optional ca) } ContentAddress ContentAddress::fromParts( - ContentAddressMethod method, Hash hash) + ContentAddressMethod method, Hash hash) noexcept { return std::visit(overloaded { [&](TextIngestionMethod _) -> ContentAddress { @@ -196,7 +198,8 @@ const Hash & ContentAddress::getHash() const }, raw); } -std::string ContentAddress::printMethodAlgo() const { +std::string ContentAddress::printMethodAlgo() const +{ return getMethod().renderPrefix() + printHashType(getHash().type); } @@ -211,7 +214,8 @@ size_t StoreReferences::size() const return (self ? 1 : 0) + others.size(); } -ContentAddressWithReferences ContentAddressWithReferences::withoutRefs(const ContentAddress & ca) { +ContentAddressWithReferences ContentAddressWithReferences::withoutRefs(const ContentAddress & ca) noexcept +{ return std::visit(overloaded { [&](const TextHash & h) -> ContentAddressWithReferences { return TextInfo { diff --git a/src/libstore/content-address.hh b/src/libstore/content-address.hh index 4183ac1e7..03a2e2a23 100644 --- a/src/libstore/content-address.hh +++ b/src/libstore/content-address.hh @@ -81,17 +81,30 @@ struct ContentAddressMethod * Parse the prefix tag which indicates how the files * were ingested, with the fixed output case not prefixed for back * compat. + * + * @param [in] m A string that should begin with the prefix. + * @param [out] m The remainder of the string after the prefix. */ static ContentAddressMethod parsePrefix(std::string_view & m); + /** + * Render the prefix tag which indicates how the files wre ingested. + * + * The rough inverse of `parsePrefix()`. + */ std::string renderPrefix() const; /** - * Parse and pretty print a content addressing method and hash type in a - * nicer way, prefixing both cases. + * Parse a content addressing method and hash type. */ static std::pair parse(std::string_view rawCaMethod); + /** + * Render a content addressing method and hash type in a + * nicer way, prefixing both cases. + * + * The rough inverse of `parse()`. + */ std::string render(HashType ht) const; }; @@ -178,7 +191,7 @@ struct ContentAddress * @param hash Hash of ingested file system data. */ static ContentAddress fromParts( - ContentAddressMethod method, Hash hash); + ContentAddressMethod method, Hash hash) noexcept; ContentAddressMethod getMethod() const; @@ -187,6 +200,10 @@ struct ContentAddress std::string printMethodAlgo() const; }; +/** + * Render the `ContentAddress` if it exists to a string, return empty + * string otherwise. + */ std::string renderContentAddress(std::optional ca);