Skip to content

Rust SDK

The Rust SDK is the reference implementation for all EvidentSource language SDKs.

Status: Reference Implementation

ComponentDescription
coreCore library: events, selectors, constraints, identifiers
clientClient library: gRPC connectivity to EvidentSource servers
functionsServer Functions library: WASM components for state changes and state views
  • Rust 1.84 or later
  • For WASM components: rustup target add wasm32-wasip2
  • Protocol Buffers compiler (protoc)

Add to your Cargo.toml:

[dependencies]
evidentsource-client = { git = "https://github.com/evidentsystems/evidentsource-sdks", branch = "main" }
# For building WASM components
evidentsource-functions = { git = "https://github.com/evidentsystems/evidentsource-sdks", branch = "main" }
use evidentsource_client::EvidentSourceClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = EvidentSourceClient::new("https://localhost:50051").await?;
let db = client.create_database("my-database".to_string()).await?;
println!("Created database at revision {}", db.revision());
Ok(())
}

State changes and state views are built as WASM components using proc macros:

use evidentsource_functions::prelude::*;
#[derive(Deserialize)]
struct CreateAccountCommand {
account_id: String,
name: String,
}
#[state_change]
fn create_account(
db: &impl Database,
cmd: CreateAccountCommand,
_meta: StateChangeMetadata,
) -> StateChangeResult {
// Validate
if cmd.name.is_empty() {
return Err(StateChangeError::validation("Name is required"));
}
// Create event
let event = CloudEventBuilder::new()
.event_type("com.example.account.opened")
.subject(&cmd.account_id)
.stream("accounts")
.json_data(&AccountOpened {
account_id: cmd.account_id.clone(),
name: cmd.name,
})?
.build();
// Return events with DCB constraint
Ok(DecideResult::new()
.event(event)
.condition(fail_if_events_match(
Subject::equals(&cmd.account_id)
.and(EventType::equals("com.example.account.opened"))
)))
}
Terminal window
# Add the WASM target
rustup target add wasm32-wasip2
# Build a component
cd state-changes/create-account
cargo build --release --target wasm32-wasip2
ExampleDescription
TodoMVCClassic todo list with CRUD operations
Savings AccountBanking example with deposits, withdrawals, interest calculations
rust/
├── core/ # Core library (foundation types)
├── client/ # Client library (gRPC connectivity)
├── functions/
│ ├── evidentsource-functions/ # Server Functions library
│ └── evidentsource-functions-macros/ # Proc macros for WASM components
├── interface/ # Proto files for gRPC API
└── examples/
├── todomvc/ # Todo list example
└── savings-account/ # Banking example

API documentation will be available on docs.rs when packages are published to crates.io.