Tool Library: A Module for Every Branch

Design problem and decision

  • Design problem The library has several branches, opened and closed over time. Each has its own shelf of tools.
  • Viewer's decision Is a branch a row in someone's table, or a module of its own? What follows from each answer?
  • Outcome The viewer can tell a keyed set of child modules from a map of rows, name the three consequences of choosing child modules, and require a bound and a closing rule in the brief.
  • Starting point Lesson 2 left the owning tree and the call graph on screen, with one Shelf hanging under Branches on key 3.
  • Handoff Lesson 4 shows how this tree comes to exist and how it is found again.
  • Pacing Three and a half minutes.

Worked design

Tool Library is illustrative. State carried in from lesson 2:

Module Saved state
Branches keyed children: 3 → Shelf
Shelf of branch 3 17 drill: 4 owned, 1 available. 22 ladder: 1, 0. 30 saw: 2, 1.
Loans five rows, each carrying the branch number 3
Call Outcome State after
open_branch(5) true Branches has keys 3 and 5. Shelf 5 is empty.
add_tool(5, 40, 2) true Shelf 5: tool 40, owned 2, available 2
close_branch(5) true key 5 gone, with its shelf and tool 40
open_branch(5) true a new Shelf 5, empty
close_branch(3) Refused(ToolsOut) nothing changes

The app allows at most 32 branches.

The story in one pass

Ask whether a branch is a row or a module. Show the keyed set of child modules and the one line that declares it. Put it beside a map of rows and compare who can read inside. Open a branch, stock it, close it, and reopen it empty. Try to close a branch with tools out and be refused. End on how other modules refer to a branch: by its number, through its owner.

Views and visual objects

Use the shared visual language. The keyed fan is part of OwnTree.

View Framing and anchor
branches Close on the Branches capsule and everything under it. Room for three child capsules side by side.
compare Split screen. Left: Branches with its keyed fan. Right: Members with its open vault table.
tree The fixed app layout from lesson 2.

Scene row_or_module — Two ways to hold many things

Already on screen The tree view from lesson 2. Call lines dimmed.

Beat question — What is a branch

View and focus tree, then push in to branches.

Narration

The library has many members and many branches. Members are rows in one table. Branches are different. Each branch is a module of its own, with its own sealed shelf. Why the difference?

Cue and animation On “Members are rows”, the Members vault opens to show 501: 3, 502: 2. On “Branches are different”, the camera pushes in on Branches and the Shelf under key 3. On “Why the difference”, a small question mark sits between the two.

Leaves with branches view. One Shelf under key 3.

Beat keyed_set — An attachment, not data

View and focus branches. Focus on the Branches vault.

Show One snippet, tabbed branches/memory/v1.rs — illustrative, modelled on Shop's branches version file. AppTree small at left with that file highlighted.

pub struct Children {
    pub(in crate::branches) shelves:
        ChildMap<u64, crate::shelf::memory::v1::Memory>,
}

Narration

Branches owns a keyed set of child modules. One key per branch, one Shelf module per key. It is declared in the version file, beside the data, as children. And it names the exact version of Shelf it holds. Remember that. It matters when Shelf changes.

Cue and animation On “keyed set”, the edge to Shelf redraws as the first rib of a fan, with empty ribs faintly behind it. On “as children”, the word Children highlights. On “exact version”, shelf::memory::v1 highlights and the v1 tab on the Shelf vault pulses.

Leaves with The fan with one filled rib. Snippet fades.

Beat compare — Who can read inside

View and focus compare.

Hold PauseAndExplain with two short columns under the two halves:

Keyed child modules            Map of rows
each value is a sealed module  each value is a row
only Shelf reads a shelf       Members reads every row
removed as a whole             removed row by row
has its own version files      changes with its owner

Narration

Here is the difference. Members can read every one of its rows. Branches cannot read inside a shelf. It can only attach one, remove one, and call its public functions. A branch is opened and closed as a whole. It holds a table of its own. That is what earns it a module.

Cue and animation On “read every one of its rows”, the key badge in Members turns and its rows light. On “cannot read inside”, a grey pill from Branches' band stops at the Shelf seal. On “attach one, remove one”, the rib pulses. Balloon lines appear in step.

Leaves with branches view. The balloon closed.

Scene lifetime — Opening and closing

Already on screen branches view. Fan with key 3.

Beat open_five — A new module on a new key

View and focus branches.

Narration

An admin opens branch five. Branches creates a new, empty Shelf and attaches it on key five, in the same call. Then a tool is added to it.

Cue and animation On “opens branch five”, the pill open_branch(5) docks at Branches inside a write frame. On “creates a new, empty Shelf”, a Shelf capsule with an empty vault and a v1 tab appears, unattached. On “attaches it”, it slides onto the rib 5. On “a tool is added”, the pill add_tool(5, 40, 2) passes through Branches to Shelf 5 and the row 40: 2/2 appears in green.

Leaves with Two shelves, on keys 3 and 5.

Beat close_five — Removed with everything inside

View and focus branches. Focus on Shelf 5.

Narration

Later, branch five closes. Branches asks the shelf one question: is every tool home? It is. Branches removes the attachment. The shelf and everything inside it are gone when the call ends.

Cue and animation On “closes”, the pill close_branch(5) docks. On “is every tool home”, a small pill all_home() goes to Shelf 5 and returns true. On “removes the attachment”, the rib 5 detaches. On “gone when the call ends”, the write frame closes and Shelf 5 fades with its row.

Leaves with One shelf, on key 3. An empty rib where 5 was.

Beat reopen_five — The same key, a new module

View and focus branches.

Narration

Open branch five again. The key is the same. The module is new. It starts empty. Nothing of the old shelf comes back.

Cue and animation On “again”, open_branch(5) docks. On “The module is new”, a fresh empty Shelf slides onto rib 5 with a slightly different outline tint for one second. On “Nothing of the old shelf”, the place where 40: 2/2 was stays blank.

Leaves with Shelves on keys 3 and 5. Shelf 5 empty.

Beat close_three_refused — A closing rule

View and focus branches. Focus on Shelf 3.

Narration

Now someone tries to close branch three. Its shelf answers: no, tools are out. Branches refuses and changes nothing. Removing a module removes its memory, so the rule that guards it belongs in the design.

Cue and animation On “close branch three”, close_branch(3) docks. On “tools are out”, all_home() returns false and the rows 17: 4/1, 22: 1/0, 30: 2/1 light briefly. On “refuses”, a neutral OutcomeCard Refused(ToolsOut) leaves. On “belongs in the design”, the rib 3 pulses, still attached.

Leaves with Shelf 3 intact with its three rows.

Scene reach_by_key — How others refer to a branch

Already on screen branches view.

Beat a_number_not_a_handle — Ask the owner

View and focus tree. Focus on a Loans row, then on Branches.

Narration

One more consequence. The loan book remembers which branch a tool came from. It stores the branch number. It cannot store the shelf itself, or a handle to it. To reach a shelf, code asks Branches by number. And because every key is a whole module, the app sets a limit. Here, thirty-two branches.

Cue and animation On “stores the branch number”, the 3 in the Loans row (114, 7) 501 · 3 · 17 highlights. On “cannot store the shelf”, a ghost of a Shelf capsule tries to enter the Loans table and bounces off. On “asks Branches by number”, a pill 3 travels the call line to Branches and down rib 3. On “thirty-two branches”, a small counter 2 / 32 appears on the fan.

Leaves with The tree view with two shelves and the counter 2 / 32. Lesson 4 starts from an empty canister and rebuilds this picture.

Brief to hand over

Illustrative.

Module branches. Owns one Shelf module per branch number. At most 32 branches. open_branch refuses a number already open or a 33rd branch. close_branch asks the shelf all_home() and refuses with ToolsOut unless it is true. Closing removes the shelf and all its tools. Opening the same number later starts an empty shelf. Other modules refer to a branch by number and reach its shelf only through Branches.

Acceptance test, expected and not run

Given Command Expect Then
key 3 only open_branch(5) true keys 3, 5. Shelf 5 empty.
keys 3, 5 open_branch(5) false no change
Shelf 5 holds 40: 2/2 close_branch(5) true key 3 only
after that open_branch(5), stock(5, 40) true, then no such tool Shelf 5 empty
Shelf 3 has tools out close_branch(3) Refused(ToolsOut) no change
32 branches open open_branch(99) false still 32

Production and source notes

  • Tool Library and the snippet are illustrative. Shop declares the same thing in one line: ChildMap<u64, crate::inventory::memory::v1::Memory>. Shop branches version file, line 3.
  • Open creates and attaches in one call, and refuses a duplicate or the 33rd. Close takes the attachment. Shop branches, lines 4 and 33 to 54.
  • Closed and reopened starts empty: Shop's native test closes branch 9, opens it, and reads None for item 44. The README says "opening the same business key creates a new module identity." Shop tests, lines 33 to 35. Shop
  • The all_home() closing rule is this course's design. Shop's close has no such rule. Say so if the Shop is shown beside it.
  • An attachment cannot hide inside data, and a module identity cannot be saved as a value: mental-model review, claim 4.
  • A parent manages attachments and calls child services without reading child fields: Conventions, lines 252 to 257.
  • The service for a branch's shelf is bound on demand from the keyed attachment, inside the owner's method. Keep that off screen. Show only the pill travelling through Branches to the shelf. Shop branches, lines 8 to 15.
  • The different outline tint in reopen_five lasts one second and then matches. It marks "new module" without inventing an identity number.