tiny.sandbox.change
Defined in tiny.sandbox.
API (6)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: lib/sandbox/src/change.zig
zig
const std = @import("std");const Allocator = std.mem.Allocator;pub const Operation = enum { put, delete,};pub const Kind = enum { file, directory, sym_link, other,};pub const Entry = struct { path: []u8, kind: Kind, hash: []u8 = &.{}, target: []u8 = &.{}, bytes: u64 = 0, pub fn clone(self: Entry, allocator: Allocator) Allocator.Error!Entry { const path = try allocator.dupe(u8, self.path); errdefer allocator.free(path); const hash = try allocator.dupe(u8, self.hash); errdefer allocator.free(hash); const target = try allocator.dupe(u8, self.target); return .{ .path = path, .kind = self.kind, .hash = hash, .target = target, .bytes = self.bytes, }; } pub fn deinit(self: *Entry, allocator: Allocator) void { allocator.free(self.path); allocator.free(self.hash); allocator.free(self.target); self.* = undefined; }};pub const Change = struct { operation: Operation, entry: Entry, pub fn clone(self: Change, allocator: Allocator) Allocator.Error!Change { return .{ .operation = self.operation, .entry = try self.entry.clone(allocator), }; } pub fn deinit(self: *Change, allocator: Allocator) void { self.entry.deinit(allocator); self.* = undefined; }};pub const Set = struct { allocator: Allocator, items: std.ArrayList(Change) = .empty, pub fn init(allocator: Allocator) Set { return .{ .allocator = allocator }; } pub fn deinit(self: *Set) void { for (self.items.items) |*item| item.deinit(self.allocator); self.items.deinit(self.allocator); self.* = undefined; } pub fn append(self: *Set, operation: Operation, entry: Entry) Allocator.Error!void { try self.items.append(self.allocator, .{ .operation = operation, .entry = try entry.clone(self.allocator), }); } pub fn len(self: *const Set) usize { return self.items.items.len; } pub fn get(self: *const Set, index: usize) ?Change { if (index >= self.items.items.len) return null; return self.items.items[index]; } pub fn find(self: *const Set, path: []const u8) ?Change { for (self.items.items) |item| { if (std.mem.eql(u8, item.entry.path, path)) return item; } return null; }};pub fn sameEntry(left: Entry, right: Entry) bool { return left.kind == right.kind and left.bytes == right.bytes and std.mem.eql(u8, left.path, right.path) and std.mem.eql(u8, left.hash, right.hash) and std.mem.eql(u8, left.target, right.target);}test "change set owns cloned entries" { var set = Set.init(std.testing.allocator); defer set.deinit(); var path = try std.testing.allocator.dupe(u8, "a.txt"); defer std.testing.allocator.free(path); var hash = try std.testing.allocator.dupe(u8, "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); defer std.testing.allocator.free(hash); try set.append(.put, .{ .path = path, .kind = .file, .hash = hash, .bytes = 4, }); path[0] = 'b'; hash["sha256:".len] = 'b'; const found = set.find("a.txt").?; try std.testing.expectEqual(Operation.put, found.operation); try std.testing.expectEqual(Kind.file, found.entry.kind); try std.testing.expectEqualStrings("sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", found.entry.hash);}Source: lib/sandbox/src/root.zig:31
zig
pub const change = @import("change.zig");Audit
| Definitions | 2 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |