Expand description
ยง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:
[dependencies]
streamweave-tempfile = "0.3.0"ยง๐ Quick Start
ยงCreate and Process Temp File
use streamweave_tempfile::{TempfileProducer, TempfileConsumer};
use streamweave_pipeline::PipelineBuilder;
let pipeline = PipelineBuilder::new()
.producer(TempfileProducer::new()?)
.transformer(/* your transformer */)
.consumer(TempfileConsumer::new()?);
pipeline.run().await?;
// Temporary files are automatically cleaned upยงCreate Temp File
use streamweave_tempfile::TempfileProducer;
let producer = TempfileProducer::new()?;
// Creates temporary file, reads from it
// File is automatically deleted when producer is droppedยงWrite to Temp File
use streamweave_tempfile::TempfileConsumer;
let consumer = TempfileConsumer::new()?;
// Writes to temporary file
// File is automatically deleted when consumer is droppedยง๐ API Overview
ยงTempfileProducer
Reads from temporary files:
pub struct TempfileProducer {
// Internal state
}Key Methods:
new()- Create producer with new temp fileproduce()- Generate stream from temp filepath()- Get path to temp file
ยงTempfileConsumer
Writes to temporary files:
pub struct TempfileConsumer {
// Internal state
}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 streamweave_tempfile::{TempfileProducer, TempfileConsumer};
use streamweave_pipeline::PipelineBuilder;
let pipeline = PipelineBuilder::new()
.producer(TempfileProducer::new()?)
.transformer(/* transform data */)
.consumer(TempfileConsumer::new()?);
pipeline.run().await?;
// Both temp files are automatically cleaned upยงKeep Temp File
Keep temporary file after processing:
use streamweave_tempfile::TempfileConsumer;
let consumer = TempfileConsumer::new()?
.keep_on_drop(true);
// 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 streamweave_tempfile::TempfileProducer;
let producer = TempfileProducer::new_in_dir("/tmp/my-temp")?;
// Creates temp file in specified directoryยงTemp File Lifecycle
Control temp file lifecycle:
use streamweave_tempfile::TempfileConsumer;
let consumer = TempfileConsumer::new()?
.keep_on_drop(false) // Delete on drop (default)
.prefix("my-prefix") // Custom filename prefix
.suffix(".tmp"); // 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 = TempfileProducer::new()?
.prefix("data")
.suffix(".tmp")
.with_config(ProducerConfig::default()
.with_name("temp-reader".to_string()));ยงConsumer Configuration
Configure temp file consumer:
let consumer = TempfileConsumer::new()?
.keep_on_drop(false)
.with_config(ConsumerConfig {
error_strategy: ErrorStrategy::Skip,
name: "temp-writer".to_string(),
});ยง๐ Error Handling
Temp file errors are handled through the error system:
use streamweave_error::ErrorStrategy;
let pipeline = PipelineBuilder::new()
.with_error_strategy(ErrorStrategy::Skip)
.producer(TempfileProducer::new()?)
.consumer(TempfileConsumer::new()?);ยงโก 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.