struct HashHelper [src]

Fields

hasher: Hasher = hasher_init

Members

Source

pub const HashHelper = struct { hasher: Hasher = hasher_init, /// Record a slice of bytes as a dependency of the process being cached. pub fn addBytes(hh: *HashHelper, bytes: []const u8) void { hh.hasher.update(mem.asBytes(&bytes.len)); hh.hasher.update(bytes); } pub fn addOptionalBytes(hh: *HashHelper, optional_bytes: ?[]const u8) void { hh.add(optional_bytes != null); hh.addBytes(optional_bytes orelse return); } pub fn addListOfBytes(hh: *HashHelper, list_of_bytes: []const []const u8) void { hh.add(list_of_bytes.len); for (list_of_bytes) |bytes| hh.addBytes(bytes); } pub fn addOptionalListOfBytes(hh: *HashHelper, optional_list_of_bytes: ?[]const []const u8) void { hh.add(optional_list_of_bytes != null); hh.addListOfBytes(optional_list_of_bytes orelse return); } /// Convert the input value into bytes and record it as a dependency of the process being cached. pub fn add(hh: *HashHelper, x: anytype) void { switch (@TypeOf(x)) { std.SemanticVersion => { hh.add(x.major); hh.add(x.minor); hh.add(x.patch); }, std.Target.Os.TaggedVersionRange => { switch (x) { .hurd => |hurd| { hh.add(hurd.range.min); hh.add(hurd.range.max); hh.add(hurd.glibc); }, .linux => |linux| { hh.add(linux.range.min); hh.add(linux.range.max); hh.add(linux.glibc); hh.add(linux.android); }, .windows => |windows| { hh.add(windows.min); hh.add(windows.max); }, .semver => |semver| { hh.add(semver.min); hh.add(semver.max); }, .none => {}, } }, std.zig.BuildId => switch (x) { .none, .fast, .uuid, .sha1, .md5 => hh.add(std.meta.activeTag(x)), .hexstring => |hex_string| hh.addBytes(hex_string.toSlice()), }, else => switch (@typeInfo(@TypeOf(x))) { .bool, .int, .@"enum", .array => hh.addBytes(mem.asBytes(&x)), else => @compileError("unable to hash type " ++ @typeName(@TypeOf(x))), }, } } pub fn addOptional(hh: *HashHelper, optional: anytype) void { hh.add(optional != null); hh.add(optional orelse return); } /// Returns a hex encoded hash of the inputs, without modifying state. pub fn peek(hh: HashHelper) [hex_digest_len]u8 { var copy = hh; return copy.final(); } pub fn peekBin(hh: HashHelper) BinDigest { var copy = hh; var bin_digest: BinDigest = undefined; copy.hasher.final(&bin_digest); return bin_digest; } /// Returns a hex encoded hash of the inputs, mutating the state of the hasher. pub fn final(hh: *HashHelper) HexDigest { var bin_digest: BinDigest = undefined; hh.hasher.final(&bin_digest); return binToHex(bin_digest); } pub fn oneShot(bytes: []const u8) [hex_digest_len]u8 { var hasher: Hasher = hasher_init; hasher.update(bytes); var bin_digest: BinDigest = undefined; hasher.final(&bin_digest); return binToHex(bin_digest); } }