summary refs log tree commit diff
path: root/src/Config.zig
blob: 51fc88527e528c2f72df92b5ef36ca330fbdfa3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Configuration
// SPDX-FileCopyrightText: 2025 Nguyễn Gia Phong
// SPDX-License-Identifier: GPL-3.0-or-later

const Allocator = std.mem.Allocator;
const cwd = std.fs.cwd;
const maxInt = std.math.maxInt;
const joinPath = std.fs.path.joinZ;
const std = @import("std");

const folders = @import("known-folders");
const zsanett = @import("zsanett");

const Config = @This();
tab_width: u8 = 8,

pub fn parse(allocator: Allocator, env: *zsanett.Table) !Config {
    if (try folders.getPath(allocator, .local_configuration)) |local_conf| {
        defer allocator.free(local_conf);
        const path = try joinPath(allocator, &.{
            local_conf,
            "kay",
            "config.janet"
        });
        defer allocator.free(path);
        if (cwd().readFileAlloc(allocator, path, maxInt(u32))) |content| {
            defer allocator.free(content);
            return try zsanett.eval(Config, env, content, path.ptr);
        } else |err| switch (err) {
            error.FileNotFound => return .{},
            else => return err,
        }
    } else return .{};
}