CalmLens Rust SDK
The Rust SDK for CalmLens moderation, storage and CDN delivery: an async client (Tokio and reqwest), an optional blocking client, typed models, streamed uploads and downloads, retries with idempotency keys, webhook verification and an offline mock. It requires Rust 1.88 or later.
Installation
| Feature | Default | Adds |
|---|---|---|
async |
yes | The async calmlens::ServerClient |
rustls |
yes | TLS through rustls with the bundled Mozilla root certificates |
rustls-native-roots |
TLS through rustls with the operating system's root certificates | |
native-tls |
TLS through the platform library | |
blocking |
calmlens::blocking::ServerClient, the same API without async |
|
testing |
calmlens::testing::MockServerClient; enable it in [dev-dependencies] only |
With default-features = false the crate contains only the models and the webhook helpers, with no HTTP stack, for services that only receive webhooks.
Versions follow semantic versioning: see Versioning and support.
Clients
| Client | Use it for |
|---|---|
calmlens::ServerClient |
Async servers. Create one at startup and share it: clones share connections |
calmlens::blocking::ServerClient |
Synchronous code; never call it directly from async code |
calmlens::testing::MockServerClient |
Offline tests, with queued responses or an in-memory CalmLens |
Quick start
Create an API key and copy your project ID as described in Authentication, and keep them on your server.
use ;
async
assets().classify and text().classify wait for the decision; assets().create queues processing, and assets().wait_for_decision or a webhook reports the result. wait_for_decision returns once the asset is settled: its classification.status is completed, with the decision in classification.moderation.status, or failed, with the reason in classification.failure_reason. While it is queued or processing it keeps waiting. The moderation status decides delivery:
| Status | Meaning |
|---|---|
pending |
Not decided yet; don't show the content |
allowed |
Show it |
limited |
Delivered like allowed, but reduce its reach: keep it out of recommendations and rank it lower |
age-restricted |
Show it only behind your own age check; the CDN requires ViewerClaims { age_confirmed: Some(true), .. } |
blocked |
Not delivered; the content is kept |
purged |
Not delivered, and the stored content is deleted |
Treat an unknown moderation status as not allowed:
let publishable = matches!;
What you can do
- Classify text and files, and read the decision: Rust SDK, Reading classification results
- Wait for queued decisions: Waiting for a decision
- Stream uploads from bytes, readers, streams and files, with content over 8 MiB sent in resumable parts automatically: Uploads, Multipart uploads
- Turn on OCR and other optional analyses: Processing features
- Moderate replies with their conversation: Conversations
- Declare a project's configuration in code, sending each
revisionyou read to refuse a change made since:upload_presets()(list,iterate,ensureby key,delete),project_settings()(get,update, which replaces the settings) andmetadata_fields()(list,ensureby ID,delete): Upload presets - Sign upload tokens for browser uploads through a preset: Upload tokens, Upload presets
- Manage assets, folders and batches, and record reviewer decisions: Methods, Reviewer decisions, Batch operations
- Build asset URLs with lenses and transforms, sign private assets' URLs, read their display info and stream content: CDN URLs, Delivering assets
- Block known images with hash lists: Hash lists
- Page through assets and poll the events feed: Pagination, Events
- Verify webhooks with axum or actix-web: Webhooks, Webhook helpers
- Configure timeouts, retries and your own reqwest client: Client configuration, Retries and idempotency
- Handle errors by code: Errors and retries, Error details
Testing
calmlens::testing::MockServerClient (feature testing) runs your code offline against queued responses or an in-memory CalmLens that simulates moderation outcomes, events and CDN delivery. Against a real project, generate_test_text_code, paint_test_image and generate_media_test_code build signed test inputs, with your project's test input key, that CalmLens decides as a chosen scenario. See Rust testing.