pub struct Workspace { /* private fields */ }Expand description
Editable view of an apimock workspace.
§Internal layout
The Workspace holds the loaded TOML model (as a Config) plus
two index maps:
id_to_address: NodeId → where the node lives inconfig.address_to_id: reverse — used when rebuilding snapshots.
On every apply() that could move nodes around (Add / Remove /
Move), these tables are partially rebuilt. Reloading the config
discards them and re-seeds with fresh IDs.
Implementations§
Source§impl Workspace
impl Workspace
Sourcepub fn load(root: PathBuf) -> Result<Self, WorkspaceError>
pub fn load(root: PathBuf) -> Result<Self, WorkspaceError>
Load a workspace rooted at the given apimock.toml-like path.
Accepts either a direct path to the config file or the
directory containing one; a missing file-path is searched for
as apimock.toml inside root. Mirrors the CLI’s existing
resolution rules.
Sourcepub fn snapshot(&self) -> WorkspaceSnapshot
pub fn snapshot(&self) -> WorkspaceSnapshot
Build a snapshot for GUI rendering.
§Allocation cost
A snapshot fully owns its data (no borrows into the workspace) so the GUI can serialise / send / store it without lifetime gymnastics. This is O(total editable nodes) allocation per call; the GUI should call it once per edit, not once per render frame.
Sourcepub fn apply(&mut self, cmd: EditCommand) -> Result<ApplyResult, ApplyError>
pub fn apply(&mut self, cmd: EditCommand) -> Result<ApplyResult, ApplyError>
Apply one edit command, mutating the in-memory workspace.
§Shape of the implementation
Each EditCommand variant maps to a small helper method. The
helpers return Result<Vec<NodeId>, ApplyError>; apply wraps
the ok-path in an ApplyResult with the right requires_reload
flag and reruns validation so the result carries up-to-date
diagnostics.
§ID stability on structural changes
Commands that change positional layout (Remove / Delete / Move
/ Add) touch self.ids carefully so NodeIds that refer to the
same logical node survive the operation. For example, after
RemoveRuleSet { id } at index i, rule sets at positions
i+1.. shift down by one: the code below explicitly migrates
their IDs so a GUI that selected rule-set #3 before the edit
still has the same ID pointing at what is now rule-set #2.
Sourcepub fn validate(&self) -> ValidationReport
pub fn validate(&self) -> ValidationReport
Validate the workspace and return a GUI-ready report.
Uses the same per-node checks snapshot() does so the numbers
line up: a node rendered with a red underline in the snapshot
will appear in report.diagnostics with the same message.
Sourcepub fn save(&mut self) -> Result<SaveResult, SaveError>
pub fn save(&mut self) -> Result<SaveResult, SaveError>
Save the workspace back to disk. Step 4 will implement this.