# lib.rs — Application Bootstrap

`<span class="editor-theme-code">lib.rs</span>`<span style="white-space: pre-wrap;"> is where the entire Tauri application is configured and started. It does the following in order:</span>

### Managed state registration

<span style="white-space: pre-wrap;">Three pieces of state are registered with Tauri's state manager so they can be injected into any command via </span>`<span class="editor-theme-code">State<'_></span>`:

- `<span class="editor-theme-code">RoverState</span>`<span style="white-space: pre-wrap;"> — the three rover mode booleans (</span>`<span class="editor-theme-code">drive_manual_mode</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">arm_manual_mode</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">pickup_mode</span>`<span style="white-space: pre-wrap;">), all wrapped in </span>`<span class="editor-theme-code">Mutex</span>`<span style="white-space: pre-wrap;"> so they are safe to read and write from async commands</span>
- `<span class="editor-theme-code">DummyStreamHandle</span>`<span style="white-space: pre-wrap;"> — holds an optional cancellation flag for the dummy simulator</span>
- `<span class="editor-theme-code">RoverAddress</span>`<span style="white-space: pre-wrap;"> — holds the port the Rover is sending to</span>

<p class="callout warning">`<span class="editor-theme-code">RoverState</span>`<span style="white-space: pre-wrap;"> is subject to change, because the rover might become able to drive and move the arm at the same time</span></p>

### Plugin registration

Three official Tauri plugins are loaded:

<table id="bkmrk-pluginpurposetauri-p"><colgroup><col></col><col></col></colgroup><tbody><tr><th>**Plugin**

</th><th>**Purpose**

</th></tr><tr><td>`<span class="editor-theme-code">tauri-plugin-fs</span>`

</td><td>File system access from the frontend

</td></tr><tr><td>`<span class="editor-theme-code">tauri-plugin-opener</span>`

</td><td>Open files/URLs in the OS default application

</td></tr><tr><td>`<span class="editor-theme-code">tauri-plugin-dialog</span>`

</td><td>Native file picker and dialog boxes

</td></tr></tbody></table>

<span style="white-space: pre-wrap;">All plugins must be registered in </span>`<span class="editor-theme-code">lib.rs</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">Cargo.toml</span>`<span style="white-space: pre-wrap;"> and if they require access to anything in </span>`<span class="editor-theme-code">src-tauri/capabilities/default.json</span>`

<p class="callout warning"><span style="white-space: pre-wrap;">AI will often try to get you to add them to </span>`<span class="editor-theme-code">tauri.conf.json</span>`<span style="white-space: pre-wrap;"> but that is, as far as I have encountered if, incorrect</span></p>

### Command registration

<span style="white-space: pre-wrap;">All Tauri commands are registered here via </span>`<span class="editor-theme-code">tauri::generate_handler!</span>`<span style="white-space: pre-wrap;">. This is the complete list of commands callable from the frontend via </span>`<span class="editor-theme-code">invoke()</span>`<span style="white-space: pre-wrap;">. If you add a new command in any </span>`<span class="editor-theme-code">commands/</span>`<span style="white-space: pre-wrap;"> file, it must also be added here or it will not be accessible from the frontend.</span>

### Setup (startup sequence)

<span style="white-space: pre-wrap;">The </span>`<span class="editor-theme-code">.setup()</span>`<span style="white-space: pre-wrap;"> closure runs once at launch, before any window is shown. It performs these steps in order:</span>

**a) GStreamer plugin path**<span style="white-space: pre-wrap;"> Sets the </span>`<span class="editor-theme-code">GST_PLUGIN_PATH</span>`<span style="white-space: pre-wrap;"> environment variable so GStreamer can find its plugins on Windows. It will look for them at </span>`<span class="editor-theme-code">C:\gstreamer\1.0\msvc_x86_64\bin</span>`. On Linux it can find the plugins automatically.

**b) Storage directory creation**<span style="white-space: pre-wrap;"> Calls </span>`<span class="editor-theme-code">ensure_storage_dirs_internal()</span>`<span style="white-space: pre-wrap;"> to create the </span>`<span class="editor-theme-code">tasks/</span>`<span style="white-space: pre-wrap;">, </span>`<span class="editor-theme-code">images/</span>`<span style="white-space: pre-wrap;">, and </span>`<span class="editor-theme-code">maps/</span>`<span style="white-space: pre-wrap;"> subdirectories under the app data directory if they don't already exist.</span>

**c) Cache clearing**<span style="white-space: pre-wrap;"> Calls </span>`<span class="editor-theme-code">clear_cache_on_startup()</span>`<span style="white-space: pre-wrap;"> to wipe any stale cached files from the previous session.</span>

**d) GStreamer streaming server**<span style="white-space: pre-wrap;"> Spawns an async task that runs </span>`<span class="editor-theme-code">commands::gstreamer::stream()</span>`<span style="white-space: pre-wrap;"> for the lifetime of the app. This starts the three GStreamer pipelines and their corresponding MJPEG HTTP servers.</span>

**e) UDP service**<span style="white-space: pre-wrap;"> Creates </span>`<span class="editor-theme-code">UdpService</span>`<span style="white-space: pre-wrap;"> (binding </span>`<span class="editor-theme-code">0.0.0.0:9000</span>`<span style="white-space: pre-wrap;">) synchronously using </span>`<span class="editor-theme-code">block_on</span>`. The socket is extracted before the service is moved into Tauri's state manager, so it can be passed to the listener independently.

**f) UDP listener**<span style="white-space: pre-wrap;"> Spawns an async task running </span>`<span class="editor-theme-code">network::listener::run_listener()</span>`<span style="white-space: pre-wrap;"> with the shared socket. This is the loop that receives, decodes, and forwards all incoming rover packets to the frontend.</span>

**g) Controller listener**<span style="white-space: pre-wrap;"> Calls </span>`<span class="editor-theme-code">commands::controller::start_controller_listener()</span>`, which spawns an OS thread to poll for gamepad events.