From 6da2a5d43ed671419b51900acc72547aedb3aa64 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sat, 24 Oct 2020 21:44:56 +0200 Subject: take an options var in init() --- src/main.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index f83f316..2d6ca46 100644 --- a/src/main.zig +++ b/src/main.zig @@ -15,11 +15,11 @@ const logger = std.log.scoped(.sqlite); /// /// // File database /// var db: sqlite.Db = undefined; -/// try db.init(allocator, .{ .File = "/tmp/data.db" }); +/// try db.init(allocator, .{ .mode = { .File = "/tmp/data.db" } }); /// /// // In memory database /// var db: sqlite.Db = undefined; -/// try db.init(allocator, .{ .Memory={} }); +/// try db.init(allocator, .{ .mode = { .Memory = {} } }); /// pub const Db = struct { const Self = @This(); @@ -34,9 +34,11 @@ pub const Db = struct { }; /// init creates a database with the provided `mode`. - pub fn init(self: *Self, allocator: *mem.Allocator, mode: Mode) !void { + pub fn init(self: *Self, allocator: *mem.Allocator, options: anytype) !void { self.allocator = allocator; + const mode = if (@hasField(@TypeOf(options), "mode")) options.mode else .Memory; + switch (mode) { .File => |path| { logger.info("opening {}", .{path}); @@ -388,7 +390,7 @@ test "sqlite: statement exec" { var allocator = &arena.allocator; var db: Db = undefined; - try db.init(testing.allocator, dbMode()); + try db.init(testing.allocator, .{ .mode = dbMode() }); // Create the tables -- cgit v1.2.3