StorageProvider

Trait StorageProvider 

Source
pub trait StorageProvider: Send + Sync {
    type Error: Error + Send + Sync + 'static;

    // Required methods
    fn put(
        &self,
        key: StorageKey,
        data: Value,
    ) -> impl Future<Output = Result<Value, Self::Error>> + Send;
    fn get(
        &self,
        key: StorageKey,
    ) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send;
    fn delete(
        &self,
        key: StorageKey,
    ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
    fn list(
        &self,
        prefix: StoragePrefix,
        offset: usize,
        limit: usize,
    ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;
    fn find_by_attribute(
        &self,
        prefix: StoragePrefix,
        attribute: &str,
        value: &str,
    ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send;
    fn exists(
        &self,
        key: StorageKey,
    ) -> impl Future<Output = Result<bool, Self::Error>> + Send;
    fn count(
        &self,
        prefix: StoragePrefix,
    ) -> impl Future<Output = Result<usize, Self::Error>> + Send;
    fn list_tenants(
        &self,
    ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
    fn list_resource_types(
        &self,
        tenant_id: &str,
    ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
    fn list_all_resource_types(
        &self,
    ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send;
    fn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send;
    fn stats(
        &self,
    ) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send;
}
Expand description

Core trait for storage providers that handle pure data persistence operations.

This trait defines a protocol-agnostic interface for storing and retrieving JSON data with tenant isolation. Implementations should focus solely on data persistence and retrieval without any SCIM-specific logic.

§Design Principles

  • PUT/GET/DELETE Model: Simple, fundamental operations
  • PUT Returns Data: Supports SCIM requirement to return resource state after operations
  • DELETE Returns Boolean: Indicates whether resource existed (for proper HTTP status codes)
  • Tenant Isolation: All operations are scoped to a specific tenant via StorageKey
  • Protocol Agnostic: No awareness of SCIM structures or semantics
  • Async First: All operations return futures for scalability
  • Error Transparency: Storage errors are clearly separated from protocol errors

§Key Design Decisions

  • No separate CREATE/UPDATE: Both are just PUT operations. Business logic determines whether this should be treated as create vs update.
  • PUT returns stored data: This enables SCIM providers to return the complete resource state after modifications without a separate GET call.
  • DELETE returns boolean: Allows proper HTTP status code handling (204 vs 404).

Required Associated Types§

Source

type Error: Error + Send + Sync + 'static

The error type returned by storage operations.

Required Methods§

Source

fn put( &self, key: StorageKey, data: Value, ) -> impl Future<Output = Result<Value, Self::Error>> + Send

Store data at the specified key and return the stored data.

§Arguments
  • key - The storage key identifying the resource location
  • data - The JSON data to store
§Returns

The data that was actually stored (may include storage-level metadata).

§Behavior
  • If a resource with the same key already exists, it is completely replaced
  • The storage implementation should ensure atomic operations where possible
  • No validation is performed on the data structure
  • The returned data should be exactly what would be retrieved by get()
Source

fn get( &self, key: StorageKey, ) -> impl Future<Output = Result<Option<Value>, Self::Error>> + Send

Retrieve data by key.

§Arguments
  • key - The storage key identifying the resource
§Returns

Some(data) if the resource exists, None if it doesn’t exist.

Source

fn delete( &self, key: StorageKey, ) -> impl Future<Output = Result<bool, Self::Error>> + Send

Delete data by key.

§Arguments
  • key - The storage key identifying the resource
§Returns

true if the resource was deleted, false if it didn’t exist.

§Note

This follows SCIM/HTTP semantics where DELETE operations don’t return resource data. The boolean return value allows proper HTTP status code selection (204 vs 404).

Source

fn list( &self, prefix: StoragePrefix, offset: usize, limit: usize, ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send

List resources matching a prefix with pagination.

§Arguments
  • prefix - The storage prefix (tenant + resource type)
  • offset - The number of resources to skip (0-based)
  • limit - The maximum number of resources to return
§Returns

A vector of (key, data) pairs.

§Behavior
  • Results should be consistently ordered (e.g., by resource ID)
  • If offset exceeds the total count, an empty vector should be returned
  • If limit is 0, an empty vector should be returned
Source

fn find_by_attribute( &self, prefix: StoragePrefix, attribute: &str, value: &str, ) -> impl Future<Output = Result<Vec<(StorageKey, Value)>, Self::Error>> + Send

Find resources by a specific attribute value.

§Arguments
  • prefix - The storage prefix (tenant + resource type)
  • attribute - The JSON path of the attribute to search (e.g., “userName”, “emails.0.value”)
  • value - The exact value to match
§Returns

A vector of (key, data) pairs for matching resources.

§Behavior
  • Performs exact string matching on the specified attribute
  • Supports nested attributes using dot notation
  • Returns all matching resources (no pagination)
  • Empty vector if no matches found
Source

fn exists( &self, key: StorageKey, ) -> impl Future<Output = Result<bool, Self::Error>> + Send

Check if a resource exists.

§Arguments
  • key - The storage key identifying the resource
§Returns

true if the resource exists, false if it doesn’t.

§Performance Note

This should be more efficient than get() as it doesn’t need to return data.

Source

fn count( &self, prefix: StoragePrefix, ) -> impl Future<Output = Result<usize, Self::Error>> + Send

Count the total number of resources matching a prefix.

§Arguments
  • prefix - The storage prefix (tenant + resource type)
§Returns

The total count of matching resources.

Source

fn list_tenants( &self, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send

List all tenant IDs that currently have data in storage.

Returns tenant IDs for all tenants that contain at least one resource of any type. This method enables dynamic tenant discovery without requiring hardcoded tenant patterns.

§Returns

A vector of tenant ID strings. Empty vector if no tenants have data.

§Errors

Returns storage-specific errors if the discovery operation fails.

§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};

let storage = InMemoryStorage::new();
let tenants = storage.list_tenants().await?;
println!("Found {} tenants", tenants.len());
Source

fn list_resource_types( &self, tenant_id: &str, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send

List all resource types for a specific tenant.

Returns resource type names (e.g., “User”, “Group”) that exist within the specified tenant. Only resource types with at least one stored resource are included.

§Arguments
  • tenant_id - The tenant ID to query for resource types
§Returns

A vector of resource type strings. Empty vector if tenant doesn’t exist or has no resources.

§Errors

Returns storage-specific errors if the query operation fails.

§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};

let storage = InMemoryStorage::new();
let types = storage.list_resource_types("tenant1").await?;
for resource_type in types {
    println!("Tenant has resource type: {}", resource_type);
}
Source

fn list_all_resource_types( &self, ) -> impl Future<Output = Result<Vec<String>, Self::Error>> + Send

List all resource types across all tenants.

Returns a deduplicated collection of all resource type names found across all tenants in storage. This provides a global view of resource types without tenant boundaries.

§Returns

A vector of unique resource type strings. Empty vector if no resources exist.

§Errors

Returns storage-specific errors if the discovery operation fails.

§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};

let storage = InMemoryStorage::new();
let all_types = storage.list_all_resource_types().await?;
println!("System supports {} resource types", all_types.len());
Source

fn clear(&self) -> impl Future<Output = Result<(), Self::Error>> + Send

Clear all data from storage.

Removes all resources from all tenants, effectively resetting the storage to an empty state. This operation is primarily intended for testing scenarios and should be used with caution in production environments.

§Returns

Ok(()) on successful clearing, or a storage-specific error on failure.

§Errors

Returns storage-specific errors if the clear operation fails partially or completely.

§Behavior
  • Removes all resources from all tenants atomically where possible
  • After successful clearing, list_tenants should return an empty vector
  • Primarily intended for testing scenarios
§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};

let storage = InMemoryStorage::new();
// ... populate storage with data ...
storage.clear().await?;
let tenants = storage.list_tenants().await?;
assert_eq!(tenants.len(), 0);
Source

fn stats( &self, ) -> impl Future<Output = Result<StorageStats, Self::Error>> + Send

Get storage statistics for debugging and monitoring.

Returns statistics about storage usage including tenant count, resource type count, and total number of resources across all tenants.

§Returns

A StorageStats struct containing usage metrics.

§Errors

Returns storage-specific errors if the stats collection operation fails.

§Examples
use scim_server::storage::{StorageProvider, InMemoryStorage};

let storage = InMemoryStorage::new();
let stats = storage.stats().await?;
println!("Total resources: {}", stats.total_resources);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§