Skip to content

Commit

Permalink
all: Revert snake_case rename (#14)
Browse files Browse the repository at this point in the history
Match Zig's own style guide for naming
and use camelCase for function names.

This reverts commit d83f882.
  • Loading branch information
abhinav authored May 2, 2024
1 parent 3db365c commit e866d27
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
### Changed
- Revert release 0.4.0.
- Switch support to Zig 0.12.

## 0.4.0 - 2024-01-27
Expand Down
6 changes: 3 additions & 3 deletions src/Archive.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ pub fn extract(self: Archive, alloc: std.mem.Allocator, dest: []const u8) !void
var dir = try std.fs.cwd().makeOpenPath(dest, .{});
defer dir.close();

return self.extract_dir(alloc, dir);
return self.extractDir(alloc, dir);
}

/// Extracts all files in the Archive to the given directory.
///
/// Paths in the archive are relative to the destination directory.
/// They are not allowed to escape the destination directory with use of `../`.
pub fn extract_dir(self: Archive, alloc: std.mem.Allocator, dir: std.fs.Dir) !void {
pub fn extractDir(self: Archive, alloc: std.mem.Allocator, dir: std.fs.Dir) !void {
const extractor = try Extractor.init(alloc, dir);
defer extractor.deinit();

for (self.files.items()) |file| {
try extractor.write_file(file);
try extractor.writeFile(file);
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/Extractor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! For example:
//!
//! ```
//! extractor.write_file(txtar.File{
//! extractor.writeFile(txtar.File{
//! .name = "../foo.txt",
//! .contents = "...",
//! });
Expand Down Expand Up @@ -63,10 +63,10 @@ const FSError = std.posix.MakeDirError || std.fs.File.OpenError || std.fs.File.W
///
/// If the file attempts to traverse outside the destination directory,
/// returns error.PathTraversal.
pub fn write_file(self: *const Extractor, f: File) WriteError!void {
pub fn writeFile(self: *const Extractor, f: File) WriteError!void {
const resolved_path = try std.fs.path.resolve(self.allocator, &.{ self.dir_path, f.name });
defer self.allocator.free(resolved_path);
if (!is_descendant(self.dir_path, resolved_path)) return error.PathTraversal;
if (!isDescendant(self.dir_path, resolved_path)) return error.PathTraversal;

if (std.fs.path.dirname(f.name)) |parent_dir| {
try self.dir.makePath(parent_dir);
Expand All @@ -77,7 +77,7 @@ pub fn write_file(self: *const Extractor, f: File) WriteError!void {
try file.writer().writeAll(f.contents);
}

test "write_file allocation error" {
test "writeFile allocation error" {
const allocator = std.testing.failing_allocator;

var temp_dir = std.testing.tmpDir(.{});
Expand All @@ -87,7 +87,7 @@ test "write_file allocation error" {
try std.testing.expectError(error.OutOfMemory, got);
}

test "write_file traversal error" {
test "writeFile traversal error" {
const allocator = std.testing.allocator;

var temp_dir = std.testing.tmpDir(.{});
Expand All @@ -103,19 +103,19 @@ test "write_file traversal error" {
"../../../foo",
};
for (tests) |name| {
const got = extractor.write_file(.{
const got = extractor.writeFile(.{
.name = name,
.contents = "quux",
});
errdefer std.debug.print("write_file({s}) should fail\n", .{name});
errdefer std.debug.print("writeFile({s}) should fail\n", .{name});

try std.testing.expectError(error.PathTraversal, got);
}
}

// Reports whether child is a descendant of parent.
// Must not be equal to parent.
fn is_descendant(p: []const u8, child: []const u8) bool {
fn isDescendant(p: []const u8, child: []const u8) bool {
// Drop trailing slash from parent.
const parent = if (p.len > 0 and p[p.len - 1] == std.fs.path.sep)
p[0 .. p.len - 1]
Expand All @@ -130,7 +130,7 @@ fn is_descendant(p: []const u8, child: []const u8) bool {
child[parent.len] == std.fs.path.sep;
}

test "is_descendant" {
test "isDescendant" {
const sep = std.fs.path.sep_str;

const tests = [_]struct {
Expand All @@ -144,8 +144,8 @@ test "is_descendant" {
};

for (tests) |tt| {
const got = is_descendant(tt.parent, tt.child);
errdefer std.debug.print("is_descendant({s}, {s}) should be {}\n", .{ tt.parent, tt.child, tt.want });
const got = isDescendant(tt.parent, tt.child);
errdefer std.debug.print("isDescendant({s}, {s}) should be {}\n", .{ tt.parent, tt.child, tt.want });
try std.testing.expectEqual(got, tt.want);
}
}
6 changes: 3 additions & 3 deletions src/Iterator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ pub fn extract(self: *Iterator, alloc: std.mem.Allocator, dest: []const u8) !voi
var dir = try std.fs.cwd().makeOpenPath(dest, .{});
defer dir.close();

return self.extract_dir(alloc, dir);
return self.extractDir(alloc, dir);
}

/// Extracts the remaining contents of an Iterator to a directory,
/// consuming the iterator in the process.
///
/// Paths in the archive are relative to the destination directory.
/// They are not allowed to escape the destination directory with use of `../`.
pub fn extract_dir(self: *Iterator, alloc: std.mem.Allocator, dir: std.fs.Dir) !void {
pub fn extractDir(self: *Iterator, alloc: std.mem.Allocator, dir: std.fs.Dir) !void {
const extractor = try Extractor.init(alloc, dir);
defer extractor.deinit();

while (self.next()) |file| {
try extractor.write_file(file);
try extractor.writeFile(file);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const std = @import("std");
const File = @import("./File.zig");

/// Formatter writes txtar files to a writer of the given type.
/// Use `new_formatter` to construct a Formatter from a writer with a known type.
/// Use `newFormatter` to construct a Formatter from a writer with a known type.
pub fn Formatter(comptime Writer: type) type {
return struct {
/// Error type returned by the Formatter.
Expand All @@ -19,7 +19,7 @@ pub fn Formatter(comptime Writer: type) type {
/// Comment, if non-null, is written at the top of the file.
pub fn init(w: Writer, comment: ?[]const u8) Error!Self {
if (comment) |c| if (c.len > 0) {
try print_ensure_nl(w, c);
try printEnsureNL(w, c);
};

return .{ .writer = w };
Expand All @@ -33,12 +33,12 @@ pub fn Formatter(comptime Writer: type) type {
/// Note that the formatter does not sanitize the file name.
/// If it contains directory traversal elements like "../",
/// they'll be written to the txtar file as-is.
pub fn write_file(self: *Self, f: File) Error!void {
pub fn writeFile(self: *Self, f: File) Error!void {
try self.writer.print("-- {s} --\n", .{f.name});
try print_ensure_nl(self.writer, f.contents);
try printEnsureNL(self.writer, f.contents);
}

fn print_ensure_nl(w: Writer, s: []const u8) Error!void {
fn printEnsureNL(w: Writer, s: []const u8) Error!void {
try w.print("{s}", .{s});
if (s.len == 0 or s[s.len - 1] != '\n') {
try w.print("\n", .{});
Expand All @@ -57,7 +57,7 @@ test "file tests" {
const w = buf.writer();
var formatter = try Formatter(@TypeOf(w)).init(w, tt.comment);
for (tt.files) |f| {
try formatter.write_file(f);
try formatter.writeFile(f);
}

const want_src = tt.canonical_src orelse tt.src;
Expand Down
20 changes: 10 additions & 10 deletions src/txtar.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
//!
//! ## Writing txtar files
//!
//! Use `new_formatter` to write txtar files to a writer.
//! Use `newFormatter` to write txtar files to a writer.
//!
//! ```
//! var file: std.fs.File = // ...
//! var f = try txtar.new_formatter(file.writer(), "This is a comment.");
//! try f.write_file(.{
//! var f = try txtar.newFormatter(file.writer(), "This is a comment.");
//! try f.writeFile(.{
//! .name = "foo/bar.txt",
//! .contents = "Hello, world!\n",
//! });
Expand Down Expand Up @@ -102,8 +102,8 @@
//! const extractor = try txtar.Extractor.init(allocator, dir);
//! defer extractor.deinit();
//!
//! try extractor.write_file(txtar.File{ ... });
//! try extractor.write_file(txtar.File{ ... });
//! try extractor.writeFile(txtar.File{ ... });
//! try extractor.writeFile(txtar.File{ ... });
//! ```

const std = @import("std");
Expand All @@ -117,17 +117,17 @@ pub const Extractor = @import("./Extractor.zig");
/// Constructs a Formatter from a writer with a known type.
///
/// This can be more convenient than instantiating the Formatter type directly.
pub fn new_formatter(writer: anytype, comment: ?[]const u8) !Formatter(@TypeOf(writer)) {
pub fn newFormatter(writer: anytype, comment: ?[]const u8) !Formatter(@TypeOf(writer)) {
return try Formatter(@TypeOf(writer)).init(writer, comment);
}

test new_formatter {
test newFormatter {
var buf = std.ArrayList(u8).init(std.testing.allocator);
defer buf.deinit();

var f = try new_formatter(buf.writer(), "comment");
try f.write_file(.{ .name = "foo.txt", .contents = "foo" });
try f.write_file(.{ .name = "bar.txt", .contents = "bar\n" });
var f = try newFormatter(buf.writer(), "comment");
try f.writeFile(.{ .name = "foo.txt", .contents = "foo" });
try f.writeFile(.{ .name = "bar.txt", .contents = "bar\n" });

try std.testing.expectEqualStrings(
\\comment
Expand Down

0 comments on commit e866d27

Please sign in to comment.