mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-11 08:46:16 +02:00
9121fed4b4
Types converted: - `NixStringContextElem` - `OutputsSpec` - `ExtendedOutputsSpec` - `DerivationOutput` - `DerivationType` Existing ones mostly conforming the pattern cleaned up: - `ContentAddressMethod` - `ContentAddressWithReferences` The `DerivationGoal::derivationType` field had a bogus initialization, now caught, so I made it `std::optional`. I think #8829 can make it non-optional again because it will ensure we always have the derivation when we construct a `DerivationGoal`. See that issue (#7479) for details on the general goal. `git grep 'Raw::Raw'` indicates the two types I didn't yet convert `DerivedPath` and `BuiltPath` (and their `Single` variants) . This is because @roberth and I (can't find issue right now...) plan on reworking them somewhat, so I didn't want to churn them more just yet. Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
228 lines
6.6 KiB
C++
228 lines
6.6 KiB
C++
#include "args.hh"
|
|
#include "content-address.hh"
|
|
#include "split.hh"
|
|
|
|
namespace nix {
|
|
|
|
std::string makeFileIngestionPrefix(FileIngestionMethod m)
|
|
{
|
|
switch (m) {
|
|
case FileIngestionMethod::Flat:
|
|
return "";
|
|
case FileIngestionMethod::Recursive:
|
|
return "r:";
|
|
default:
|
|
throw Error("impossible, caught both cases");
|
|
}
|
|
}
|
|
|
|
std::string ContentAddressMethod::renderPrefix() const
|
|
{
|
|
return std::visit(overloaded {
|
|
[](TextIngestionMethod) -> std::string { return "text:"; },
|
|
[](FileIngestionMethod m2) {
|
|
/* Not prefixed for back compat with things that couldn't produce text before. */
|
|
return makeFileIngestionPrefix(m2);
|
|
},
|
|
}, raw);
|
|
}
|
|
|
|
ContentAddressMethod ContentAddressMethod::parsePrefix(std::string_view & m)
|
|
{
|
|
ContentAddressMethod method = FileIngestionMethod::Flat;
|
|
if (splitPrefix(m, "r:"))
|
|
method = FileIngestionMethod::Recursive;
|
|
else if (splitPrefix(m, "text:"))
|
|
method = TextIngestionMethod {};
|
|
return method;
|
|
}
|
|
|
|
std::string ContentAddressMethod::render(HashType ht) const
|
|
{
|
|
return std::visit(overloaded {
|
|
[&](const TextIngestionMethod & th) {
|
|
return std::string{"text:"} + printHashType(ht);
|
|
},
|
|
[&](const FileIngestionMethod & fim) {
|
|
return "fixed:" + makeFileIngestionPrefix(fim) + printHashType(ht);
|
|
}
|
|
}, raw);
|
|
}
|
|
|
|
std::string ContentAddress::render() const
|
|
{
|
|
return std::visit(overloaded {
|
|
[](const TextIngestionMethod &) -> std::string {
|
|
return "text:";
|
|
},
|
|
[](const FileIngestionMethod & method) {
|
|
return "fixed:"
|
|
+ makeFileIngestionPrefix(method);
|
|
},
|
|
}, method.raw)
|
|
+ this->hash.to_string(Base32, true);
|
|
}
|
|
|
|
/**
|
|
* Parses content address strings up to the hash.
|
|
*/
|
|
static std::pair<ContentAddressMethod, HashType> parseContentAddressMethodPrefix(std::string_view & rest)
|
|
{
|
|
std::string_view wholeInput { rest };
|
|
|
|
std::string_view prefix;
|
|
{
|
|
auto optPrefix = splitPrefixTo(rest, ':');
|
|
if (!optPrefix)
|
|
throw UsageError("not a content address because it is not in the form '<prefix>:<rest>': %s", wholeInput);
|
|
prefix = *optPrefix;
|
|
}
|
|
|
|
auto parseHashType_ = [&](){
|
|
auto hashTypeRaw = splitPrefixTo(rest, ':');
|
|
if (!hashTypeRaw)
|
|
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", wholeInput);
|
|
HashType hashType = parseHashType(*hashTypeRaw);
|
|
return std::move(hashType);
|
|
};
|
|
|
|
// Switch on prefix
|
|
if (prefix == "text") {
|
|
// No parsing of the ingestion method, "text" only support flat.
|
|
HashType hashType = parseHashType_();
|
|
return {
|
|
TextIngestionMethod {},
|
|
std::move(hashType),
|
|
};
|
|
} else if (prefix == "fixed") {
|
|
// Parse method
|
|
auto method = FileIngestionMethod::Flat;
|
|
if (splitPrefix(rest, "r:"))
|
|
method = FileIngestionMethod::Recursive;
|
|
HashType hashType = parseHashType_();
|
|
return {
|
|
std::move(method),
|
|
std::move(hashType),
|
|
};
|
|
} else
|
|
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
|
|
}
|
|
|
|
ContentAddress ContentAddress::parse(std::string_view rawCa)
|
|
{
|
|
auto rest = rawCa;
|
|
|
|
auto [caMethod, hashType] = parseContentAddressMethodPrefix(rest);
|
|
|
|
return ContentAddress {
|
|
.method = std::move(caMethod),
|
|
.hash = Hash::parseNonSRIUnprefixed(rest, hashType),
|
|
};
|
|
}
|
|
|
|
std::pair<ContentAddressMethod, HashType> ContentAddressMethod::parse(std::string_view caMethod)
|
|
{
|
|
std::string asPrefix = std::string{caMethod} + ":";
|
|
// parseContentAddressMethodPrefix takes its argument by reference
|
|
std::string_view asPrefixView = asPrefix;
|
|
return parseContentAddressMethodPrefix(asPrefixView);
|
|
}
|
|
|
|
std::optional<ContentAddress> ContentAddress::parseOpt(std::string_view rawCaOpt)
|
|
{
|
|
return rawCaOpt == ""
|
|
? std::nullopt
|
|
: std::optional { ContentAddress::parse(rawCaOpt) };
|
|
};
|
|
|
|
std::string renderContentAddress(std::optional<ContentAddress> ca)
|
|
{
|
|
return ca ? ca->render() : "";
|
|
}
|
|
|
|
std::string ContentAddress::printMethodAlgo() const
|
|
{
|
|
return method.renderPrefix()
|
|
+ printHashType(hash.type);
|
|
}
|
|
|
|
bool StoreReferences::empty() const
|
|
{
|
|
return !self && others.empty();
|
|
}
|
|
|
|
size_t StoreReferences::size() const
|
|
{
|
|
return (self ? 1 : 0) + others.size();
|
|
}
|
|
|
|
ContentAddressWithReferences ContentAddressWithReferences::withoutRefs(const ContentAddress & ca) noexcept
|
|
{
|
|
return std::visit(overloaded {
|
|
[&](const TextIngestionMethod &) -> ContentAddressWithReferences {
|
|
return TextInfo {
|
|
.hash = ca.hash,
|
|
.references = {},
|
|
};
|
|
},
|
|
[&](const FileIngestionMethod & method) -> ContentAddressWithReferences {
|
|
return FixedOutputInfo {
|
|
.method = method,
|
|
.hash = ca.hash,
|
|
.references = {},
|
|
};
|
|
},
|
|
}, ca.method.raw);
|
|
}
|
|
|
|
std::optional<ContentAddressWithReferences> ContentAddressWithReferences::fromPartsOpt(
|
|
ContentAddressMethod method, Hash hash, StoreReferences refs) noexcept
|
|
{
|
|
return std::visit(overloaded {
|
|
[&](TextIngestionMethod _) -> std::optional<ContentAddressWithReferences> {
|
|
if (refs.self)
|
|
return std::nullopt;
|
|
return ContentAddressWithReferences {
|
|
TextInfo {
|
|
.hash = std::move(hash),
|
|
.references = std::move(refs.others),
|
|
}
|
|
};
|
|
},
|
|
[&](FileIngestionMethod m2) -> std::optional<ContentAddressWithReferences> {
|
|
return ContentAddressWithReferences {
|
|
FixedOutputInfo {
|
|
.method = m2,
|
|
.hash = std::move(hash),
|
|
.references = std::move(refs),
|
|
}
|
|
};
|
|
},
|
|
}, method.raw);
|
|
}
|
|
|
|
ContentAddressMethod ContentAddressWithReferences::getMethod() const
|
|
{
|
|
return std::visit(overloaded {
|
|
[](const TextInfo & th) -> ContentAddressMethod {
|
|
return TextIngestionMethod {};
|
|
},
|
|
[](const FixedOutputInfo & fsh) -> ContentAddressMethod {
|
|
return fsh.method;
|
|
},
|
|
}, raw);
|
|
}
|
|
|
|
Hash ContentAddressWithReferences::getHash() const
|
|
{
|
|
return std::visit(overloaded {
|
|
[](const TextInfo & th) {
|
|
return th.hash;
|
|
},
|
|
[](const FixedOutputInfo & fsh) {
|
|
return fsh.hash;
|
|
},
|
|
}, raw);
|
|
}
|
|
|
|
}
|