calmlens 0.19.0

Rust SDK for CalmLens moderation, storage and CDN delivery
Documentation

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

cargo add calmlens
cargo add tokio --features macros,rt-multi-thread
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.

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 calmlens::{models::*, ServerClient};

#[tokio::main]
async fn main() -> calmlens::Result<()> {
    let client = ServerClient::new("your-api-key", "proj_8fK2mQx7Lp4NvB3tRz9Y")?;
    let asset = client.assets().classify(SubmitAsset::text("Hello world")).await?;
    if asset.classification.moderation.status == AssetModerationStatus::Allowed {
        println!("{}", asset.id);
    }
    let queued = client
        .assets()
        .create(SubmitAsset::new("note.txt", Content::bytes(b"Hello".to_vec(), "text/plain")))
        .await?;
    client
        .assets()
        .update(UpdateAsset { asset_id: queued.id, description: Patch::Null, ..Default::default() })
        .await?;
    Ok(())
}

assets().classify and text().classify wait for the decision; assets().create queues processing, and assets().wait_for_decision or a webhook reports the result. 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!(
    asset.classification.moderation.status,
    AssetModerationStatus::Allowed | AssetModerationStatus::Limited
);

What you can do

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.

Links