Skip to content

Separating business logic

Keep component interactions nearby and domain behavior reusable in Rust modules.

On this page

A single-file component does not mean a single-file application. Keep the UI state and wiring that help you understand one component in .iru. Move business rules and infrastructure into Rust modules or libraries.

LocationExample responsibilities
.iru componentCurrent input, expanded state, local error message, event wiring
src/composables/Reusable Signal/Computed construction and UI logic
src/domain/ or a Cargo libraryValidation rules, calculations and domain types
src/services/Persistence, network access and other external work

Import an ordinary Rust module

Declare a module from the application entry point:

src/main.rs
mod domain;
// Keep the generated include and mount code from the scaffold below this.
src/domain.rs
/// Apply a domain rule independently of any GUI component.
pub fn workspace_name_is_valid(name: &str) -> bool {
    !name.trim().is_empty() && name.chars().count() <= 80
}

Then import and call it from your component:

App.iru: Rust block
use crate::domain::workspace_name_is_valid;

fn validate(state: &mut State, _: &Context<State>) {
    state.message = if workspace_name_is_valid(&state.name) {
        "Name is valid".into()
    } else {
        "Use a name between 1 and 80 characters".into()
    };
}

The component owns the message and input fields. The domain function knows nothing about components, events or GUI engines. You can test and reuse it from a command-line tool or another application.

Keep handlers short when work is slow

Do not perform a network request or long disk operation inside a click handler. Handlers run on the application coordinator. Use background tasks with owned inputs and apply the result to UI state when it returns.

Splitting files should follow ownership and reuse. Keep a small local handler beside its binding when that makes the component easier to read; extract it when it obscures the layout or belongs to a broader service.

Inrux · Built with Nuxt Content, IPEX, and Akaza UI.