mirror of
https://github.com/NixOS/nix
synced 2025-06-27 21:01:16 +02:00
Move value-only methods to InstallableValue
These methods would previously fail on the other `Installable`s, so moving them to this class is more correct as to where they actually work. Additionally, a `InstallableValueCommand` is created to make it easier (or rather no worse than before) to write commands that just work on `InstallableValue`s. Besides being a cleanup to avoid failing default methods, this gets us closer to https://github.com/NixOS/rfcs/pull/134.
This commit is contained in:
parent
acd707acca
commit
c998e0172f
14 changed files with 127 additions and 71 deletions
44
src/libcmd/installable-value.cc
Normal file
44
src/libcmd/installable-value.cc
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "installable-value.hh"
|
||||
#include "eval-cache.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
std::vector<ref<eval_cache::AttrCursor>>
|
||||
InstallableValue::getCursors(EvalState & state)
|
||||
{
|
||||
auto evalCache =
|
||||
std::make_shared<nix::eval_cache::EvalCache>(std::nullopt, state,
|
||||
[&]() { return toValue(state).first; });
|
||||
return {evalCache->getRoot()};
|
||||
}
|
||||
|
||||
ref<eval_cache::AttrCursor>
|
||||
InstallableValue::getCursor(EvalState & state)
|
||||
{
|
||||
/* Although getCursors should return at least one element, in case it doesn't,
|
||||
bound check to avoid an undefined behavior for vector[0] */
|
||||
return getCursors(state).at(0);
|
||||
}
|
||||
|
||||
static UsageError nonValueInstallable(Installable & installable)
|
||||
{
|
||||
return UsageError("installable '%s' does not correspond to a Nix language value", installable.what());
|
||||
}
|
||||
|
||||
InstallableValue & InstallableValue::require(Installable & installable)
|
||||
{
|
||||
auto * castedInstallable = dynamic_cast<InstallableValue *>(&installable);
|
||||
if (!castedInstallable)
|
||||
throw nonValueInstallable(installable);
|
||||
return *castedInstallable;
|
||||
}
|
||||
|
||||
ref<InstallableValue> InstallableValue::require(ref<Installable> installable)
|
||||
{
|
||||
auto castedInstallable = installable.dynamic_pointer_cast<InstallableValue>();
|
||||
if (!castedInstallable)
|
||||
throw nonValueInstallable(*installable);
|
||||
return ref { castedInstallable };
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue