stdb-common
Core utilities and common functionality for SpacetimeDB-based game development. This library provides essential building blocks including validation, error handling, and UUID generation.
Features
Validation
- String validation: Length constraints with descriptive error messages
- Numeric validation: Range validation for all unsigned integer types (
u8
,u16
,u32
,u64
,u128
,usize
) - Reducer context requirements: Access control validation for private operations
UUID Generation
- UUID v4: Cryptographically random UUIDs using SpacetimeDB's random number generator
- UUID v7: Timestamp-based UUIDs for sortable identifiers
- String representation: UUIDs are represented as strings for SpacetimeDB compatibility
Error Handling
- Structured errors:
ValidationError
enum with specific error types - Error mapping:
ErrorMapper
trait for consistent error transformation - Service results:
ServiceResult<T>
type alias for standardized error handling
Usage
Validation
use ;
UUID Generation
use ;
use ReducerContext;
Reducer Context Requirements
use ReducerContextRequirements;
use ReducerContext;
Error Types
ValidationError
RequiredField
: Field is required but emptyFieldTooSmall
: Value is below minimum thresholdFieldTooLarge
: Value exceeds maximum threshold
API Reference
Functions
validate_str(name, value, min_length, max_length)
: Validates string lengthvalidate_u8/u16/u32/u64/u128/usize(name, value, min_value, max_value)
: Validates numeric ranges
Traits
UuidExt
: ExtendsReducerContext
with UUID generation methodsReducerContextRequirements
: Provides access control validationErrorMapper
: Maps errors toServiceError
Types
Uuid
: String representation of UUIDServiceResult<T>
: Result type withServiceError
ValidationError
: Validation-specific error enum
Testing
The library includes comprehensive unit tests covering:
- UUID version and variant bit validation
- String validation edge cases
- Numeric validation boundaries
- Error message formatting
Run tests with:
&& &&