Rust SDK
The Rust SDK is the reference implementation for all EvidentSource language SDKs.
Status: Reference Implementation
SDK Components
Section titled “SDK Components”| Component | Description |
|---|---|
| core | Core library: events, selectors, constraints, identifiers |
| client | Client library: gRPC connectivity to EvidentSource servers |
| functions | Server Functions library: WASM components for state changes and state views |
Requirements
Section titled “Requirements”- Rust 1.84 or later
- For WASM components:
rustup target add wasm32-wasip2 - Protocol Buffers compiler (
protoc)
Installation
Section titled “Installation”Add to your Cargo.toml:
[dependencies]evidentsource-client = { git = "https://github.com/evidentsystems/evidentsource-sdks", branch = "main" }
# For building WASM componentsevidentsource-functions = { git = "https://github.com/evidentsystems/evidentsource-sdks", branch = "main" }Quick Start
Section titled “Quick Start”Client Usage
Section titled “Client Usage”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(())}Server Functions Usage
Section titled “Server Functions Usage”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")) )))}Building WASM Components
Section titled “Building WASM Components”# Add the WASM targetrustup target add wasm32-wasip2
# Build a componentcd state-changes/create-accountcargo build --release --target wasm32-wasip2Examples
Section titled “Examples”| Example | Description |
|---|---|
| TodoMVC | Classic todo list with CRUD operations |
| Savings Account | Banking example with deposits, withdrawals, interest calculations |
Project Structure
Section titled “Project Structure”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 exampleAPI Reference
Section titled “API Reference”API documentation will be available on docs.rs when packages are published to crates.io.
Next Steps
Section titled “Next Steps”- Getting Started - Build your first EvidentSource application
- State Changes - Learn about command handling
- State Views - Learn about view materialization