streamweave-tempfile
Temporary file handling for StreamWeave
Create and process temporary files with automatic cleanup.
The streamweave-tempfile package provides temporary file handling for StreamWeave. It enables creating temporary files, processing data in temporary files, and automatic cleanup when files are no longer needed.
✨ Key Features
- TempfileProducer: Read from temporary files
- TempfileConsumer: Write to temporary files
- Automatic Cleanup: Temporary files are automatically cleaned up
- RAII Pattern: Files are cleaned up when dropped
- Configurable Cleanup: Control when files are deleted
📦 Installation
Add this to your Cargo.toml:
[]
= "0.3.0"
🚀 Quick Start
Create and Process Temp File
use ;
use PipelineBuilder;
let pipeline = new
.producer
.transformer
.consumer;
pipeline.run.await?;
// Temporary files are automatically cleaned up
Create Temp File
use TempfileProducer;
let producer = new?;
// Creates temporary file, reads from it
// File is automatically deleted when producer is dropped
Write to Temp File
use TempfileConsumer;
let consumer = new?;
// Writes to temporary file
// File is automatically deleted when consumer is dropped
📖 API Overview
TempfileProducer
Reads from temporary files:
Key Methods:
new()- Create producer with new temp fileproduce()- Generate stream from temp filepath()- Get path to temp file
TempfileConsumer
Writes to temporary files:
Key Methods:
new()- Create consumer with new temp fileconsume(stream)- Write stream items to temp filepath()- Get path to temp file
📚 Usage Examples
Process Temp Data
Process data in temporary file:
use ;
use PipelineBuilder;
let pipeline = new
.producer
.transformer
.consumer;
pipeline.run.await?;
// Both temp files are automatically cleaned up
Keep Temp File
Keep temporary file after processing:
use TempfileConsumer;
let consumer = new?
.keep_on_drop;
// Process...
pipeline.run.await?;
// File is kept, can access via consumer.path()
let path = consumer.path;
Custom Temp Directory
Create temp files in specific directory:
use TempfileProducer;
let producer = new_in_dir?;
// Creates temp file in specified directory
Temp File Lifecycle
Control temp file lifecycle:
use TempfileConsumer;
let consumer = new?
.keep_on_drop // Delete on drop (default)
.prefix // Custom filename prefix
.suffix; // Custom filename suffix
// Process...
// File is automatically deleted when consumer is dropped
🏗️ Architecture
Temporary file handling:
┌──────────────┐
│ Temp File │───> TempfileProducer ───> Stream ───> Transformer ───> Stream ───> TempfileConsumer ───> Temp File
└──────────────┘ └──────────────┘
│
▼
(Auto-cleanup)
Temp File Flow:
- TempfileProducer/TempfileConsumer creates temp file
- Data flows through pipeline
- Temp file is automatically cleaned up when dropped
- Optional: Keep file for later access
🔧 Configuration
Producer Configuration
Configure temp file producer:
let producer = new?
.prefix
.suffix
.with_config;
Consumer Configuration
Configure temp file consumer:
let consumer = new?
.keep_on_drop
.with_config;
🔍 Error Handling
Temp file errors are handled through the error system:
use ErrorStrategy;
let pipeline = new
.with_error_strategy
.producer
.consumer;
⚡ Performance Considerations
- Automatic Cleanup: Files are cleaned up automatically
- RAII Pattern: Cleanup happens on drop
- Memory Efficient: Temp files use disk, not memory
- Configurable: Control cleanup behavior
📝 Examples
For more examples, see:
🔗 Dependencies
streamweave-tempfile depends on:
streamweave- Core traitsstreamweave-error- Error handlingstreamweave-message(optional) - Message envelope supporttokio- Async runtimetempfile- Temporary file creationfutures- Stream utilitiesasync-stream- Stream generation
🎯 Use Cases
Temporary files are used for:
- Intermediate Processing: Store intermediate results
- Large Data: Handle data too large for memory
- Batch Processing: Process data in batches
- Testing: Create test data files
- Data Transformation: Transform data through temp files
📖 Documentation
🔗 See Also
- streamweave - Core traits
- streamweave-file - File I/O
- streamweave-fs - File system operations
- streamweave-pipeline - Pipeline API
🤝 Contributing
Contributions are welcome! Please see the Contributing Guide for details.
📄 License
This project is licensed under the CC BY-SA 4.0 license.