forked from lix-project/lix
Add ContentAddressMethod and parse/render it
This commit is contained in:
parent
29c82ccc77
commit
dfa547c6a8
|
@ -37,48 +37,86 @@ std::string renderContentAddress(ContentAddress ca) {
|
||||||
}, ca);
|
}, ca);
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentAddress parseContentAddress(std::string_view rawCa) {
|
std::string renderContentAddressMethod(ContentAddressMethod cam) {
|
||||||
auto rest = rawCa;
|
return std::visit(overloaded {
|
||||||
|
[](TextHashMethod &th) {
|
||||||
|
return std::string{"text:"} + printHashType(htSHA256);
|
||||||
|
},
|
||||||
|
[](FixedOutputHashMethod &fshm) {
|
||||||
|
return "fixed:" + makeFileIngestionPrefix(fshm.fileIngestionMethod) + printHashType(fshm.hashType);
|
||||||
|
}
|
||||||
|
}, cam);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Parses content address strings up to the hash.
|
||||||
|
*/
|
||||||
|
static ContentAddressMethod parseContentAddressMethodPrefix(std::string_view & rest) {
|
||||||
|
std::string wholeInput(rest);
|
||||||
|
|
||||||
std::string_view prefix;
|
std::string_view prefix;
|
||||||
{
|
{
|
||||||
auto optPrefix = splitPrefixTo(rest, ':');
|
auto optPrefix = splitPrefixTo(rest, ':');
|
||||||
if (!optPrefix)
|
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", wholeInput);
|
||||||
prefix = *optPrefix;
|
prefix = *optPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parseHashType_ = [&](){
|
auto parseHashType_ = [&](){
|
||||||
auto hashTypeRaw = splitPrefixTo(rest, ':');
|
auto hashTypeRaw = splitPrefixTo(rest, ':');
|
||||||
if (!hashTypeRaw)
|
if (!hashTypeRaw)
|
||||||
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", rawCa);
|
throw UsageError("content address hash must be in form '<algo>:<hash>', but found: %s", wholeInput);
|
||||||
HashType hashType = parseHashType(*hashTypeRaw);
|
HashType hashType = parseHashType(*hashTypeRaw);
|
||||||
return std::move(hashType);
|
return std::move(hashType);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Switch on prefix
|
// Switch on prefix
|
||||||
if (prefix == "text") {
|
if (prefix == "text") {
|
||||||
// No parsing of the method, "text" only support flat.
|
// No parsing of the ingestion method, "text" only support flat.
|
||||||
HashType hashType = parseHashType_();
|
HashType hashType = parseHashType_();
|
||||||
if (hashType != htSHA256)
|
if (hashType != htSHA256)
|
||||||
throw Error("text content address hash should use %s, but instead uses %s",
|
throw Error("text content address hash should use %s, but instead uses %s",
|
||||||
printHashType(htSHA256), printHashType(hashType));
|
printHashType(htSHA256), printHashType(hashType));
|
||||||
return TextHash {
|
return TextHashMethod {};
|
||||||
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(hashType)),
|
|
||||||
};
|
|
||||||
} else if (prefix == "fixed") {
|
} else if (prefix == "fixed") {
|
||||||
// Parse method
|
// Parse method
|
||||||
auto method = FileIngestionMethod::Flat;
|
auto method = FileIngestionMethod::Flat;
|
||||||
if (splitPrefix(rest, "r:"))
|
if (splitPrefix(rest, "r:"))
|
||||||
method = FileIngestionMethod::Recursive;
|
method = FileIngestionMethod::Recursive;
|
||||||
HashType hashType = parseHashType_();
|
HashType hashType = parseHashType_();
|
||||||
return FixedOutputHash {
|
return FixedOutputHashMethod {
|
||||||
.method = method,
|
.fileIngestionMethod = method,
|
||||||
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(hashType)),
|
.hashType = std::move(hashType),
|
||||||
};
|
};
|
||||||
} else
|
} else
|
||||||
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
|
throw UsageError("content address prefix '%s' is unrecognized. Recogonized prefixes are 'text' or 'fixed'", prefix);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
ContentAddress parseContentAddress(std::string_view rawCa) {
|
||||||
|
auto rest = rawCa;
|
||||||
|
|
||||||
|
ContentAddressMethod caMethod = parseContentAddressMethodPrefix(rest);
|
||||||
|
|
||||||
|
return std::visit(
|
||||||
|
overloaded {
|
||||||
|
[&](TextHashMethod thm) {
|
||||||
|
return ContentAddress(TextHash {
|
||||||
|
.hash = Hash::parseNonSRIUnprefixed(rest, htSHA256)
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[&](FixedOutputHashMethod fohMethod) {
|
||||||
|
return ContentAddress(FixedOutputHash {
|
||||||
|
.method = fohMethod.fileIngestionMethod,
|
||||||
|
.hash = Hash::parseNonSRIUnprefixed(rest, std::move(fohMethod.hashType)),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}, caMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
ContentAddressMethod parseContentAddressMethod(std::string_view caMethod) {
|
||||||
|
std::string_view asPrefix {std::string{caMethod} + ":"};
|
||||||
|
return parseContentAddressMethodPrefix(asPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) {
|
std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt) {
|
||||||
return rawCaOpt == "" ? std::optional<ContentAddress> {} : parseContentAddress(rawCaOpt);
|
return rawCaOpt == "" ? std::optional<ContentAddress> {} : parseContentAddress(rawCaOpt);
|
||||||
|
|
|
@ -55,4 +55,23 @@ std::optional<ContentAddress> parseContentAddressOpt(std::string_view rawCaOpt);
|
||||||
|
|
||||||
Hash getContentAddressHash(const ContentAddress & ca);
|
Hash getContentAddressHash(const ContentAddress & ca);
|
||||||
|
|
||||||
|
/*
|
||||||
|
We only have one way to hash text with references, so this is single-value
|
||||||
|
type is only useful in std::variant.
|
||||||
|
*/
|
||||||
|
struct TextHashMethod { };
|
||||||
|
struct FixedOutputHashMethod {
|
||||||
|
FileIngestionMethod fileIngestionMethod;
|
||||||
|
HashType hashType;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::variant<
|
||||||
|
TextHashMethod,
|
||||||
|
FixedOutputHashMethod
|
||||||
|
> ContentAddressMethod;
|
||||||
|
|
||||||
|
ContentAddressMethod parseContentAddressMethod(std::string_view rawCaMethod);
|
||||||
|
|
||||||
|
std::string renderContentAddressMethod(ContentAddressMethod caMethod);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue