mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-27 08:16:14 +02:00
Factor the prefix splitting in content-address
This commit is contained in:
parent
7ba0fae0dd
commit
77b51f4598
1 changed files with 12 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include "args.hh"
|
#include "args.hh"
|
||||||
#include "content-address.hh"
|
#include "content-address.hh"
|
||||||
|
#include "parser.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -43,23 +44,19 @@ std::string renderContentAddress(ContentAddress ca) {
|
||||||
ContentAddress parseContentAddress(std::string_view rawCa) {
|
ContentAddress parseContentAddress(std::string_view rawCa) {
|
||||||
auto rest = rawCa;
|
auto rest = rawCa;
|
||||||
|
|
||||||
// Ensure prefix
|
std::string_view prefix;
|
||||||
const auto prefixSeparator = rawCa.find(':');
|
{
|
||||||
if (prefixSeparator == string::npos)
|
auto optPrefix = splitPrefix(rest, ':');
|
||||||
|
if (!optPrefix)
|
||||||
throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa);
|
throw UsageError("not a content address because it is not in the form \"<prefix>:<rest>\": %s", rawCa);
|
||||||
auto prefix = rest.substr(0, prefixSeparator);
|
prefix = *optPrefix;
|
||||||
rest = rest.substr(prefixSeparator + 1);
|
}
|
||||||
|
|
||||||
auto parseHashType_ = [&](){
|
auto parseHashType_ = [&](){
|
||||||
// Parse hash type
|
auto hashTypeRaw = splitPrefix(rest, ':');
|
||||||
auto algoSeparator = rest.find(':');
|
if (!hashTypeRaw)
|
||||||
HashType hashType;
|
throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rawCa);
|
||||||
if (algoSeparator == string::npos)
|
HashType hashType = parseHashType(*hashTypeRaw);
|
||||||
throw UsageError("content address hash must be in form \"<algo>:<hash>\", but found: %s", rest);
|
|
||||||
hashType = parseHashType(rest.substr(0, algoSeparator));
|
|
||||||
|
|
||||||
rest = rest.substr(algoSeparator + 1);
|
|
||||||
|
|
||||||
return std::move(hashType);
|
return std::move(hashType);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue