calmlens 0.41.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, verifying certificates with the operating system's store
native-tls TLS through the platform library
blocking calmlens::blocking::ServerClient, the same API without async
tracing A tracing event, with the target calmlens, for every HTTP attempt
testing calmlens::testing: the offline mock and test input generators, for [dev-dependencies] or a smoke-test binary
strict-contract For the SDK's own contract tests only; don't enable it in applications

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. The minimum supported Rust version is 1.88; a minor release may raise it to a version released at least six months earlier, and a patch release never does.

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<()> {
    // Or ServerClient::from_env(), which reads CALMLENS_API_KEY and CALMLENS_PROJECT_ID.
    let client = ServerClient::new("your-api-key", "proj_8fK2mQx7Lp4NvB3tRz9Y")?;
    let asset = client.assets().classify(SubmitAsset::text("Hello world")).await?;
    if asset.classification.moderation.decision == Some(AssetModerationDecision::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::new(queued.id).name("Note").clear_external_id())
        .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. wait_for_decision returns once the asset is settled: its classification.status is completed, with the decision in classification.moderation.decision, or failed, with the reason in classification.failure_reason. While it is queued or processing it keeps waiting. The decision decides delivery:

Decision Meaning
None 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::new().age_confirmed(true)
blocked Not delivered; the content is kept
purged Not delivered, and the stored content is deleted

Jurisdiction rules can make the outcome stricter for viewers in some countries: serve the content through CalmLens URLs with the viewer's region (Viewer region), or apply classification.moderation.regions when you serve your own copies (Self-hosted content).

Treat a missing or unknown decision as not allowed:

let publishable = matches!(
    asset.classification.moderation.decision,
    Some(AssetModerationDecision::Allowed | AssetModerationDecision::Limited)
);

Options types are #[non_exhaustive]: build them with new, which takes the required fields, and the chained setters (clear_* sends null), so a minor release can add an option. Enums keep values this SDK version doesn't list as Unknown; to act on one, compare as_str() in a _ arm rather than matching Unknown, which stops matching once a later version lists the value.

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