From 370ac940dd7816ad4052fafa4e0f8d17784fa16b Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Thu, 1 Aug 2024 13:42:02 -0700 Subject: refactor: make HashType and Base enum classes for type safety Change-Id: I9fbd55a9d50464a56fe11cb42a06a206914150d8 --- src/nix/add-to-store.cc | 4 ++-- src/nix/flake.cc | 8 ++++---- src/nix/hash.cc | 50 ++++++++++++++++++++++++------------------------- src/nix/path-info.cc | 2 +- src/nix/prefetch.cc | 8 ++++---- src/nix/verify.cc | 4 ++-- 6 files changed, 38 insertions(+), 38 deletions(-) (limited to 'src/nix') diff --git a/src/nix/add-to-store.cc b/src/nix/add-to-store.cc index 7e16e12c3..b9f7ece89 100644 --- a/src/nix/add-to-store.cc +++ b/src/nix/add-to-store.cc @@ -32,11 +32,11 @@ struct CmdAddToStore : MixDryRun, StoreCommand StringSink sink; sink << dumpPath(path); - auto narHash = hashString(htSHA256, sink.s); + auto narHash = hashString(HashType::SHA256, sink.s); Hash hash = narHash; if (ingestionMethod == FileIngestionMethod::Flat) { - HashSink hsink(htSHA256); + HashSink hsink(HashType::SHA256); hsink << readFileSource(path); hash = hsink.finish().first; } diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 672930342..5ea9e077b 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -226,7 +226,7 @@ struct CmdFlakeMetadata : FlakeCommand, MixJSON j["url"] = flake.lockedRef.to_string(); // FIXME: rename to lockedUrl j["locked"] = fetchers::attrsToJSON(flake.lockedRef.toAttrs()); if (auto rev = flake.lockedRef.input.getRev()) - j["revision"] = rev->to_string(Base16, false); + j["revision"] = rev->to_string(Base::Base16, false); if (auto dirtyRev = fetchers::maybeGetStrAttr(flake.lockedRef.toAttrs(), "dirtyRev")) j["dirtyRevision"] = *dirtyRev; if (auto revCount = flake.lockedRef.input.getRevCount()) @@ -253,7 +253,7 @@ struct CmdFlakeMetadata : FlakeCommand, MixJSON if (auto rev = flake.lockedRef.input.getRev()) logger->cout( ANSI_BOLD "Revision:" ANSI_NORMAL " %s", - rev->to_string(Base16, false)); + rev->to_string(Base::Base16, false)); if (auto dirtyRev = fetchers::maybeGetStrAttr(flake.lockedRef.toAttrs(), "dirtyRev")) logger->cout( ANSI_BOLD "Revision:" ANSI_NORMAL " %s", @@ -1434,13 +1434,13 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON if (json) { auto res = nlohmann::json::object(); res["storePath"] = store->printStorePath(tree.storePath); - res["hash"] = hash.to_string(SRI, true); + res["hash"] = hash.to_string(Base::SRI, true); logger->cout(res.dump()); } else { notice("Downloaded '%s' to '%s' (hash '%s').", lockedRef.to_string(), store->printStorePath(tree.storePath), - hash.to_string(SRI, true)); + hash.to_string(Base::SRI, true)); } } }; diff --git a/src/nix/hash.cc b/src/nix/hash.cc index 12b66a83c..f6add527a 100644 --- a/src/nix/hash.cc +++ b/src/nix/hash.cc @@ -11,9 +11,9 @@ using namespace nix; struct CmdHashBase : Command { FileIngestionMethod mode; - Base base = SRI; + Base base = Base::SRI; bool truncate = false; - HashType ht = htSHA256; + HashType ht = HashType::SHA256; std::vector paths; std::optional modulus; @@ -22,25 +22,25 @@ struct CmdHashBase : Command addFlag({ .longName = "sri", .description = "Print the hash in SRI format.", - .handler = {&base, SRI}, + .handler = {&base, Base::SRI}, }); addFlag({ .longName = "base64", .description = "Print the hash in base-64 format.", - .handler = {&base, Base64}, + .handler = {&base, Base::Base64}, }); addFlag({ .longName = "base32", .description = "Print the hash in base-32 (Nix-specific) format.", - .handler = {&base, Base32}, + .handler = {&base, Base::Base32}, }); addFlag({ .longName = "base16", .description = "Print the hash in base-16 format.", - .handler = {&base, Base16}, + .handler = {&base, Base::Base16}, }); addFlag(Flag::mkHashTypeFlag("type", &ht)); @@ -90,7 +90,7 @@ struct CmdHashBase : Command ? computeHashModulo(ht, *modulus, source).first : hashSource(ht, source).first; if (truncate && h.hashSize > 20) h = compressHash(h, 20); - logger->cout(h.to_string(base, base == SRI)); + logger->cout(h.to_string(base, base == Base::SRI)); } } }; @@ -110,16 +110,16 @@ struct CmdToBase : Command std::string description() override { return fmt("convert a hash to %s representation", - base == Base16 ? "base-16" : - base == Base32 ? "base-32" : - base == Base64 ? "base-64" : + base == Base::Base16 ? "base-16" : + base == Base::Base32 ? "base-32" : + base == Base::Base64 ? "base-64" : "SRI"); } void run() override { for (auto s : args) - logger->cout(Hash::parseAny(s, ht).to_string(base, base == SRI)); + logger->cout(Hash::parseAny(s, ht).to_string(base, base == Base::SRI)); } }; @@ -129,10 +129,10 @@ struct CmdHash : NixMultiCommand : MultiCommand({ {"file", []() { return make_ref(FileIngestionMethod::Flat);; }}, {"path", []() { return make_ref(FileIngestionMethod::Recursive); }}, - {"to-base16", []() { return make_ref(Base16); }}, - {"to-base32", []() { return make_ref(Base32); }}, - {"to-base64", []() { return make_ref(Base64); }}, - {"to-sri", []() { return make_ref(SRI); }}, + {"to-base16", []() { return make_ref(Base::Base16); }}, + {"to-base32", []() { return make_ref(Base::Base32); }}, + {"to-base64", []() { return make_ref(Base::Base64); }}, + {"to-sri", []() { return make_ref(Base::SRI); }}, }) { } @@ -158,7 +158,7 @@ static int compatNixHash(int argc, char * * argv) { std::optional ht; bool flat = false; - Base base = Base16; + Base base = Base::Base16; bool truncate = false; enum { opHash, opTo } op = opHash; std::vector ss; @@ -169,10 +169,10 @@ static int compatNixHash(int argc, char * * argv) else if (*arg == "--version") printVersion("nix-hash"); else if (*arg == "--flat") flat = true; - else if (*arg == "--base16") base = Base16; - else if (*arg == "--base32") base = Base32; - else if (*arg == "--base64") base = Base64; - else if (*arg == "--sri") base = SRI; + else if (*arg == "--base16") base = Base::Base16; + else if (*arg == "--base32") base = Base::Base32; + else if (*arg == "--base64") base = Base::Base64; + else if (*arg == "--sri") base = Base::SRI; else if (*arg == "--truncate") truncate = true; else if (*arg == "--type") { std::string s = getArg(*arg, arg, end); @@ -180,19 +180,19 @@ static int compatNixHash(int argc, char * * argv) } else if (*arg == "--to-base16") { op = opTo; - base = Base16; + base = Base::Base16; } else if (*arg == "--to-base32") { op = opTo; - base = Base32; + base = Base::Base32; } else if (*arg == "--to-base64") { op = opTo; - base = Base64; + base = Base::Base64; } else if (*arg == "--to-sri") { op = opTo; - base = SRI; + base = Base::SRI; } else if (*arg != "" && arg->at(0) == '-') return false; @@ -203,7 +203,7 @@ static int compatNixHash(int argc, char * * argv) if (op == opHash) { CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive); - if (!ht.has_value()) ht = htMD5; + if (!ht.has_value()) ht = HashType::MD5; cmd.ht = ht.value(); cmd.base = base; cmd.truncate = truncate; diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc index 613c5b191..b14eef467 100644 --- a/src/nix/path-info.cc +++ b/src/nix/path-info.cc @@ -90,7 +90,7 @@ struct CmdPathInfo : StorePathsCommand, MixJSON std::cout << store->pathInfoToJSON( // FIXME: preserve order? StorePathSet(storePaths.begin(), storePaths.end()), - true, showClosureSize, SRI, AllowInvalid).dump(); + true, showClosureSize, Base::SRI, AllowInvalid).dump(); } else { diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 13d94d645..0b04a04e6 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -133,7 +133,7 @@ std::tuple prefetchFile( static int main_nix_prefetch_url(int argc, char * * argv) { { - HashType ht = htSHA256; + HashType ht = HashType::SHA256; std::vector args; bool printPath = getEnv("PRINT_PATH") == "1"; bool fromExpr = false; @@ -256,7 +256,7 @@ struct CmdStorePrefetchFile : StoreCommand, MixJSON bool executable = false; bool unpack = false; std::optional name; - HashType hashType = htSHA256; + HashType hashType = HashType::SHA256; std::optional expectedHash; CmdStorePrefetchFile() @@ -316,13 +316,13 @@ struct CmdStorePrefetchFile : StoreCommand, MixJSON if (json) { auto res = nlohmann::json::object(); res["storePath"] = store->printStorePath(storePath); - res["hash"] = hash.to_string(SRI, true); + res["hash"] = hash.to_string(Base::SRI, true); logger->cout(res.dump()); } else { notice("Downloaded '%s' to '%s' (hash '%s').", url, store->printStorePath(storePath), - hash.to_string(SRI, true)); + hash.to_string(Base::SRI, true)); } } }; diff --git a/src/nix/verify.cc b/src/nix/verify.cc index eb68e67bc..8783d4e04 100644 --- a/src/nix/verify.cc +++ b/src/nix/verify.cc @@ -109,8 +109,8 @@ struct CmdVerify : StorePathsCommand act2.result(resCorruptedPath, store->printStorePath(info->path)); printError("path '%s' was modified! expected hash '%s', got '%s'", store->printStorePath(info->path), - info->narHash.to_string(Base32, true), - hash.first.to_string(Base32, true)); + info->narHash.to_string(Base::Base32, true), + hash.first.to_string(Base::Base32, true)); } } -- cgit v1.2.3