Skip to Content

The floor crate

One crate for the framework, its build step, Floor's API client and the floor CLI: its features, prelude, macros and the files a backend needs.

Floor in Rust is one crate, at version 0.6. On crates.io it's usefloor (the name floor was taken there); its library is floor, so code says use floor::…. Its features pick the part you need: a product backend takes the default, app.

Getting it

In Cargo.toml (floor new writes this for you):

[dependencies]
floor = { package = "usefloor", version = "0.6" }

package = "usefloor" keeps the name floor in your code. Installing needs no token, on a laptop or in CI.

Features

FeatureWhat it gives youWho uses it
app (default)The framework: a product backend as files, run by Floor. Turns on link-ws.Product backends
buildfloor::build::generate(), for a backend's build.rsProduct backends, as a build-dependency
clientfloor::client, a typed client for Floor's API, and its types (api)Tools, scripts, services
apifloor::api: the requests and responses Floor's API takes and givesclient, bundle
cliThe floor command-line toolcargo install usefloor --features cli
linkfloor::link: the connection between Floor and a backendFloor itself; app includes it
link-wsThe link over a WebSocket, for a backend that can't reach Floor's socket (floor dev on a laptop)app includes it
bundlefloor::bundle: the .floor release filecli

A backend needs only app and, in its build-dependencies, build.

The prelude

use floor::prelude::*; at the top of a handler file brings in what handlers use:

KindItems
The handleCx
Extractors and trigger inputsJson, Query, Path, Body, Headers, Mqtt, MqttRaw, Job, DeviceEvent, Stop
OutputsJson, Created, NoContent
ErrorsError, Result, OrNotFound
RealtimeChannel
WebSocketsSocket, Message (from floor::ws)
ServicesChat, Notification, Upload
Attributesget, post, put, patch, delete, ws, mqtt, every, cron, device, job, background
DeriveType
serdeSerialize, Deserialize

The crate root

Beyond the prelude, the crate root has:

ItemWhat it is
floor::App, floor::Auth, floor::Limits, floor::PermissionsThe app the build step generates, and who may call a route. See App.
floor::runRuns a backend. See App.
floor::UserThe caller, as Floor vouches for them. See Cx.
floor::Request, floor::Response, floor::IntoResponseA request as extractors read it, and what a route returns. See Handlers.
floor::FromRequest, floor::FromTrigger, floor::Input, floor::OutputThe traits behind extractors and trigger inputs, and how a route is described to the client.
floor::FloorApiFloor as a backend reaches it. Tests use a fake one.
floor::Methodhttp::Method.
floor::servicesWhat Cx's services take and return. See Cx.
floor::typesThe shapes behind #[derive(floor::Type)].
floor::wsSocket and Message, for WebSockets.
floor::migrateA backend's migrations, db/migrations/.
floor::testingTestApp. See Testing.

The attributes and the derive are also at the root: #[floor::get(public)] works without the prelude.

Re-exports

PathWhat
floor::serde_jsonserde_json
floor::sqlxsqlx
floor::tracingtracing, for handlers' logs: floor::tracing::info!(…), which floor dev and the journal show

Keep serde, serde_json and sqlx as your own dependencies as well. Their derives and sqlx's query! macros look for them by name.

Macros

floor::app!

floor::app!();

Includes the app the build step generated: pub fn app() -> floor::App and the module tree of api/, on/ and jobs/. Put it in src/lib.rs.

floor::channel!

floor::channel!(pub Lanes = "lanes", LaneCard);

Declares a realtime channel and the message it carries. It makes a unit struct Lanes that implements floor::Channel, with NAME = "lanes" and Message = LaneCard. cx.publish(Lanes, &card) publishes on app:lanes. The message type must implement Serialize and floor::Type.

Declare channels in channels.rs at the crate root. The build step reads that file and registers each channel, so the dashboard's client gets a typed channels.lanes.

// channels.rs
floor::channel!(pub Lanes = "lanes", crate::model::LaneCard);

floor::permissions!

floor::permissions! {
    visits: { read: [admin, guard], remark: [admin, guard] },
    gates: { open: [admin, guard] },
    vehicles: { write: [admin] },
}

Roles by resource and action, once for the whole API. It defines pub fn permissions() -> floor::Permissions, with one permission per resource:action (visits:read) and the roles that hold it. Routes use it with #[get(can = "visits:read")], and handlers with cx.can("visits:read").

Write it in permissions.rs at the crate root; the build step includes it. A can that names a permission this file doesn't define stops the backend when it starts, and fails its tests.

The files a backend needs

gate/
├── Cargo.toml
├── build.rs
├── src/lib.rs
├── src/main.rs
├── api/           routes and WebSockets
├── on/            MQTT and device triggers, background tasks
├── jobs/          ticks, cron schedules, queue jobs
├── permissions.rs
├── channels.rs
└── db/migrations/

floor new <name> writes all of these. See Attributes for what goes in api/, on/ and jobs/.

Cargo.toml

The crate names floor twice: as the dependency, and as the build-dependency whose build feature runs from build.rs.

[package]
name = "gate"
version = "0.1.0"
edition = "2024"
publish = false

[dependencies]
floor = { package = "usefloor", version = "0.6" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sqlx = { version = "0.9", default-features = false, features = ["postgres", "runtime-tokio", "tls-rustls-ring-webpki", "uuid", "time", "json", "migrate", "macros"] }

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
floor = { package = "usefloor", version = "0.6", default-features = false, features = ["build"] }

build.rs

fn main() {
    floor::build::generate();
}

The build step walks api/, on/ and jobs/, finds the handlers in each file, and writes $OUT_DIR/floor_app.rs. It is a build script, not a macro, so that Cargo rebuilds when you add a handler file. A mistake, such as two files serving the same route, fails the build with a message that names the file.

src/lib.rs

//! gate: a backend on the Floor framework.

floor::app!();

pub mod model;

The crate's own modules (models, logic shared by handlers) live under src/ as usual.

src/main.rs

fn main() -> std::process::ExitCode {
    floor::run(gate::app())
}

floor::run serves HTTP, connects to Floor, and runs triggers until Floor drains the backend or it is told to stop. See App.

On this page