authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:11:43 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-14 02:11:43 -08:00
logacc5fa4deed75353b3d36dbf81e5366ceeefba88
treec6044d08f9bf5883c82ae344912a1720cd283722
parent53ded53c8f3f8e06a8e75afdf81ad62d389d1311

zig: add util/modfile


2 files changed, 56 insertions(+), 0 deletions(-)

src/util/index.zig+1
...@@ -3,3 +3,4 @@ usingnamespace @import("./ansi.zig");...@@ -3,3 +3,4 @@ usingnamespace @import("./ansi.zig");
3usingnamespace @import("./funcs.zig");3usingnamespace @import("./funcs.zig");
4usingnamespace @import("./dep_type.zig");4usingnamespace @import("./dep_type.zig");
5usingnamespace @import("./dep.zig");5usingnamespace @import("./dep.zig");
6usingnamespace @import("./modfile.zig");
src/util/modfile.zig created+55
...@@ -0,0 +1,55 @@
1const std = @import("std");
2
3const u = @import("index.zig");
4const yaml = @import("./yaml.zig");
5
6//
7//
8
9const b = 1;
10const kb = b * 1024;
11const mb = kb * 1024;
12
13pub const ModFile = struct {
14 const Self = @This();
15
16 alloc: *std.mem.Allocator,
17 name: []const u8,
18 main: []const u8,
19 deps: []u.Dep,
20
21 pub fn init(alloc: *std.mem.Allocator, fpath: []const u8) !Self {
22 //
23 const mpath = try std.fs.realpathAlloc(alloc, fpath);
24 const file = try std.fs.openFileAbsolute(mpath, .{});
25 defer file.close();
26 const input = try file.reader().readAllAlloc(alloc, mb);
27 const doc = try yaml.parse(alloc, input);
28
29 const name = doc.mapping.get("name").?.string;
30 const main = doc.mapping.get("main").?.string;
31
32 const dep_list = &std.ArrayList(u.Dep).init(alloc);
33 if (doc.mapping.get("dependencies")) |dep_seq| {
34 if (dep_seq == .sequence) {
35 for (dep_seq.sequence) |item| {
36 const dtype = item.mapping.get("type").?.string;
37 const path = item.mapping.get("path").?.string;
38 const dep_type = std.meta.stringToEnum(u.DepType, dtype).?;
39
40 try dep_list.append(u.Dep{
41 .type = dep_type,
42 .path = path,
43 });
44 }
45 }
46 }
47
48 return Self{
49 .alloc = alloc,
50 .name = name,
51 .main = main,
52 .deps = dep_list.items,
53 };
54 }
55};