-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Use CompressionAlgo enum throughout #15020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #pragma once | ||
| ///@file | ||
|
|
||
| #include "nix/util/compression-algo.hh" | ||
| #include "nix/util/types.hh" | ||
| #include "nix/util/hash.hh" | ||
| #include "nix/store/path-info.hh" | ||
|
|
@@ -12,7 +13,7 @@ struct StoreDirConfig; | |
| struct UnkeyedNarInfo : virtual UnkeyedValidPathInfo | ||
| { | ||
| std::string url; | ||
| std::string compression; | ||
| std::string compression; // FIXME: Use CompressionAlgo | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| std::optional<Hash> fileHash; | ||
| uint64_t fileSize = 0; | ||
|
|
||
|
|
@@ -42,7 +43,7 @@ struct NarInfo : ValidPathInfo, UnkeyedNarInfo | |
| /* Later copies from `*this` are pointless. The argument is only | ||
| there so the constructors can also call | ||
| `UnkeyedValidPathInfo`, but this won't happen since the base | ||
| class is virtual. Only this counstructor (assuming it is most | ||
| class is virtual. Only this constructor (assuming it is most | ||
| derived) will initialize that virtual base class. */ | ||
| , ValidPathInfo{info.path, static_cast<const UnkeyedValidPathInfo &>(*this)} | ||
| , UnkeyedNarInfo{static_cast<const UnkeyedValidPathInfo &>(*this)} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include "nix/util/compression-algo.hh" | ||
| #include "nix/util/error.hh" | ||
| #include "nix/util/types.hh" | ||
|
|
||
| #include <unordered_map> | ||
|
|
||
| namespace nix { | ||
|
|
||
| CompressionAlgo parseCompressionAlgo(std::string_view method, bool suggestions) | ||
| { | ||
| #define NIX_COMPRESSION_ALGO_FROM_STRING(name, value) {name, CompressionAlgo::value}, | ||
| static const std::unordered_map<std::string_view, CompressionAlgo> lookupTable = { | ||
| NIX_FOR_EACH_COMPRESSION_ALGO(NIX_COMPRESSION_ALGO_FROM_STRING)}; | ||
| #undef NIX_COMPRESSION_ALGO_FROM_STRING | ||
|
|
||
| if (auto it = lookupTable.find(method); it != lookupTable.end()) | ||
| return it->second; | ||
|
|
||
| ErrorInfo err = {.level = lvlError, .msg = HintFmt("unknown compression method '%s'", method)}; | ||
|
|
||
| if (suggestions) { | ||
| static const StringSet allNames = [&]() { | ||
| StringSet res; | ||
| for (auto & [name, _] : lookupTable) | ||
| res.emplace(name); | ||
| return res; | ||
| }(); | ||
| err.suggestions = Suggestions::bestMatches(allNames, method); | ||
| } | ||
|
|
||
| throw UnknownCompressionMethod(std::move(err)); | ||
| } | ||
|
|
||
| std::string showCompressionAlgo(CompressionAlgo method) | ||
| { | ||
| switch (method) { | ||
| #define NIX_COMPRESSION_ALGO_TO_STRING(name, value) \ | ||
| case CompressionAlgo::value: \ | ||
| return name; | ||
| NIX_FOR_EACH_COMPRESSION_ALGO(NIX_COMPRESSION_ALGO_TO_STRING); | ||
| #undef NIX_COMPRESSION_ALGO_TO_STRING | ||
| } | ||
| unreachable(); | ||
| } | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include "nix/util/configuration.hh" | ||
| #include "nix/util/compression-settings.hh" | ||
| #include "nix/util/json-impls.hh" | ||
| #include "nix/util/config-impl.hh" | ||
| #include "nix/util/abstract-setting-to-json.hh" | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| namespace nix { | ||
|
|
||
| template<> | ||
| CompressionAlgo BaseSetting<CompressionAlgo>::parse(const std::string & str) const | ||
| try { | ||
| return parseCompressionAlgo(str, /*suggestions=*/true); | ||
| } catch (UnknownCompressionMethod & e) { | ||
| throw UsageError(e.info().suggestions, "option '%s' has invalid value '%s'", name, str); | ||
| } | ||
|
|
||
| template<> | ||
| std::optional<CompressionAlgo> BaseSetting<std::optional<CompressionAlgo>>::parse(const std::string & str) const | ||
| try { | ||
| if (str.empty()) | ||
| return std::nullopt; | ||
| return parseCompressionAlgo(str, /*suggestions=*/true); | ||
| } catch (UnknownCompressionMethod & e) { | ||
| throw UsageError(e.info().suggestions, "option '%s' has invalid value '%s'", name, str); | ||
| } | ||
|
|
||
| template<> | ||
| struct BaseSetting<CompressionAlgo>::trait | ||
| { | ||
| static constexpr bool appendable = false; | ||
| }; | ||
|
|
||
| template<> | ||
| struct BaseSetting<std::optional<CompressionAlgo>>::trait | ||
| { | ||
| static constexpr bool appendable = false; | ||
| }; | ||
|
|
||
| template<> | ||
| std::string BaseSetting<CompressionAlgo>::to_string() const | ||
| { | ||
| return std::string{showCompressionAlgo(value)}; | ||
| } | ||
|
|
||
| template<> | ||
| std::string BaseSetting<std::optional<CompressionAlgo>>::to_string() const | ||
| { | ||
| if (value) | ||
| return std::string{showCompressionAlgo(*value)}; | ||
| return ""; | ||
| } | ||
|
|
||
| /* Same as with all settings - empty string means std::nullopt. */ | ||
| template<> | ||
| struct json_avoids_null<CompressionAlgo> : std::true_type | ||
| {}; | ||
|
|
||
| #define NIX_COMPRESSION_JSON(name, value) {CompressionAlgo::value, name}, | ||
| NLOHMANN_JSON_SERIALIZE_ENUM(CompressionAlgo, {NIX_FOR_EACH_COMPRESSION_ALGO(NIX_COMPRESSION_JSON)}); | ||
| #undef NIX_COMPRESSION_JSON | ||
|
|
||
| /* Explicit instantiation of templates */ | ||
| template class BaseSetting<CompressionAlgo>; | ||
| template class BaseSetting<std::optional<CompressionAlgo>>; | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#15022