mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
mustache cleanup
This commit is contained in:
parent
9557bebf0b
commit
21c4fa12d3
4 changed files with 30 additions and 63 deletions
|
@ -4,8 +4,8 @@
|
||||||
|
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.@"facil.io" = .{
|
.@"facil.io" = .{
|
||||||
.url = "https://github.com/zigzap/facil.io/archive/2c04cd1949328dd62fe5d262b9cc930e54392ab8.tar.gz",
|
.url = "https://github.com/zigzap/facil.io/archive/6b19a580df3cf1baeac47b369823c5a3e22c660c.tar.gz",
|
||||||
.hash = "12209d3b552145f24431e5a2e6a4ad59ceaa9656f7fba8af7a8aa704a8784a79f55d",
|
.hash = "122025ad890e99fc225fd9bbd9fba6d8dacf7f0aec23e4a32a9b0a205de894ffe1f5",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,18 @@ fn on_request_verbose(r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
const template = "{{=<< >>=}}* Users:\r\n<<#users>><<id>>. <<& name>> (<<name>>)\r\n<</users>>\r\nNested: <<& nested.item >>.";
|
const template = "{{=<< >>=}}* Users:\r\n<<#users>><<id>>. <<& name>> (<<name>>)\r\n<</users>>\r\nNested: <<& nested.item >>.";
|
||||||
const p = zap.MustacheNew(template) catch return;
|
const p = zap.MustacheNew(template) catch return;
|
||||||
|
defer zap.MustacheFree(p);
|
||||||
const User = struct {
|
const User = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
id: isize,
|
id: isize,
|
||||||
};
|
};
|
||||||
std.debug.print("{*}\n", .{p});
|
const ret = zap.MustacheBuild(p, .{
|
||||||
if (zap.MustacheBuild(p, .{
|
|
||||||
.users = [_]User{
|
.users = [_]User{
|
||||||
User{
|
.{
|
||||||
.name = "Rene",
|
.name = "Rene",
|
||||||
.id = 1,
|
.id = 1,
|
||||||
},
|
},
|
||||||
User{
|
.{
|
||||||
.name = "Caro",
|
.name = "Caro",
|
||||||
.id = 6,
|
.id = 6,
|
||||||
},
|
},
|
||||||
|
@ -30,11 +30,13 @@ fn on_request_verbose(r: zap.SimpleRequest) void {
|
||||||
.nested = .{
|
.nested = .{
|
||||||
.item = "nesting works",
|
.item = "nesting works",
|
||||||
},
|
},
|
||||||
})) |s| {
|
});
|
||||||
|
defer ret.deinit();
|
||||||
|
if (ret.str()) |s| {
|
||||||
std.debug.print("{s}\n", .{s});
|
std.debug.print("{s}\n", .{s});
|
||||||
_ = r.sendBody(s);
|
_ = r.sendBody(s);
|
||||||
}
|
}
|
||||||
_ = r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>");
|
_ = r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_request_minimal(r: zap.SimpleRequest) void {
|
fn on_request_minimal(r: zap.SimpleRequest) void {
|
||||||
|
|
|
@ -2,11 +2,6 @@
|
||||||
// (see http://facil.io/0.7.x/fiobj_mustache)
|
// (see http://facil.io/0.7.x/fiobj_mustache)
|
||||||
// easier / possible / more zig-like
|
// easier / possible / more zig-like
|
||||||
|
|
||||||
// const C = @cImport({
|
|
||||||
// @cInclude("mustache_parser.h");
|
|
||||||
// @cInclude("fiobj_mustache.h");
|
|
||||||
// });
|
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const util = @import("util.zig");
|
const util = @import("util.zig");
|
||||||
|
|
||||||
|
@ -48,15 +43,9 @@ pub const MustacheError = error{
|
||||||
};
|
};
|
||||||
|
|
||||||
// pub extern fn fiobj_mustache_load(filename: fio_str_info_s) ?*mustache_s;
|
// pub extern fn fiobj_mustache_load(filename: fio_str_info_s) ?*mustache_s;
|
||||||
|
|
||||||
// implement these: fiobj_mustache.c
|
|
||||||
// pub extern fn fiobj_mustache_new(args: mustache_load_args_s) ?*mustache_s;
|
|
||||||
// pub extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
|
|
||||||
// pub extern fn fiobj_mustache_build(mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
|
||||||
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
||||||
|
|
||||||
pub fn MustacheNew(data: []const u8) MustacheError!*Mustache {
|
pub fn MustacheNew(data: []const u8) MustacheError!*Mustache {
|
||||||
// pub fn MustacheNew(data: []const u8) !*Mustache {
|
|
||||||
var err: mustache_error_en = undefined;
|
var err: mustache_error_en = undefined;
|
||||||
var args: MustacheLoadArgs = .{
|
var args: MustacheLoadArgs = .{
|
||||||
.filename = null,
|
.filename = null,
|
||||||
|
@ -88,14 +77,11 @@ pub fn MustacheNew(data: []const u8) MustacheError!*Mustache {
|
||||||
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
// pub extern fn fiobj_mustache_build2(dest: FIOBJ, mustache: ?*mustache_s, data: FIOBJ) FIOBJ;
|
||||||
|
|
||||||
pub extern fn fiobj_hash_new() FIOBJ;
|
pub extern fn fiobj_hash_new() FIOBJ;
|
||||||
pub extern fn fiobj_free(arg_o: FIOBJ) callconv(.C) void;
|
|
||||||
pub extern fn fiobj_hash_set(hash: FIOBJ, key: FIOBJ, obj: FIOBJ) c_int;
|
pub extern fn fiobj_hash_set(hash: FIOBJ, key: FIOBJ, obj: FIOBJ) c_int;
|
||||||
pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void;
|
pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void;
|
||||||
pub extern fn fiobj_float_new(num: f64) FIOBJ;
|
pub extern fn fiobj_float_new(num: f64) FIOBJ;
|
||||||
pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ;
|
pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ;
|
||||||
|
pub extern fn fiobj_free_wrapped(o: FIOBJ) callconv(.C) void;
|
||||||
// pub extern fn fiobj_num_new(num: isize) callconv(.C) FIOBJ;
|
|
||||||
|
|
||||||
pub const FIOBJ_T_TRUE: c_int = 22;
|
pub const FIOBJ_T_TRUE: c_int = 22;
|
||||||
pub const FIOBJ_T_FALSE: c_int = 38;
|
pub const FIOBJ_T_FALSE: c_int = 38;
|
||||||
pub fn fiobj_true() callconv(.C) FIOBJ {
|
pub fn fiobj_true() callconv(.C) FIOBJ {
|
||||||
|
@ -108,50 +94,29 @@ pub extern fn fiobj_ary_new2(capa: usize) FIOBJ;
|
||||||
pub extern fn fiobj_str_new(str: [*c]const u8, len: usize) FIOBJ;
|
pub extern fn fiobj_str_new(str: [*c]const u8, len: usize) FIOBJ;
|
||||||
pub extern fn fiobj_str_buf(capa: usize) FIOBJ;
|
pub extern fn fiobj_str_buf(capa: usize) FIOBJ;
|
||||||
|
|
||||||
|
const MustacheBuildResult = struct {
|
||||||
|
fiobj_result: FIOBJ = 0,
|
||||||
|
|
||||||
|
pub fn deinit(m: *const MustacheBuildResult) void {
|
||||||
|
fiobj_free_wrapped(m.fiobj_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn str(m: *const MustacheBuildResult) ?[]const u8 {
|
||||||
|
return util.fio2str(m.fiobj_result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// this build is slow because it needs to translate to a FIOBJ data
|
// this build is slow because it needs to translate to a FIOBJ data
|
||||||
// object FIOBJ_T_HASH
|
// object FIOBJ_T_HASH
|
||||||
pub fn MustacheBuild(mustache: *Mustache, data: anytype) ?[]const u8 {
|
pub fn MustacheBuild(mustache: *Mustache, data: anytype) MustacheBuildResult {
|
||||||
|
|
||||||
// FIOBJ data = fiobj_hash_new();
|
|
||||||
// FIOBJ key = fiobj_str_new("users", 5);
|
|
||||||
// FIOBJ ary = fiobj_ary_new2(4);
|
|
||||||
// fiobj_hash_set(data, key, ary);
|
|
||||||
// fiobj_free(key);
|
|
||||||
// for (int i = 0; i < 4; ++i) {
|
|
||||||
// FIOBJ id = fiobj_str_buf(4);
|
|
||||||
// fiobj_str_write_i(id, i);
|
|
||||||
// FIOBJ name = fiobj_str_buf(4);
|
|
||||||
// fiobj_str_write(name, "User ", 5);
|
|
||||||
// fiobj_str_write_i(name, i);
|
|
||||||
// FIOBJ usr = fiobj_hash_new2(2);
|
|
||||||
// key = fiobj_str_new("id", 2);
|
|
||||||
// fiobj_hash_set(usr, key, id);
|
|
||||||
// fiobj_free(key);
|
|
||||||
// key = fiobj_str_new("name", 4);
|
|
||||||
// fiobj_hash_set(usr, key, name);
|
|
||||||
// fiobj_free(key);
|
|
||||||
// fiobj_ary_push(ary, usr);
|
|
||||||
// }
|
|
||||||
// key = fiobj_str_new("nested", 6);
|
|
||||||
// ary = fiobj_hash_new2(2);
|
|
||||||
// fiobj_hash_set(data, key, ary);
|
|
||||||
// fiobj_free(key);
|
|
||||||
// key = fiobj_str_new("item", 4);
|
|
||||||
// fiobj_hash_set(ary, key, fiobj_str_new("dot notation success", 20));
|
|
||||||
// fiobj_free(key);
|
|
||||||
|
|
||||||
const T = @TypeOf(data);
|
const T = @TypeOf(data);
|
||||||
if (@typeInfo(T) != .Struct) {
|
if (@typeInfo(T) != .Struct) {
|
||||||
@compileError("No struct: '" ++ @typeName(T) ++ "'");
|
@compileError("No struct: '" ++ @typeName(T) ++ "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// std.debug.print("data: ", .{});
|
|
||||||
const fiobj_data = fiobjectify(data);
|
const fiobj_data = fiobjectify(data);
|
||||||
// std.debug.print("{any}\n", .{fiobj_data});
|
|
||||||
|
|
||||||
// TODO: fiobj_free everything
|
return .{ .fiobj_result = fiobj_mustache_build(mustache, fiobj_data) };
|
||||||
var ret = fiobj_mustache_build(mustache, fiobj_data);
|
|
||||||
return util.fio2str(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fiobjectify(
|
pub fn fiobjectify(
|
||||||
|
@ -209,6 +174,7 @@ pub fn fiobjectify(
|
||||||
const fvalue = fiobjectify(v);
|
const fvalue = fiobjectify(v);
|
||||||
// std.debug.print(" fiobj value: {any}\n", .{fvalue});
|
// std.debug.print(" fiobj value: {any}\n", .{fvalue});
|
||||||
_ = fiobj_hash_set(m, fname, fvalue);
|
_ = fiobj_hash_set(m, fname, fvalue);
|
||||||
|
fiobj_free_wrapped(fname);
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
},
|
},
|
||||||
|
@ -250,7 +216,6 @@ pub fn fiobjectify(
|
||||||
unreachable;
|
unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub extern fn fiobj_mustache_free(mustache: ?*mustache_s) void;
|
|
||||||
pub fn MustacheFree(m: ?*Mustache) void {
|
pub fn MustacheFree(m: ?*Mustache) void {
|
||||||
fiobj_mustache_free(m);
|
fiobj_mustache_free(m);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub const C = @cImport({
|
pub const Cutil = @cImport({
|
||||||
@cInclude("http.h");
|
@cInclude("http.h");
|
||||||
@cInclude("fio.h");
|
@cInclude("fio.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn fio2str(o: C.FIOBJ) ?[]const u8 {
|
pub fn fio2str(o: Cutil.FIOBJ) ?[]const u8 {
|
||||||
if (o == 0) return null;
|
if (o == 0) return null;
|
||||||
const x: C.fio_str_info_s = C.fiobj_obj2cstr(o);
|
const x: Cutil.fio_str_info_s = Cutil.fiobj_obj2cstr(o);
|
||||||
return std.mem.span(x.data);
|
return std.mem.span(x.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn str2fio(s: []const u8) C.fio_str_info_s {
|
pub fn str2fio(s: []const u8) Cutil.fio_str_info_s {
|
||||||
return .{
|
return .{
|
||||||
.data = toCharPtr(s),
|
.data = toCharPtr(s),
|
||||||
.len = s.len,
|
.len = s.len,
|
||||||
|
|
Loading…
Add table
Reference in a new issue