1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-21 15:44:10 +00:00

- Updated tests (all passing)

- Resolved `init()`  comment stating an error occured if nothing was passed in.
- Replaced error at an unreachable location in `init()` to `unreachable`
- Made many members private
This commit is contained in:
Brook Jeynes 2023-12-31 10:27:13 +10:00
parent 9fd8c13b32
commit 9cc371e567
2 changed files with 16 additions and 16 deletions

View file

@ -9,18 +9,17 @@ const util = @import("util.zig");
pub const Mustache = struct { pub const Mustache = struct {
const Self = @This(); const Self = @This();
pub const struct_mustache_s = opaque {}; const struct_mustache_s = opaque {};
pub const enum_mustache_error_en = c_uint; const mustache_s = struct_mustache_s;
pub const mustache_error_en = enum_mustache_error_en; const enum_mustache_error_en = c_uint;
const mustache_error_en = enum_mustache_error_en;
pub const mustache_s = struct_mustache_s; extern fn fiobj_mustache_new(args: MustacheLoadArgsFio) ?*mustache_s;
pub extern fn fiobj_mustache_new(args: MustacheLoadArgsFio) ?*mustache_s; extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ;
pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ; extern fn fiobj_mustache_build2(dest: fio.FIOBJ, mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ;
pub extern fn fiobj_mustache_build2(dest: fio.FIOBJ, mustache: ?*mustache_s, data: fio.FIOBJ) fio.FIOBJ; extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
pub extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
/// Load arguments used when creating a new Mustache instance. /// Load arguments used when creating a new Mustache instance.
/// One source of data must be passed in, otherwise an error will occur.
pub const MustacheLoadArgs = struct { pub const MustacheLoadArgs = struct {
/// Filename. This enables partial templates on filesystem. /// Filename. This enables partial templates on filesystem.
filename: ?[]const u8 = null, filename: ?[]const u8 = null,
@ -94,7 +93,7 @@ pub const Mustache = struct {
11 => return Error.MUSTACHE_ERR_USER_ERROR, 11 => return Error.MUSTACHE_ERR_USER_ERROR,
else => return Error.MUSTACHE_ERR_UNKNOWN, else => return Error.MUSTACHE_ERR_UNKNOWN,
} }
return Error.MUSTACHE_ERR_UNKNOWN; unreachable;
} }
/// Convenience function to create a new `Mustache` instance with in-memory data loaded; /// Convenience function to create a new `Mustache` instance with in-memory data loaded;

View file

@ -24,20 +24,21 @@ const data = .{
test "mustacheData" { test "mustacheData" {
const template = "{{=<< >>=}}* Users:\n<<#users>><<id>>. <<& name>> (<<name>>)\n<</users>>\nNested: <<& nested.item >>."; const template = "{{=<< >>=}}* Users:\n<<#users>><<id>>. <<& name>> (<<name>>)\n<</users>>\nNested: <<& nested.item >>.";
const p = try zap.mustacheData(template);
defer zap.mustacheFree(p);
const ret = zap.mustacheBuild(p, data); var mustache = try zap.Mustache.fromData(template);
defer mustache.deinit();
const ret = mustache.build(data);
defer ret.deinit(); defer ret.deinit();
try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.", ret.str().?); try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.", ret.str().?);
} }
test "mustacheLoad" { test "mustacheLoad" {
const p = try zap.mustacheLoad("./src/tests/testtemplate.html"); var mustache = try zap.Mustache.fromFile("./src/tests/testtemplate.html");
defer zap.mustacheFree(p); defer mustache.deinit();
const ret = zap.mustacheBuild(p, data); const ret = mustache.build(data);
defer ret.deinit(); defer ret.deinit();
try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.\n", ret.str().?); try std.testing.expectEqualSlices(u8, "* Users:\n1. Rene (Rene)\n6. Caro (Caro)\nNested: nesting works.\n", ret.str().?);