diff --git a/src/ui/project.zig b/src/ui/project.zig new file mode 100644 index 0000000..0fd0edc --- /dev/null +++ b/src/ui/project.zig @@ -0,0 +1,79 @@ +//! 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"); + +pub const Error = error{ ProjectFailed, OutOfMemory }; + +pub const Opened = struct { + rules_file: []const u8, + input_path: []const u8, + current_path: []const u8, + history_pairs: usize, + snap: snapshot.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_path: []const u8, + diags: *diag.List, +) Error!Opened { + const manifest_text = manifest.readText(arena, io, base, manifest_path, diags) catch + return error.ProjectFailed; + const loaded = manifest.load(arena, manifest_path, manifest_text, diags) catch + return error.ProjectFailed; + loaded.verifyInputRoot(io, base, diags) catch return error.ProjectFailed; + + var root = base.openDir(io, loaded.input_path, .{ .iterate = true }) catch { + const message = try std.fmt.allocPrint( + arena, + "input root '{s}' cannot be opened", + .{loaded.input_path}, + ); + try diags.add(.{ .message = message, .file = loaded.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, + manifest_path, + loaded.rules_file, + root, + found, + diags, + ) catch return error.ProjectFailed; + + return .{ + .rules_file = loaded.rules_file, + .input_path = loaded.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, loaded.rules_file, snap.rules, diags) catch null, + }; +} diff --git a/src/ui/root.zig b/src/ui/root.zig index 0054701..8e755ff 100644 --- a/src/ui/root.zig +++ b/src/ui/root.zig @@ -4,10 +4,12 @@ pub const server = @import("server.zig"); pub const state = @import("state.zig"); pub const session = @import("session.zig"); pub const editor = @import("editor.zig"); +pub const project = @import("project.zig"); test { _ = server; _ = state; _ = session; _ = editor; + _ = project; } diff --git a/src/ui/session.zig b/src/ui/session.zig index 7799ff2..6e768ef 100644 --- a/src/ui/session.zig +++ b/src/ui/session.zig @@ -20,11 +20,10 @@ const std = @import("std"); const diag = @import("../diag.zig"); -const discover = @import("../discover.zig"); const editor_mod = @import("editor.zig"); const engine = @import("../engine.zig"); const finalize = @import("../finalize.zig"); -const manifest = @import("../config/manifest.zig"); +const project = @import("project.zig"); const snapshot = @import("../snapshot.zig"); const state = @import("state.zig"); @@ -235,54 +234,23 @@ fn load( diags: *diag.List, session: *Session, ) !void { - const manifest_text = try manifest.readText(arena, io, base, manifest_path, diags); - const loaded = try manifest.load(arena, manifest_path, manifest_text, diags); - try loaded.verifyInputRoot(io, base, diags); - - var root = base.openDir(io, loaded.input_path, .{ .iterate = true }) catch { - try diags.add(.{ - .message = try std.fmt.allocPrint( - arena, - "input root '{s}' cannot be opened", - .{loaded.input_path}, - ), - .file = loaded.input_path, - }); - return error.InvalidConfig; - }; - defer root.close(io); - - const found = try discover.discover(arena, io, root, diags); - session.view.file = found.current; - session.view.history_pairs = found.history.len; - session.rules_file = loaded.rules_file; - - session.snap = try snapshot.read( - arena, - io, - base, - manifest_path, - loaded.rules_file, - root, - found, - diags, - ); - - // M1-PROJ-018: a broken rules file leaves the session running with the - // diagnostics on show, so they can be corrected. The draft then starts - // empty, and the interface shows what is wrong with the file. - const ruleset = config_rules.load(arena, loaded.rules_file, session.snap.rules, diags) catch { + const opened = try project.open(arena, io, base, manifest_path, diags); + + session.rules_file = opened.rules_file; + session.snap = opened.snap; + session.view.file = opened.current_path; + session.view.history_pairs = opened.history_pairs; + + if (opened.ruleset) |ruleset| { + session.editor = try editor_mod.Editor.fromSaved(arena, ruleset); + } else { + // M1-PROJ-018: the rules file did not load, so the draft starts empty + // and what was wrong with the file is all there is to say about it. session.opening = try arena.dupe(diag.Diagnostic, diags.items.items); - try session.evaluate(); - return; - }; - // The file loaded, so there is nothing about it left to report. - session.editor = try editor_mod.Editor.fromSaved(arena, ruleset); + } try session.evaluate(); } -const config_rules = @import("../config/rules.zig"); - // -- tests ------------------------------------------------------------------- const expect = std.testing.expect;