How airstrike/iced_receipts works
Project Type and Purpose
• This is a desktop GUI application (no separate frontend/backend) built in Rust using the Elm-style MVU pattern with the iced framework.
• Purpose: manage a list of sales (“receipts”) with multiple screens (list, show, edit), unified state, and an Action API to compose in-app instructions and iced tasks.
File Structure
• src/main.rs: Defines the App struct, view(), and top-level update() that drives the MVU loop.
• src/action.rs: Defines Action<Instruction, Message> type (optional Instruction + iced Task<Message>).
• src/list.rs: List screen module (view() + update()).
• src/sale/edit.rs & sale/show.rs: Two modes of Sale screen, each with its own view() + update().
• src/sale.rs: Shell module that routes Message::Show and Message::Edit to the appropriate submodule.
README for Insights
• Emphasis on unidirectional flow: view() → Messages → update() → Action → tasks → new Messages.
• Action composes pure instructions (app‐level commands) with runtime tasks (e.g., focusing an input).
• App::update calls perform(instruction) to turn an Instruction into a Task<Message>, then chains it with the Action’s task.
Main Components & Their Interactions
a. GUI / iced Runtime
• Hosts the event loop, scheduler, and renderer.
• Invokes App::view() to build the UI, and App::update() on incoming Messages or completed tasks.
b. App (Top-Level)
• Holds global state (list of sales, which screen is active, etc.).
• view(): delegates to active screen’s view(), tagging widgets with Message variants.
• update(msg):
• Pattern-matches on Message (e.g. Message::Sale(id, msg))
• Delegates to screen-specific update(), yielding an Action<Instruction, Message>
• If action.instruction is Some(instr), calls perform(instr) -> Task<Message>
• Chains instruction_task.chain(action.task) and returns it to iced.
c. Screens (List, Show, Edit)
• Each defines its own Message enum, view(), and update() returning Action.
• Example: in sale/show, update() on StartEdit returns Action::instruction(StartEdit).with_task(focus_next()).
d. Action API
• Type: struct Action<Instruction, Message> { instruction: Option<Instruction>, task: Task<Message> }
• Methods:
• Action::instruction(instr) sets an instruction, defaulting task to Task::none().
• .with_task(task) attaches a runtime task.
• .map_instruction(...) / .map(...) to rewrap inner messages/instructions at the App level.
e. perform(instruction)
• Converts an Instruction into a Task<Message> by performing side effects (e.g. I/O, focus).
• Returns a future that, when resolved, yields a new Message fed back to App::update().
Architectural Patterns & Design Principles
• MVU (Model-View-Update): Strict unidirectional data flow.
• Action Monad: Composes pure “instructions” and asynchronous “tasks” without exploding enum variants.
• Modularity: Screens are isolated modules, each owning its own state, Message, view(), and update().
• Chaining Tasks: Ensures instructions can spawn tasks (e.g. UI focus), with results feeding back as messages.
Detail
• Clearly separate Instruction (app‐level command) from Task<Message> (async runtime work).
• How screens scoping is handled via Message::Sale(id, msg) and .map_instruction / .map.
• Chaining step: instruction_task.chain(action.task).