Building capabilities
We covered effects and commands in detail, and hinted throughout at capabilities: the developer-friendly APIs you actually use when writing core code. Time to look at them directly, both using them and building our own.
In practice, apps need a fairly limited number of capabilities (typically around seven, almost certainly fewer than ten). The weather app uses six: Render, KeyValue, Http, Location, Secret, and Time. Capabilities are reusable across apps, so if you build one that others would benefit from, the Crux team would like to hear about it.
Using a capability
Capabilities don't return a Command directly; they return a command builder, which lets you chain behaviour before committing to a specific event. We saw the abstract shape in Managed Effects: Http::get(...).expect_json().build().then_send(Event::ReceivedResponse).
The weather app's current-weather fetch shows the same pattern in production code:
#![allow(unused)] fn main() { // Rust /// Fetch current weather for a specific location #[must_use] pub fn fetch<Effect, Event>( location: Location, api_key: ApiKey, ) -> RequestBuilder< Effect, Event, impl std::future::Future<Output = Result<CurrentWeatherResponse, WeatherError>>, > where Effect: From<Request<HttpRequest>> + Send + 'static, Event: Send + 'static, { Http::get(WEATHER_URL) .expect_json::<CurrentWeatherResponse>() .query(&CurrentWeatherQuery { lat: location.lat.to_string(), lon: location.lon.to_string(), units: "metric", appid: api_key.into(), }) .expect("could not serialize query string") .build() .map(|result| match result { Ok(mut response) => response .take_body() .map_or(Err(WeatherError::ParseError), |weather_data| { Ok(weather_data) }), Err(crux_http::HttpError::Http { code, .. }) if code == StatusCode::UNAUTHORIZED || code == StatusCode::FORBIDDEN => { Err(WeatherError::Unauthorized) } Err(_) => Err(WeatherError::NetworkError), }) } }
Http::get(...) starts a builder, .expect_json::<T>() pins down the response type, .query(...) adds URL parameters, .build() produces a RequestBuilder, and .map(...) translates the shell's Result<Response, HttpError> into the more convenient Result<CurrentWeatherResponse, WeatherError>. The caller finishes it off with .then_send(SomeEvent): fetch returns a builder, not a command, so callers can hook it into their own event type.
Note that a 4xx or 5xx response arrives on the Err side of that Result, never as an Ok(Response) carrying an error status. See Handling crux_http rejections for how to read one and how to test it.
That's how a capability gets used. But where do these APIs come from? Let's build one.
One output per operation
Before we write any code, one rule shapes everything that follows: an operation type has exactly one output type, and exactly one operation kind.
The output is what the shell answers with. The Operation trait ties it to the operation:
// Rust
pub trait Operation: Send + 'static {
type Output: Send + Unpin + 'static;
}
Because each operation is its own type, the only answer the shell can give it is a value of its Output.
The kind says how many times the shell answers:
- notify: the shell is told, and never answers.
Outputis(). - request: the shell answers exactly once, with the operation's
Output. - stream: the shell answers any number of times, each with an
Output.
The kind is part of the type, so the compiler holds every call site to it: sending an operation with the wrong Command constructor fails to compile. Type generation also passes it on to the shell, so each generated handler method has the right shape. A request returns its output, and a stream is handed a sink to send into.
You declare both with #[derive(Operation)]:
// Rust
use crux_core::macros::Operation;
/// Told to the shell, never answered.
#[derive(Operation, Facet, Serialize, Deserialize, Clone, Debug)]
#[operation(notify)]
pub struct Publish(pub Vec<u8>);
/// Answered exactly once.
#[derive(Operation, Facet, Serialize, Deserialize, Clone, Debug)]
#[operation(request, output = ValueResult)]
pub struct GetValue { pub key: String }
/// Answered a sequence of times.
#[derive(Operation, Facet, Serialize, Deserialize, Clone, Debug)]
#[operation(stream, output = Message)]
pub struct Subscribe;
The derive writes the Operation implementation, its Output, the kind, and the marker trait (crux_core::operation::Notify, Request or Stream) that goes with it, so they can't disagree.
Outputs must be types type generation can emit, which rules out
std::result::Result. Where an operation can fail, the convention is a
concrete two-variant enum, like crux_kv's ValueResult (the output of the
GetValue example above):
// Rust
#[derive(Facet, Serialize, Deserialize, Clone, Debug)]
#[repr(C)]
pub enum ValueResult {
Ok(Value),
Err(KeyValueError),
}
Capabilities usually keep a From impl to hand the developer-facing Result
alias back from their builders, so app code still works in Result.
Capabilities used to be written as one enum of operations sharing one response
enum, with the kind chosen by whichever Command constructor was called. If
your app has capabilities like that, the
Migrating to per-operation types
guide walks through the change. Apps upgrading from before crux_http 0.19
should also see Migrating crux_http to native http types.
A simple custom capability: Location
Render ships in crux_core; crux_http, crux_kv, and crux_time are separate crates Crux publishes. Location services aren't. They work differently enough across platforms that a cross-platform crate would do more harm than good, and they're specific enough that we didn't want to maintain an official one either. So the weather app defines its own.
A capability is two things:
- A protocol for talking to the shell: an operation type per operation, each with its output.
- An ergonomic API for the core developer: usually a handful of command-builder functions.
Here's the whole protocol for Location:
#![allow(unused)] fn main() { // Rust //! A custom capability for accessing the device's location. //! //! Two operations, each with its own output: [`IsLocationEnabled`] is answered //! with a `bool`, [`GetLocation`] with the coordinates the shell managed to //! obtain — or `None` if it couldn't. There is no shared response enum, so //! neither the shell nor the core can answer one question with the other's //! answer. The developer-facing command builders live in the [`command`] //! submodule. pub mod command; use crux_core::macros::Operation; use facet::Facet; use serde::{Deserialize, Serialize}; /// Geographic coordinates as returned by the shell. #[derive(Facet, Serialize, Deserialize, Clone, Copy, Debug, PartialEq)] pub struct Location { pub lat: f64, pub lon: f64, } /// Ask whether location services are currently enabled and authorised. #[derive(Operation, Facet, Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)] #[operation(request, output = bool)] pub struct IsLocationEnabled; /// Ask for the device's current coordinates. #[derive(Operation, Facet, Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq)] #[operation(request, output = Option<Location>)] pub struct GetLocation; }
Location has two operations, and each has its own output: the shell answers IsLocationEnabled with a bool and GetLocation with an Option<Location>. Both declare request, so the shell answers each exactly once and the compiler rejects Command::notify_shell(GetLocation).
Location itself needs no mention anywhere: type generation walks each output's shape and emits every type it finds, so Option<Location> is enough for the shells to get a Location.
The developer API is equally small:
#![allow(unused)] fn main() { // Rust //! Command builders for the [location capability](super). //! //! Each builder issues one operation, and the operation's single output is //! already the type the caller wants — there is nothing to narrow. They're //! generic over `Effect` and `Event` so they can be reused from any Crux //! app whose `Effect` type can wrap that request. use std::future::Future; use crux_core::{Command, Request, command::RequestBuilder}; use super::{GetLocation, IsLocationEnabled, Location}; /// Asks the shell whether location services are currently enabled. #[must_use] pub fn is_location_enabled<Effect, Event>() -> RequestBuilder<Effect, Event, impl Future<Output = bool>> where Effect: Send + From<Request<IsLocationEnabled>> + 'static, Event: Send + 'static, { Command::request_from_shell(IsLocationEnabled) } /// Asks the shell for the device's current coordinates. #[must_use] pub fn get_location<Effect, Event>() -> RequestBuilder<Effect, Event, impl Future<Output = Option<Location>>> where Effect: Send + From<Request<GetLocation>> + 'static, Event: Send + 'static, { Command::request_from_shell(GetLocation) } }
Each function issues one operation and hands back its output.
Notice the generic signatures: both functions are generic over Effect and Event. The trait bound Effect: From<Request<GetLocation>> says the caller's Effect type must be able to wrap that request. Every #[effect]-generated enum implements it for each operation it lists. Bounding per operation rather than per capability is what lets an app carry KvGet and KvSet without ever hearing about ListKeys.
A richer example: Secret
Location is about as minimal as a capability gets. Secret (storing, fetching, and deleting an API key) has three operations, and each one has different outcomes.
Fetching a secret either finds it or doesn't. Storing one either succeeds or fails, and so does deleting one. Each operation names only its own outcomes:
#![allow(unused)] fn main() { // Rust //! A custom capability for storing and retrieving secrets (e.g. API keys). //! //! Three operations — [`FetchSecret`], [`StoreSecret`] and [`DeleteSecret`] — //! each with its own output type naming only the outcomes that operation can //! actually have: [`SecretFetchResponse`], [`SecretStoreResponse`] and //! [`SecretDeleteResponse`]. There is no wide response enum shared between //! them, so no call site has to rule out variants that cannot happen. The //! developer-facing command builders live in the [`command`] submodule. //! //! The names include `Secret` because the generated shell types share one //! namespace, and type generation rejects two types with the same name. pub mod command; use crux_core::macros::Operation; use facet::Facet; use serde::{Deserialize, Serialize}; /// The key under which the weather API key is stored. pub const API_KEY_NAME: &str = "openweather_api_key"; /// Fetch the secret stored under the given key (if any). #[derive(Operation, Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[operation(request, output = SecretFetchResponse)] pub struct FetchSecret(pub String); /// Store the second value under the first key, replacing any existing value. #[derive(Operation, Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[operation(request, output = SecretStoreResponse)] pub struct StoreSecret(pub String, pub String); /// Delete the secret stored under the given key. #[derive(Operation, Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[operation(request, output = SecretDeleteResponse)] pub struct DeleteSecret(pub String); /// The output of a [`FetchSecret`]. #[derive(Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[repr(C)] pub enum SecretFetchResponse { /// No secret is stored under this key. Missing(String), /// The stored secret value. Fetched(String), } /// The output of a [`StoreSecret`]. #[derive(Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[repr(C)] pub enum SecretStoreResponse { /// The secret was stored successfully under `key`. Stored(String), /// Storage failed; the string carries the error message. StoreError(String), } /// The output of a [`DeleteSecret`]. #[derive(Facet, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[repr(C)] pub enum SecretDeleteResponse { /// The secret was removed. Deleted(String), /// Deletion failed; the string carries the error message. DeleteError(String), } }
Secret has three operations, each with a two-variant output of its own. The shell's generated handler method for FetchSecret, for example, returns a SecretFetchResponse.
The operation names carry the capability's name for a reason that has nothing to do with Rust: type generation puts every operation of every capability into one namespace on the shell side, so a bare Delete here would clash with any other capability or app operation called Delete. Type generation rejects two shared types that generate the same name. An app-defined operation with a generic name is worth prefixing from the start.
The developer API is correspondingly plain:
#![allow(unused)] fn main() { // Rust //! Command builders for the [secret capability](super). //! //! Each builder issues one operation and hands back that operation's output //! unchanged — [`SecretFetchResponse`], [`SecretStoreResponse`] or //! [`SecretDeleteResponse`]. They're generic over `Effect` and `Event` so any //! Crux app can adopt them. use std::future::Future; use crux_core::Request; use crux_core::command::RequestBuilder; use super::{ DeleteSecret, FetchSecret, SecretDeleteResponse, SecretFetchResponse, SecretStoreResponse, StoreSecret, }; /// Fetches the secret stored under `key`, if any. #[must_use] pub fn fetch<Ef, Ev>( key: impl Into<String>, ) -> RequestBuilder<Ef, Ev, impl Future<Output = SecretFetchResponse>> where Ef: From<Request<FetchSecret>> + Send + 'static, Ev: Send + 'static, { crux_core::Command::request_from_shell(FetchSecret(key.into())) } /// Stores `value` under `key`, replacing any existing secret. #[must_use] pub fn store<Ef, Ev>( key: impl Into<String>, value: impl Into<String>, ) -> RequestBuilder<Ef, Ev, impl Future<Output = SecretStoreResponse>> where Ef: From<Request<StoreSecret>> + Send + 'static, Ev: Send + 'static, { crux_core::Command::request_from_shell(StoreSecret(key.into(), value.into())) } /// Deletes the secret stored under `key`. #[must_use] pub fn delete<Ef, Ev>( key: impl Into<String>, ) -> RequestBuilder<Ef, Ev, impl Future<Output = SecretDeleteResponse>> where Ef: From<Request<DeleteSecret>> + Send + 'static, Ev: Send + 'static, { crux_core::Command::request_from_shell(DeleteSecret(key.into())) } }
Each builder issues its request and returns the output unchanged.
Using these builders looks no different from the location ones: call secret::command::fetch(API_KEY_NAME) and finish with .then_send(...) to bind the eventual SecretFetchResponse to an event.
Notifications and streams
Both capabilities above are requests. The notes example's pub/sub capability has one of each of the other two kinds, which makes it the best place to see them.
#![allow(unused)] fn main() { // Rust /// Broadcast `bytes` to every peer. Nothing is expected in return. #[derive(Operation, Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[operation(notify)] pub struct Publish(pub Vec<u8>); /// Receive every [`Message`] a peer publishes, until the subscription is /// dropped. #[derive(Operation, Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[operation(stream, output = Message)] pub struct Subscribe; /// One published payload, as it arrives from a peer. #[derive(Facet, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct Message(pub Vec<u8>); }
Publish is a notification: the shell broadcasts the bytes and there is nothing to answer, so its Output is () and #[operation(notify)] takes no output argument at all. Subscribe is a stream: the shell resolves the request once per Message that arrives from a peer, for as long as the subscription lives.
The builders differ in the same way:
#![allow(unused)] fn main() { // Rust #[must_use] pub fn subscribe() -> StreamBuilder<Effect, Event, impl Stream<Item = Vec<u8>>> where Effect: From<Request<Subscribe>> + Send + 'static, { Command::stream_from_shell(Subscribe).map(|Message(data)| data) } #[must_use] pub fn publish(data: Vec<u8>) -> NotificationBuilder<Effect, Event, impl Future<Output = ()>> where Effect: From<Request<Publish>> + Send + 'static, { Command::notify_shell(Publish(data)) } }
Command::stream_from_shell produces a StreamBuilder, whose .then_send fires an event per item rather than once; Command::notify_shell produces a NotificationBuilder, which has no output to send anywhere. Try them the other way round and the compiler stops you:
error[E0080]: evaluation panicked: this operation does not declare
OperationKind::Request; send it with notify_shell or stream_from_shell instead
The check is a const assertion evaluated after monomorphisation, so it fires
on cargo build, cargo test or cargo clippy --all-targets, but not on
cargo check or in your editor. The
operation-kind traits RFC proposes making
the kind an associated type of Operation, which would turn this into an
ordinary trait-bound error you see as you type.
On the shell side, type generation uses the kinds to shape the handler interface: publish returns nothing, subscribe is handed a sink to send Messages into, and a request method returns its output.
What capabilities provide
Putting it together, a capability gives you two things:
- A protocol: one operation type per operation, each declaring its single output and its kind, which together define the wire format between core and shell.
- A developer API: small command-builder functions that speak in convenient Rust types rather than the raw protocol.
In ports-and-adapters vocabulary, capabilities are the ports; the shell-side code that actually carries out each operation is the adapter. The core expresses what it wants done; the shell decides how to do it. Keeping that separation tight is what makes the core portable.
Speaking of the shell, it's time to look at how these operations get carried out on each platform. That's the next chapter.