//! Opening a project for an interactive session (M1-PROJ-018). //! //! Split out of session.zig because opening is a different job from editing: //! this reads the manifest, pairs the files and takes the snapshot once, and //! then has nothing more to do with the session that runs on top of it. //! //! M1-PROJ-018 draws the line this module exists to draw. A rules file that //! does not load is survivable -- the interface exists to fix exactly that -- //! so `ruleset` comes back null and the diagnostics come back with it. //! Anything further up, from a missing manifest to a failed pairing, is not: //! M1-UI-009 makes the manifest read-only, so the interface could not correct //! any of it, and a server that only displayed the message would hide it. const std = @import("std"); const diag = @import("../diag.zig"); const discover = @import("../discover.zig"); const Manifest = @import("../config/Manifest.zig"); const rules = @import("../config/rules.zig"); const Snapshot = @import("../Snapshot.zig"); const Project = @This(); rules_file: []const u8, input_path: []const u8, current_path: []const u8, history_pairs: usize, snap: Snapshot, /// Null when the rules file itself did not load. The session starts with /// an empty draft and the diagnostics on show. ruleset: ?rules.Ruleset, pub fn open( arena: std.mem.Allocator, io: std.Io, base: std.Io.Dir, manifest: *const Manifest, diags: *diag.List, ) error{ OutOfMemory, ProjectFailed }!Project { var root = base.openDir(io, manifest.input_path, .{ .iterate = true }) catch { try diags.addFmt("input root '{s}' cannot be opened", .{manifest.input_path}, .{ .file = manifest.input_path }); return error.ProjectFailed; }; defer root.close(io); const found = discover.discover(arena, io, root, diags) catch return error.ProjectFailed; const snap = Snapshot.read(arena, io, .{ .base = base, .manifest_path = manifest.manifest_path, .rules_path = manifest.rules_file, .root = root, .found = found, }, diags) catch return error.ProjectFailed; return .{ .rules_file = manifest.rules_file, .input_path = manifest.input_path, .current_path = found.current, .history_pairs = found.history.len, .snap = snap, // M1-PROJ-018: survivable, unlike everything above it. .ruleset = rules.load(arena, manifest.rules_file, snap.rules, diags) catch null, }; }