chore(3p/nix/hash): prefer StatusOr over throwing constructor

The use of `unwrap_throw` can be used as a later grep target.

Change-Id: I8c54ed90c4289f07aecb8a1393dd10204c8bce4e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1493
Reviewed-by: glittershark <grfn@gws.fyi>
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Kane York 2020-07-27 19:57:04 -07:00 committed by kanepyork
parent 2a292c71f4
commit 1cbffe21f3
15 changed files with 97 additions and 49 deletions

View file

@ -177,16 +177,12 @@ std::string Hash::to_string(Base base, bool includeType) const {
return s;
}
Hash::Hash(const std::string& s, HashType type) : type(type) {
Hash::Hash(std::string_view s, HashType type) : type(type) {
absl::StatusOr<Hash> result = deserialize(s, type);
if (result.ok()) {
*this = *result;
} else {
throw BadHash(result.status().message());
}
*this = unwrap_throw(result);
}
absl::StatusOr<Hash> Hash::deserialize(const std::string& s, HashType type) {
absl::StatusOr<Hash> Hash::deserialize(std::string_view s, HashType type) {
size_t pos = 0;
bool isSRI = false;
@ -280,6 +276,14 @@ absl::StatusOr<Hash> Hash::deserialize(const std::string& s, HashType type) {
return dest;
}
Hash Hash::unwrap_throw(absl::StatusOr<Hash> hash) {
if (hash.ok()) {
return *hash;
} else {
throw BadHash(hash.status().message());
}
}
namespace hash {
union Ctx {