Skip to content

Commit

Permalink
Add comments for public functions and tweak readme to reflect new api
Browse files Browse the repository at this point in the history
  • Loading branch information
pwbh committed Aug 30, 2024
1 parent 7a026d2 commit ad6ab48
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Easiest way to use ymlz is to fetch it via `zig fetch`, **make sure provide it the url of latest released version as the argument**. See an example below.

```bash
zig fetch --save https://github.com/pwbh/ymlz/archive/refs/tags/0.0.3.tar.gz
zig fetch --save https://github.com/pwbh/ymlz/archive/refs/tags/0.1.0.tar.gz
```

Now in your `build.zig` we need to import ymlz as a module the following way:
Expand Down Expand Up @@ -85,7 +85,8 @@ const Tester = struct {
},
},
};

/// Usage
/// zig build run -- ./file.yml
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
Expand All @@ -98,9 +99,17 @@ pub fn main() !void {
return error.NoPathArgument;
}


const yml_location = args[1];

const yml_path = try std.fs.cwd().realpathAlloc(
allocator,
yml_location,
);
defer allocator.free(yml_path);

var ymlz = try Ymlz(Tester).init(allocator);
const result = try ymlz.load(yml_location);
const result = try ymlz.loadFile(yml_path);
defer ymlz.deinit(result);

// We can print and see that all the fields have been loaded
Expand Down
9 changes: 8 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ pub fn main() !void {
}

const yml_location = args[1];

const yml_path = try std.fs.cwd().realpathAlloc(
allocator,
yml_location,
);
defer allocator.free(yml_path);

var ymlz = try Ymlz(Tester).init(allocator);
const result = try ymlz.load(yml_location);
const result = try ymlz.loadFile(yml_path);
defer ymlz.deinit(result);

std.debug.print("Tester: {any}\n", .{result});
Expand Down
4 changes: 4 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub fn Ymlz(comptime Destination: type) type {
self.deinitRecursively(st);
}

/// Uses absolute path for the yml file path. Can be used in conjunction
/// such as `std.fs.cwd()` in order to create relative paths.
/// See Github README for example.
pub fn loadFile(self: *Self, yml_path: []const u8) !Destination {
const file = try std.fs.openFileAbsolute(yml_path, .{ .mode = .read_only });
defer file.close();
Expand All @@ -67,6 +70,7 @@ pub fn Ymlz(comptime Destination: type) type {
return std.fs.File.read(file.*, buf);
}

/// Allows passing a reader which will be used to parse your raw yml bytes.
pub fn loadReader(self: *Self, reader: AnyReader) !Destination {
if (@typeInfo(Destination) != .Struct) {
@panic("ymlz only able to load yml files into structs");
Expand Down

0 comments on commit ad6ab48

Please sign in to comment.