reinhardt-utils
Common utilities and helper functions
Overview
Collection of utility functions and helpers used throughout the framework.
Includes date/time utilities, string manipulation, encoding/decoding, and other common operations.
Installation
Add reinhardt to your Cargo.toml:
[]
= { = "0.1.0-alpha.1", = ["utils"] }
# Or use a preset:
# reinhardt = { version = "0.1.0-alpha.1", features = ["standard"] } # Recommended
# reinhardt = { version = "0.1.0-alpha.1", features = ["full"] } # All features
Then import utility features:
use ;
use ;
use ;
use ;
Note: Utility features are included in the standard and full feature presets.
Features
Implemented ✓
HTML Utilities (html module)
- HTML Escaping/Unescaping
escape(): Escapes HTML special characters (<,>,&,",')unescape(): Unescapes HTML entitiesconditional_escape(): Conditional escaping with autoescape flag supportescape_attr(): HTML attribute value escaping (handles newlines and tabs)
- HTML Manipulation
strip_tags(): Removes HTML tagsstrip_spaces_between_tags(): Removes whitespace between tagstruncate_html_words(): Truncates by word count while preserving HTML tagsformat_html(): HTML generation via placeholder replacement
- Safe Strings
SafeString: Safe string type for bypassing automatic escaping
Encoding Utilities (encoding module)
- URL Encoding
urlencode(): URL encoding (spaces converted to+)urldecode(): URL decoding
- JavaScript Escaping
escapejs(): JavaScript string escaping (handles quotes, control characters, special characters)
- Slugification
slugify(): URL slug generation (lowercase, special character removal, hyphen-separated)
- Text Processing
truncate_chars(): Truncate by character count (appends...)truncate_words(): Truncate by word count (appends...)wrap_text(): Wrap text at specified widthforce_str(): Safely convert byte sequences to UTF-8 stringsforce_bytes(): Convert strings to byte sequences
- Line Break Processing
linebreaks(): Convert line breaks to<br>tags (with paragraph support)linebreaksbr(): Convert line breaks to<br>tags (simple version)
Date/Time Formatting (dateformat module)
- Django/PHP-style Formatting
format(): Date/time formatting with format strings- Supported format codes:
- Year:
Y(4-digit),y(2-digit) - Month:
m(zero-padded),n(no padding),F(full name),M(abbreviated) - Day:
d(zero-padded),j(no padding),l(day name),D(day abbreviated) - Hour:
H(24-hour),h(12-hour),G/g(unpadded versions) - Minute:
i, Second:s - AM/PM:
A(uppercase),a(lowercase)
- Year:
- Shortcut Functions (
shortcutssubmodule)iso_date(): YYYY-MM-DD formatiso_datetime(): YYYY-MM-DD HH:MM:SS formatus_date(): MM/DD/YYYY formateu_date(): DD/MM/YYYY formatfull_date(): "Monday, January 1, 2025" formatshort_date(): "Jan 1, 2025" formattime_24(): 24-hour format timetime_12(): 12-hour format time (with AM/PM)
Text Manipulation (text module)
- Case Conversion
capfirst(): Capitalize first letter of each wordtitle(): Title case conversion (first letter uppercase, rest lowercase)
- Number Formatting
intcomma(): Add comma separators to integers (every 3 digits)floatcomma(): Add comma separators to floating-point numbersordinal(): Add ordinal suffixes (1st, 2nd, 3rd, 4th, etc.)
- Singular/Plural
pluralize(): Toggle singular/plural based on count
- Padding
ljust(): Left-justify (right padding)rjust(): Right-justify (left padding)center(): Center-align (both-side padding)
- Phone Number Formatting
phone_format(): Convert 10/11-digit phone numbers to(XXX) XXX-XXXXformat
Timezone Utilities (timezone module)
- Basic DateTime Retrieval
now(): Current UTC timelocaltime(): Current local time
- Timezone Conversion
to_local(): UTC to local timezone conversionto_utc(): Local to UTC conversionto_timezone(): Timezone conversion by IANA name (currently UTC only)
- Naive/Aware Conversion
make_aware_utc(): Convert naive datetime to UTC timezone-awaremake_aware_local(): Convert naive datetime to local timezone-awareis_aware(): Check for timezone information presence (alwaystruein Rust)
- Parse/Format
parse_datetime(): Parse ISO 8601 datetime stringsformat_datetime(): Output datetime in ISO 8601 format (RFC 3339)
- Timezone Name Retrieval
get_timezone_name_utc(): Get timezone name for UTC datetimeget_timezone_name_local(): Get timezone name for local datetime
cache
Features
Core Cache API - Implemented ✓
Cachetrait: Async-first trait for cache operations with generic type supportget<T>(): Retrieve values from cache with automatic deserializationset<T>(): Store values with optional TTL (Time-To-Live)delete(): Remove individual cache entrieshas_key(): Check cache key existenceclear(): Remove all entries from cacheget_many(): Batch retrieval of multiple cache keysset_many(): Batch storage of multiple valuesdelete_many(): Batch deletion of multiple keysincr(): Atomic increment for numeric valuesdecr(): Atomic decrement for numeric values
Cache Backends - Implemented ✓
-
InMemoryCache: Thread-safe in-memory cache backend
- Built on
Arc<RwLock<HashMap>>for concurrent access - Automatic expiration with TTL support
with_default_ttl(): Configure default expiration timecleanup_expired(): Manual cleanup of expired entries- JSON serialization via serde for type safety
- Built on
-
RedisCache: Redis-backed distributed cache (requires
redis-backendfeature)- Connection pooling with
ConnectionManagerfor efficient connection reuse with_default_ttl(): Default TTL configurationwith_key_prefix(): Namespace support for multi-tenant scenarios- Automatic key prefixing for organized cache entries
- Full Redis integration with all core operations implemented
- Batch operations (
get_many,set_many,delete_many) for improved performance - Atomic operations (
incr,decr) using Redis native commands
- Connection pooling with
Cache Key Management - Implemented ✓
- CacheKeyBuilder: Utility for generating versioned cache keys
new(): Create builder with custom prefixwith_version(): Version-based cache invalidationbuild(): Generate prefixed and versioned keysbuild_many(): Batch key generation- Format:
{prefix}:{version}:{key}
HTTP Middleware - Implemented ✓
-
CacheMiddleware: Automatic HTTP response caching
- Request method filtering (GET-only by default via
cache_get_only) - Response status code filtering (2xx-only by default via
cache_success_only) - Cache-Control header parsing (max-age, no-cache, no-store directives)
- Configurable cache timeout with
CacheMiddlewareConfig - Query parameter-aware cache key generation
- Full response caching (status, headers, body)
- Request method filtering (GET-only by default via
-
CacheMiddlewareConfig: Middleware configuration
with_default_timeout(): Set default cache durationwith_key_prefix(): Configure cache namespacecache_all_methods(): Enable caching for non-GET requestscache_all_responses(): Cache non-2xx responses- Custom Cache-Control header name support
Dependency Injection Support - Implemented ✓
-
CacheService: High-level service with DI integration
- Automatic injection via
reinhardt-di - Integrated
CacheKeyBuilderfor automatic key prefixing - Methods:
get(),set(),delete()with automatic key building - Access to underlying cache via
cache()method - Access to key builder via
key_builder()method
- Automatic injection via
-
RedisConfig: Redis configuration for DI (requires
redis-backendfeature)new(): Custom Redis URL configurationlocalhost(): Quick localhost setup- Automatic injection from singleton scope
- Fallback to localhost if not configured
-
Injectable trait implementations:
InMemoryCache: Uses default singleton-based injectionCacheKeyBuilder: Custom default ("app" prefix, version 1)RedisCache: Injects withRedisConfigdependencyCacheService: Composes cache and key builder via DI
Feature Flags - Implemented ✓
redis-backend: Enable Redis support (optional dependency)memcached-backend: Enable Memcached support (optional dependency)all-backends: Enable all backend implementations
logging
Features
Implemented ✓
Core Logging Infrastructure
- Logger System: Core logger implementation with handler attachment support
Loggerstruct with warning level loggingLoggerHandle: Wrapper aroundArc<Logger>for thread-safe logger access- LogHandler trait for extensible log processing
- Thread-safe handler management
- Log Levels: Support for Debug, Info, Warn, and Error severity levels
- Log Records: Structured log record representation
Logging Configuration
- Global Logging Manager: Singleton-based global logging initialization and management
init_global_logging()for one-time setupget_logger(name)returnsLoggerHandlefor retrieving named loggers- Thread-safe access via
once_cell
- Configuration Structures:
LoggingConfig: Main configuration containerHandlerConfig: Handler-specific settingsLoggerConfig: Logger-specific settings
Log Handlers
- Console Handler: Output logs to standard output/error
- File Handler: Write logs to file system
- JSON Handler: Structured JSON log output
- Memory Handler: In-memory log storage for testing and debugging
- Level-based filtering
- Cloneable for test assertions
Formatters
- Formatter Trait: Extensible log formatting interface
- Standard Formatter: Default human-readable format
- Server Formatter: Server-optimized log format
- Control Character Escaping:
escape_control_chars()utility for safe log output
Filters
- Filter Trait: Generic log filtering interface
- Callback Filter: Custom filter implementation support
- Debug-based Filters:
RequireDebugTrue: Only log when debug mode is enabledRequireDebugFalse: Only log when debug mode is disabled
Parameter Representation Utilities
- Smart Parameter Truncation: Prevent log overflow with large data structures
repr_params(): Truncate arrays, objects, and nested structurestruncate_param(): Truncate individual string valuesReprParamsConfig: Configurable truncation behavior
- Multi-batch Parameter Display: Show first/last items for large parameter sets
- Character-level Truncation: Middle truncation preserving start/end context
- SQLAlchemy-inspired Design: Based on proven parameter representation patterns
Convenience APIs
- Global Logging Functions:
emit_warning(): Quick warning emissionattach_memory_handler(): Easy test handler attachment
- Type Safety: Strongly-typed configuration and log levels
- Async Support: Built on Tokio for async runtime compatibility
Security Logging
- SecurityLogger: Dedicated logger for security-related events
- Authentication events (success/failure)
- Authorization violations
- CSRF violations
- Rate limit exceeded events
- Suspicious file operations
- Disallowed host access
- SecurityError: Enum for categorizing security events
AuthenticationFailed,AuthorizationFailed,InvalidTokenRateLimitExceeded,SuspiciousActivity,CsrfViolationInvalidInput,AccessDenied,DisallowedHost
Usage Example:
use ;
let logger = new;
// Log authentication events
logger.log_auth_event; // INFO level
logger.log_auth_event; // WARNING level
// Log security errors
logger.log_security_error; // ERROR level
// Log CSRF violation with details
logger.log_csrf_violation;
// Log rate limit exceeded
logger.log_rate_limit_exceeded;
// Log suspicious file operations
logger.log_suspicious_file_operation;
// Log disallowed host access
logger.log_disallowed_host;
Log Level Mapping:
| Event | Log Level |
|---|---|
| Authentication success | INFO |
| Authentication failure | WARNING |
| CSRF violation | ERROR |
| Rate limit exceeded | WARNING |
| Authorization failure | WARNING |
| Suspicious activity | ERROR |
static
Features
Core Functionality
✓ Implemented
-
Static File Configuration (
StaticFilesConfig)- Configurable static root directory for collected files
- Static URL path configuration with validation
- Multiple source directories support via
STATICFILES_DIRS - Media URL configuration and conflict detection
-
Storage Backends (
Storagetrait)FileSystemStorage- Local file system storageMemoryStorage- In-memory storage for testing- Extensible storage backend system
-
Static File Finder (
StaticFilesFinder)- Locate files across multiple static directories
- Support for collecting files from various sources
find_all()- Recursively discover all static files across configured directories- Efficient directory tree traversal with proper error handling
-
Hashed File Storage (
HashedFileStorage)- File hashing for cache busting
- Configurable hashing algorithms (MD5, SHA-256)
- Automatic hash calculation and filename generation
- Integration with manifest system
-
Manifest System (
ManifestStaticFilesStorage)- JSON manifest for mapping original filenames to hashed versions
- Versioned manifest format (currently V1)
- Enables efficient static file lookup in production
- Supports deployment workflows with pre-collected assets
-
Media Asset Management (
Media,HasMedia)- CSS and JavaScript dependency declaration for forms and widgets
- Media type organization (e.g., "all", "screen", "print")
- HTML rendering for
<link>and<script>tags - Dependency merging with duplicate prevention
- Trait-based system for components to declare their assets
-
Static File Handler (
StaticFileHandler)- HTTP request handling for static files
- MIME type detection via
mime_guess - Error handling with
StaticErrorandStaticResulttypes - File serving with proper content types
- Directory serving with automatic index file detection
- Configurable index files (default:
["index.html"]) viawith_index_files() - Serves index.html when accessing directories directly
- ETag Support: Content-based ETag generation for conditional requests
- Automatic ETag generation using hash of file content
- Support for
If-None-Matchheaders - 304 Not Modified responses for cached resources
- Implemented in
handler.rs(StaticFile::etag()method)
-
Configuration Validation (
checksmodule)- Django-style system checks for static files configuration
- Multiple check levels: Debug, Info, Warning, Error, Critical
- Comprehensive validation rules:
static.E001- STATIC_ROOT not setstatic.E002- STATIC_ROOT in STATICFILES_DIRSstatic.E003- STATIC_URL is emptystatic.E004- STATICFILES_DIRS entry is not a directorystatic.W001- STATIC_ROOT is subdirectory of STATICFILES_DIRSstatic.W002- STATIC_URL doesn't start with '/'static.W003- STATIC_URL doesn't end with '/'static.W004- STATICFILES_DIRS is emptystatic.W005- Directory doesn't existstatic.W006- Duplicate STATICFILES_DIRS entriesstatic.W007- MEDIA_URL doesn't start with '/'static.W008- MEDIA_URL doesn't end with '/'static.W009- MEDIA_URL prefix conflict with STATIC_URL
- Helpful hints for fixing configuration issues
-
Health Check System (
healthmodule)- Health status monitoring (Healthy, Degraded, Unhealthy)
- Async health check trait with
async_trait - Health check manager for centralized monitoring
- Detailed health reports with metadata support
- Marker traits for specialized checks:
CacheHealthCheck- Cache-related health checksDatabaseHealthCheck- Database-related health checks
- Component-level health status tracking
- Production-ready monitoring integration
-
Metrics Collection (
metricsmodule)- Performance metrics tracking
- Request timing and profiling (
RequestTimer) - Request-specific metrics (
RequestMetrics) - Centralized metrics collection (
MetricsCollector) - Generic metric types for custom measurements
-
Middleware (
StaticFilesMiddleware)- Request/response processing for static files
- Integration with HTTP pipeline
- Automatic static file serving in development
-
Dependency Resolution (
DependencyGraph)- Track dependencies between static assets
- Resolve asset loading order
- Support for complex asset dependency chains
Implemented in Related Crates
-
collectstatic Command (implemented in
reinhardt-commands)- ✓ CLI command for collecting static files from all sources
- ✓ Copy files to STATIC_ROOT with optional processing
- ✓ Integration with deployment workflows
- ✓ Progress reporting and verbose output
- See reinhardt-commands for details
-
GZip Compression (implemented in
reinhardt-middleware)- ✓ Response compression for bandwidth optimization
- ✓ Configurable compression level (0-9)
- ✓ Minimum size threshold configuration
- ✓ Content-type filtering (text/*, application/json, etc.)
- ✓ Automatic Accept-Encoding detection
- ✓ Compression only when beneficial (size check)
- See reinhardt-middleware for details
-
Brotli Compression (implemented in
reinhardt-middleware)- ✓ Advanced compression with better ratios than gzip
- ✓ Configurable quality levels (Fast, Balanced, Best)
- ✓ Window size configuration (10-24)
- ✓ Content-type filtering
- ✓ Automatic Accept-Encoding: br detection
- ✓ Intelligent compression (only when beneficial)
- See reinhardt-middleware for details
-
Cache-Control Header Management
- ✓ Configurable cache policies per file type
- ✓ Long-term caching for static assets (CSS, JS, fonts, images)
- ✓ Short-term caching for HTML files
- ✓ Flexible cache directives (public, private, no-cache, immutable, etc.)
- ✓ max-age and s-maxage configuration
- ✓ Vary header support
- ✓ Pattern-based cache policies
-
CDN Integration
- ✓ Multi-provider support (CloudFront, Fastly, Cloudflare, Custom)
- ✓ CDN URL generation with path prefixes
- ✓ Versioned URL generation
- ✓ HTTPS/HTTP configuration
- ✓ Custom header support
- ✓ Cache invalidation request helpers
- ✓ Wildcard purge support
-
Advanced Storage Backends (
storagemodule)S3Storage- S3-compatible storage backend (AWS S3, MinIO, LocalStack)- Configurable credentials (access key, secret key)
- Custom endpoint support for S3-compatible services
- Path-style addressing configuration
- Path prefix support within buckets
AzureBlobStorage- Azure Blob Storage backend- Shared key and SAS token authentication
- Custom endpoint support for Azure emulator
- Container and path prefix configuration
GcsStorage- Google Cloud Storage backend- Service account credentials (JSON or file path)
- Custom endpoint support for GCS emulator
- Project ID and bucket configuration
StorageRegistry- Custom storage backend registration system- Dynamic registration of storage backends
- Factory pattern for creating storage instances
- Backend lifecycle management (register, unregister, clear)
-
Pages/SSR Integration (
template_integrationmodule)TemplateStaticConfig- Configuration for static file generation in SSR contexts- Automatic hashed filename resolution via manifest
- Supports custom static URLs (CDN, etc.)
- Can be integrated with reinhardt-pages SSR and other rendering systems
-
File Processing Pipeline (
processingmodule)- CSS/JavaScript minification (basic whitespace and comment removal)
- Asset bundling with dependency resolution
- Processing pipeline manager
- Configurable optimization levels
- Feature flag:
processing(default: disabled)
-
Development Server Features (
dev_servermodule)- File system watching with
notifycrate - Auto-reload notification system using broadcast channels
- Development error pages with detailed debugging information
- WebSocket-based reload notifications (port 35729 by default)
- Smart reload strategies:
- CSS files: Reload without full page refresh
- Other files: Full page reload
- Multiple path watching support
- Client connection tracking
- Feature flag:
dev-server(default: disabled)
- File system watching with
-
Advanced File Processing
- Image optimization (PNG, JPEG, WebP) - Feature flag:
image-optimization - Source map generation - Feature flag:
source-maps - Asset compression (gzip, brotli) - Feature flag:
compression - Minification for CSS and JavaScript
- Asset bundling with dependency resolution
- Image optimization (PNG, JPEG, WebP) - Feature flag:
-
Advanced Minification (OXC-powered)
- Variable renaming (mangling) - Feature flag:
advanced-minification - Dead code elimination
- Production-grade compression
- Console.log removal option
- Debugger statement removal
- Variable renaming (mangling) - Feature flag:
storage
Features
Implemented ✓
Core Storage Abstraction
- Storage Trait: Async trait defining standard storage operations
- File save, read, delete operations
- File existence checking and metadata retrieval
- Directory listing functionality
- URL generation for file access
- File timestamp operations (accessed, created, modified times)
File and Metadata Handling
- FileMetadata: Comprehensive file metadata structure
- Path, size, content type tracking
- Creation and modification timestamps
- Optional checksum support (SHA-256)
- Builder pattern methods (
with_content_type,with_checksum)
- StoredFile: File representation with metadata and content
Error Handling
- StorageError: Comprehensive error types
- File not found errors
- I/O error propagation
- Invalid path detection
- Storage full conditions
- Permission denied errors
- File already exists errors
Local Filesystem Storage
- LocalStorage: Production-ready local filesystem backend
- Automatic directory creation
- Path traversal attack prevention
- SHA-256 checksum computation
- File timestamp retrieval (accessed, created, modified)
- URL generation with configurable base URL
- Comprehensive directory listing
In-Memory Storage
- InMemoryStorage: Testing and development storage backend
- Thread-safe in-memory file storage using Arc
- Timestamp tracking (accessed, created, modified)
- Directory-style path organization
- Configurable file and directory permission modes
- Django-style deconstruction for serialization
- Clone support for easy testing
Prelude Module
- Re-exports of commonly used types for convenient importing
utils-core
Features
Implemented ✓
HTML Utilities
- HTML escaping - Escape/unescape HTML special characters for XSS prevention
- Tag stripping - Remove HTML tags from text
- Space normalization - Strip spaces between HTML tags
- Attribute escaping - Escape values for safe use in HTML attributes
- Template formatting - Simple placeholder-based HTML templating
- Conditional escaping - Context-aware HTML escaping
- SafeString - Mark strings as safe to bypass autoescaping
- HTML truncation - Truncate HTML content to specified word count while preserving tags
Common Utilities
- Type conversion helpers
- String manipulation utilities
- Collection helpers
- Time and date utilities
- Encoding/decoding utilities
- Core abstraction types