forked from lix-project/lix
Add helper function parseHashFormat[Opt] printHashFormat
Add hash format analogy of parseHashTypeOpt, parseHashType, and printHashType. Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
This commit is contained in:
parent
231b0fca6d
commit
6b47635180
|
@ -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)
|
std::optional<HashType> parseHashTypeOpt(std::string_view s)
|
||||||
{
|
{
|
||||||
if (s == "md5") return htMD5;
|
if (s == "md5") return htMD5;
|
||||||
|
|
|
@ -181,6 +181,21 @@ HashResult hashPath(HashType ht, const Path & path,
|
||||||
*/
|
*/
|
||||||
Hash compressHash(const Hash & hash, unsigned int newSize);
|
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.
|
* Parse a string representing a hash type.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -79,6 +79,21 @@ namespace nix {
|
||||||
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"
|
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"
|
||||||
"c7d329eeb6dd26545e96e55b874be909");
|
"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 {
|
namespace rc {
|
||||||
|
|
Loading…
Reference in a new issue