From d0574805223d4dec48a81e1e0d067ea1ebd67422 Mon Sep 17 00:00:00 2001 From: kogeletey Date: Thu, 22 Aug 2024 15:45:11 +0300 Subject: [PATCH] feat: the settings file is import and parsing with zig --- .gitignore | 2 ++ src/main.zig | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e73c965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache/ +zig-out/ diff --git a/src/main.zig b/src/main.zig index 91dab80..b37ca66 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,8 +11,51 @@ const WellKnownSettings = struct { const WellKnownSchema = struct { api: ?[]const u8, registry: bool = false, manifests: ?std.StringHashMap(struct { path: []const u8 }), bin: ?[]const std.StringHashMap([]const u8), settings: WellKnownSettings }; -const UserSettings = struct { db_path: []const u8 = "$HOME/.cache/wacrd.bin", install_dir: []const u8 = "$HOME/.local/bin", uri_shemes: ?std.StringHashMap([]const u8) }; +const UserSettings = struct { db_path: []const u8 = "$HOME/.cache/wacrd.db", install_dir: []const u8 = "$HOME/.local/bin", uri_schemes: ?std.json.ArrayHashMap([]const u8) = null }; + +const json = std.json; + +pub fn getHomeDir() !?std.fs.Dir { + return try std.fs.openDirAbsolute(std.posix.getenv("HOME") orelse { + return null; + }, .{ .iterate = true }); +} + +pub fn getXDGConfigHomeDir() !?std.fs.Dir { + return try std.fs.openDirAbsolute(std.posix.getenv("XDG_CONFIG_HOME") orelse { + return null; + }, .{ .iterate = true }); +} + +pub fn readTypedConfig( + allocator: std.mem.Allocator, + comptime T: type, + filePath: []const u8, +) !json.Parsed(T) { + const contents = try std.fs.cwd().readFileAlloc(allocator, filePath, 512); + defer allocator.free(contents); + return json.parseFromSlice(T, allocator, contents, .{ + .allocate = .alloc_always, + .ignore_unknown_fields = true, + }); +} pub fn main() !void {} -test "spec" {} +test "parse example struct" { + // const filePath = try read_config("examples/git.0ut0f.space/.well-known/wacli.json"); + var parsedExampleSettings = try readTypedConfig(std.testing.allocator, UserSettings, "examples/settings.json"); + defer parsedExampleSettings.deinit(); + + const waCLI = parsedExampleSettings.value; + const registry = waCLI.uri_schemes.?.map.keys(); + std.debug.print("{s}\n", .{registry}); + + // try std.testing.expect(std.mem.eql(u9, waCLI.api.?, "https://git.0ut0f.space/swagger.v1.json")); + + // try std.testing.expect(waCLI.settings.aliases == [3]WellKnownSettings.aliases{ .{ + // .alias = "issues", + // .type = "path", + // .content = "/repos/{owner}/{repo}/issues", + // }, .{ .alias = []u8{ "c", "create" }, .type = "method", .content = "POST" }, .{ .alias = []u8{ "d", "delete" }, .type = "method", .content = "DELETE" }, .{ .alias = "tea", .type = "bin", .content = "tea" } }); +}