Skip to content

Commit

Permalink
Add helper function parseHashFormat[Opt] printHashFormat
Browse files Browse the repository at this point in the history
Add hash format analogy of
parseHashTypeOpt, parseHashType, and printHashType.

Co-authored-by: Valentin Gagarin <[email protected]>
  • Loading branch information
ShamrockLee and fricklerhandwerk committed Oct 18, 2023
1 parent 231b0fc commit 6b47635
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/libutil/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,41 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
}


std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName)
{
if (hashFormatName == "base16") return HashFormat::Base16;
if (hashFormatName == "base32") return HashFormat::Base32;
if (hashFormatName == "base64") return HashFormat::Base64;
if (hashFormatName == "sri") return HashFormat::SRI;
return std::nullopt;
}

HashFormat parseHashFormat(std::string_view hashFormatName)
{
auto opt_f = parseHashFormatOpt(hashFormatName);
if (opt_f)
return *opt_f;
throw UsageError("unknown hash format '%1%', expect 'base16', 'base32', 'base64', or 'sri'", hashFormatName);
}

std::string_view printHashFormat(HashFormat HashFormat)
{
switch (HashFormat) {
case HashFormat::Base64:
return "base64";
case HashFormat::Base32:
return "base32";
case HashFormat::Base16:
return "base16";
case HashFormat::SRI:
return "sri";
default:
// illegal hash base enum value internally, as opposed to external input
// which should be validated with nice error message.
assert(false);
}
}

std::optional<HashType> parseHashTypeOpt(std::string_view s)
{
if (s == "md5") return htMD5;
Expand Down
15 changes: 15 additions & 0 deletions src/libutil/hash.hh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ HashResult hashPath(HashType ht, const Path & path,
*/
Hash compressHash(const Hash & hash, unsigned int newSize);

/**
* Parse a string representing a hash format.
*/
HashFormat parseHashFormat(std::string_view hashFormatName);

/**
* std::optional version of parseHashFormat that doesn't throw error.
*/
std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName);

/**
* The reverse of parseHashFormat.
*/
std::string_view printHashFormat(HashFormat hashFormat);

/**
* Parse a string representing a hash type.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/libutil/tests/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ namespace nix {
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"
"c7d329eeb6dd26545e96e55b874be909");
}

/* ----------------------------------------------------------------------------
* parseHashFormat, parseHashFormatOpt, printHashFormat
* --------------------------------------------------------------------------*/

TEST(hashFormat, testRoundTripPrintParse) {
for (const HashFormat hashFormat: { HashFormat::Base64, HashFormat::Base32, HashFormat::Base16, HashFormat::SRI}) {
ASSERT_EQ(parseHashFormat(printHashFormat(hashFormat)), hashFormat);
ASSERT_EQ(*parseHashFormatOpt(printHashFormat(hashFormat)), hashFormat);
}
}

TEST(hashFormat, testParseHashFormatOptException) {
ASSERT_EQ(parseHashFormatOpt("sha0042"), std::nullopt);
}
}

namespace rc {
Expand Down

0 comments on commit 6b47635

Please sign in to comment.