Skip to main content

Crate encore

Crate encore 

Source
Expand description

The native Rust SDK for Encore.

Unlike the Go and TypeScript SDKs (which reach the runtime over a C ABI / napi), this crate links the Rust encore_sdk_core directly — Rust calling Rust, no marshalling layer.

Infrastructure resources are declared as module-scope statics using const constructors, mirroring Encore’s other SDKs:

ⓘ
use encore::{pubsub, secrets};
use encore::storage::postgres;
use encore::pubsub::Topic;
use serde::{Serialize, Deserialize};

static DB: postgres::Database = postgres::Database::new("hello").migrations("./migrations");

#[derive(Serialize, Deserialize)]
struct SignupEvent { user_id: String }

static SIGNUPS: Topic<SignupEvent> = Topic::new("signups");

static API_KEY: secrets::Secret = secrets::Secret::new("ApiKey");

A service is a directory with an encore.service.json ({ "name": ..., "lang": "rust" }) and ordinary Rust code. Trait-based services with #[encore::service] / #[endpoint] and HTTP serving are a later milestone.

Re-exports§

pub use env::app_meta;
pub use encore_sdk_core::telemetry::opentelemetry;

Modules§

config
Application config: typed values a service reads, written in TOML or JSON files beside the code, and changed while the app runs.
cron
Cron jobs: requests Encore makes of a service on a schedule.
env
Metadata about the running application — its environment, build, deployment and API gateways.
log
The console log backend. init installs Encore’s logger as the log crate’s global: each record at info and up, and the runtime’s own lines RUST_LOG (else ENCORE_RUNTIME_LOG) enables, prints on stderr in the Encore log format, with the trace and span ids of Context::current, for the log collector. ENCORE_LOG_FORMAT and ENCORE_NOLOG override the format. The runtime’s lines (the encore and encore_* crates’) log under encore.runtime, their target in encore.runtime.target.
metrics
Metrics: counters, gauges and histograms of what an app does.
objects
Object storage buckets. Declared at module scope; resolved from the runtime on use.
pubsub
Pub/Sub topics and subscriptions. Topics are typed by the message they carry and declared at module scope; a subscription names its topic and the function that handles its messages.
secrets
Application secrets. Declared at module scope and resolved from the runtime.
services
Reaching the app’s other services.
shutdown
Graceful shutdown on SIGTERM or SIGINT (Ctrl+C/Ctrl+Break on Windows).
storage
Storage resources: databases an app declares and Encore provisions.
telemetry
Tracing. Encore records its spans and metrics through its own tracer provider and meter provider, never the opentelemetry globals, which the runtime leaves to the app: pass the providers to instrumentation libraries, or install them as the globals. Infra calls parent their spans on opentelemetry::Context::current. Logs print through crate::log.
testing
Tests. Under encore test every test gets infrastructure of its own — databases, buckets and topics no other test sees — with nothing to set up:

Macros§

fixture
Declares fixtures: Fixtures, with less to write.

Structs§

Error
An error returned by an Encore SDK operation or an API endpoint handler.

Enums§

ErrCode
A gRPC-style error code, the same set the rest of Encore uses.

Functions§

initialize
Eagerly initialize the runtime, so an app fails fast at startup rather than on first resource use. Optional — any resource access triggers initialization.
run_blocking
Block the calling thread until a shutdown signal, then drain infrastructure and exit. The usual way to keep a service process alive that serves no requests of its own.
tokio_handle
A handle to the core runtime’s tokio runtime, for apps that need to spawn onto it (e.g. background infra tasks).

Attribute Macros§

test
Marks a test, as #[tokio::test] does, and tells Encore about it before it runs. See testing.