cape 0.0.1

🦸 An unintrusive ORM for Rust
Documentation

Cape: Type-safe record and persistence state management

Cape provides a type-safe way to manage domain objects with persistence state tracking. Records wrap domain objects with metadata, relations, and track their persistence state at the type level to prevent common database synchronization errors.

Core Concepts

  • Record<T, R, S, K>: Wraps a domain object T with relations R, persistence state S, and key type K
  • Persistence States: Type-level tracking of whether records are new (N) or stored (S)
  • Inner Trait: Marker trait for types that can be stored in records
  • Id<K>: Optional identifier wrapper that enforces correct usage patterns
  • Timestamps: Creation and update time tracking (optional chrono integration)

Example

use cape::prelude::*;

#[derive(Clone)]
struct User {
    name: String,
    email: String,
}

// Create a new record (type: Record<User, (), N, i64>)
let new_user: Record<User, (), N, i64> = Record::new(User {
    name: "Alice".to_string(),
    email: "alice@example.com".to_string(),
});

// After saving to database, convert to stored state
// let stored_user = new_user.store(123, timestamp);
// let user_id = stored_user.id(); // Only available for stored records