From acc5fa4deed75353b3d36dbf81e5366ceeefba88 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 14 Nov 2020 02:11:43 -0800 Subject: [PATCH] zig: add util/modfile --- src/util/index.zig | 1 + src/util/modfile.zig | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/util/modfile.zig diff --git a/src/util/index.zig b/src/util/index.zig index cae5ee766ee42b8eff3b4ac0ec04b8767bbbcb73..6f3c03d5e2169d4bfc31bede1fa99c9669d1b0c0 100644 --- a/src/util/index.zig +++ b/src/util/index.zig @@ -3,3 +3,4 @@ usingnamespace @import("./ansi.zig"); usingnamespace @import("./funcs.zig"); usingnamespace @import("./dep_type.zig"); usingnamespace @import("./dep.zig"); +usingnamespace @import("./modfile.zig"); diff --git a/src/util/modfile.zig b/src/util/modfile.zig new file mode 100644 index 0000000000000000000000000000000000000000..5cf80995211f99e55a5fa3bfcdac913bb241fc99 --- /dev/null +++ b/src/util/modfile.zig @@ -0,0 +1,55 @@ +const std = @import("std"); + +const u = @import("index.zig"); +const yaml = @import("./yaml.zig"); + +// +// + +const b = 1; +const kb = b * 1024; +const mb = kb * 1024; + +pub const ModFile = struct { + const Self = @This(); + + alloc: *std.mem.Allocator, + name: []const u8, + main: []const u8, + deps: []u.Dep, + + pub fn init(alloc: *std.mem.Allocator, fpath: []const u8) !Self { + // + const mpath = try std.fs.realpathAlloc(alloc, fpath); + const file = try std.fs.openFileAbsolute(mpath, .{}); + defer file.close(); + const input = try file.reader().readAllAlloc(alloc, mb); + const doc = try yaml.parse(alloc, input); + + const name = doc.mapping.get("name").?.string; + const main = doc.mapping.get("main").?.string; + + const dep_list = &std.ArrayList(u.Dep).init(alloc); + if (doc.mapping.get("dependencies")) |dep_seq| { + if (dep_seq == .sequence) { + for (dep_seq.sequence) |item| { + const dtype = item.mapping.get("type").?.string; + const path = item.mapping.get("path").?.string; + const dep_type = std.meta.stringToEnum(u.DepType, dtype).?; + + try dep_list.append(u.Dep{ + .type = dep_type, + .path = path, + }); + } + } + } + + return Self{ + .alloc = alloc, + .name = name, + .main = main, + .deps = dep_list.items, + }; + } +}; -- 2.54.0