diff --git a/README.md b/README.md index 7c800ed..a51d5ca 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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(); @@ -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 diff --git a/src/main.zig b/src/main.zig index 9a94e4d..9cec8af 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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}); diff --git a/src/root.zig b/src/root.zig index e89a72f..403bac9 100644 --- a/src/root.zig +++ b/src/root.zig @@ -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(); @@ -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");