commit a985ef99872f458e3eacbb1eb1674e1dec098d11
parent 6d0d2f300c1b2b88ddadcb2aa263ad0dd1c219b6
Author: Christian Ermann <christianermann@gmail.com>
Date: Sat, 28 Feb 2026 13:13:52 -0800
Add missing types
Diffstat:
| M | src/main.zig | | | 218 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 209 insertions(+), 9 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -17,6 +17,7 @@ const Api = struct {
objects: []Object,
};
+
const Value64 = union(enum) {
int: u64,
float: f64,
@@ -56,22 +57,221 @@ const Value64 = union(enum) {
}
};
+const PrimitiveType = enum {
+ c_void,
+ @"bool",
+ nullable_string,
+ string_with_default_empty,
+ out_string,
+ uint16,
+ uint32,
+ uint64,
+ @"usize",
+ int16,
+ int32,
+ float32,
+ nullable_float32,
+ float64,
+ float64_supertype,
+ @"array<bool>",
+ @"array<string>",
+ @"array<uint16>",
+ @"array<uint32>",
+ @"array<uint64>",
+ @"array<usize>",
+ @"array<int16>",
+ @"array<int32>",
+ @"array<float32>",
+ @"array<float64>",
+};
+
+const ComplexType = union(enum) {
+ single: TypeInfo,
+ array: TypeInfo,
+
+ const Category = enum {
+ typedef,
+ @"enum",
+ bitflag,
+ @"struct",
+ function_type,
+ object
+ };
+
+ const TypeInfo = struct {
+ category: Category,
+ name: []const u8,
+ };
+
+ pub fn parse(raw: []const u8) ?ComplexType {
+ var array = false;
+ var type_str = raw;
+ if (std.mem.startsWith(u8, raw, "array<")) {
+ array = true;
+ type_str = raw[6..raw.len - 1];
+ }
+ const dot_idx = std.mem.indexOfScalar(u8, type_str, '.') orelse {
+ return null;
+ };
+ const category = std.meta.stringToEnum(
+ Category,
+ type_str[0..dot_idx],
+ ) orelse return null;
+ const name = type_str[dot_idx + 1..];
+ const type_info = TypeInfo{
+ .category = category,
+ .name = name,
+ };
+ if (array) {
+ return .{ .array = type_info };
+ } else {
+ return .{ .single = type_info };
+ }
+ }
+
+ pub fn jsonParse(
+ allocator: std.mem.Allocator,
+ source: anytype,
+ options: std.json.ParseOptions,
+ ) !ComplexType {
+ _ = allocator;
+ _ = options;
+ const token = try source.next();
+ return switch (token) {
+ .string => |inner| {
+ if (parse(inner)) |val| {
+ return val;
+ } else {
+ return error.UnexpectedToken;
+ }
+ return parse(inner);
+ },
+ else => error.UnexpectedToken,
+ };
+ }
+};
+
+const CallbackType = struct {
+ name: []const u8,
+
+ pub fn parse(raw: []const u8) ?CallbackType {
+ if (!std.mem.startsWith(u8, raw, "callback.")) {
+ return null;
+ }
+ const name = raw[9..];
+ return .{ .name = name };
+ }
+
+ pub fn jsonParse(
+ allocator: std.mem.Allocator,
+ source: anytype,
+ options: std.json.ParseOptions,
+ ) !CallbackType {
+ _ = allocator;
+ _ = options;
+ const token = try source.next();
+ return switch (token) {
+ .string => |inner| {
+ if (parse(inner)) |val| {
+ return val;
+ } else {
+ return error.UnexpectedToken;
+ }
+ return parse(inner);
+ },
+ else => error.UnexpectedToken,
+ };
+ }
+};
+
+const Type = union(enum) {
+ primitive: PrimitiveType,
+ complex: ComplexType,
+ callback: CallbackType,
+
+ pub fn jsonParse(
+ allocator: std.mem.Allocator,
+ source: anytype,
+ options: std.json.ParseOptions,
+ ) !Type {
+ _ = allocator;
+ _ = options;
+ const token = try source.next();
+ return switch (token) {
+ .string => |inner| {
+ if (std.meta.stringToEnum(PrimitiveType, inner)) |val| {
+ return .{ .primitive = val };
+ }
+ if (ComplexType.parse(inner)) |val| {
+ return .{ .complex = val };
+ } else {}
+ if (CallbackType.parse(inner)) |val| {
+ return .{ .callback = val };
+ }
+ return error.UnexpectedToken;
+ },
+ else => error.UnexpectedToken,
+ };
+ }
+};
+
+const Pointer = enum {
+ immutable,
+ mutable,
+};
+
+const CallbackStyle = enum {
+ callback_mode,
+ immediate,
+};
+
+const DefaultValue = union(enum) {
+ string: []const u8,
+ number: u64,
+ @"bool": bool,
+
+ pub fn jsonParse(
+ allocator: std.mem.Allocator,
+ source: anytype,
+ options: std.json.ParseOptions,
+ ) !DefaultValue {
+ _ = allocator;
+ _ = options;
+ const token = try source.next();
+ return switch (token) {
+ .number => |inner| {
+ const val = try std.fmt.parseInt(u64, inner, 10);
+ return .{ .number = val };
+ },
+ .string => |inner| {
+ return .{ .string = inner };
+ },
+ .true => {
+ return .{ .bool = true };
+ },
+ .false => {
+ return .{ .bool = false };
+ },
+ else => error.UnexpectedToken,
+ };
+ }
+};
const ParameterType = struct {
name: []const u8,
doc: []const u8,
type: []const u8,
passed_with_ownership: ?bool = null,
- pointer: ?[]const u8 = null,
+ pointer: ?Pointer = null,
optional: ?bool = null,
- // TODO: default: ... ,
+ default: ?DefaultValue = null,
};
const TypeDef = struct {
name: []const u8,
namespace: ?[]const u8 = null,
doc: []const u8,
- type: []const u8,
+ type: PrimitiveType,
};
const Constant = struct {
@@ -126,16 +326,16 @@ const Callback = struct {
name: []const u8,
namespace: ?[]const u8 = null,
doc: []const u8,
- callback_style: ?[]const u8 = null,
- // TODO: args: ... ,
+ style: CallbackStyle,
+ args: ?[]ParameterType = null,
};
const ReturnType = struct {
doc: []const u8,
- type: []const u8,
+ type: Type,
optional: ?bool = null,
passed_with_ownership: ?bool = null,
- pointer: ?[]const u8 = null,
+ pointer: ?Pointer = null,
};
const Function = struct {
@@ -143,8 +343,8 @@ const Function = struct {
namespace: ?[]const u8 = null,
doc: []const u8,
returns: ?ReturnType = null,
- callback: ?[]const u8 = null,
- // TODO: args ... ,
+ callback: ?CallbackType = null,
+ args: ?[]ParameterType = null,
};
const Object = struct {