const std = @import("std");

const Class = @import("Class.zig");

pub fn main(init: std.process.Init) !void {
    const io = init.io;
    const allocator = init.gpa;

    var args = try init.minimal.args.iterateAllocator(init.arena.allocator());
    defer args.deinit();

    var stderr_writer: std.Io.File.Writer = .init(.stderr(), io, &.{});
    const stderr = &stderr_writer.interface;

    var stdout_buf: [1024]u8 = undefined;
    var stdout_writer: std.Io.File.Writer = .init(.stdout(), io, &stdout_buf);
    const stdout = &stdout_writer.interface;

    std.debug.assert(args.skip());
    const file_path = if (args.next()) |fp| fp else {
        try stderr.writeAll("No file argument provided.\n");
        return;
    };

    const file = std.Io.Dir.cwd().openFile(io, file_path, .{}) catch |err| switch (err) {
        error.FileNotFound => {
            try stderr.writeAll("File not found.\n");
            return;
        },
        error.IsDir => {
            try stderr.writeAll("Path is a directory.\n");
            return;
        },
        else => return err,
    };
    defer file.close(io);

    var file_buf: [1024]u8 = undefined;
    var file_reader = file.reader(io, &file_buf);
    const reader = &file_reader.interface;

    var class = try Class.parse(reader, allocator);
    defer class.deinit();

    try stdout.print("{f}", .{ class });

    try stdout.flush();
}

