Crate streamweave_tempfile

Crate streamweave_tempfile 

Source
Expand description

ยงstreamweave-tempfile

Crates.io Documentation License: CC BY-SA 4.0

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 file
  • produce() - Generate stream from temp file
  • path() - Get path to temp file

ยงTempfileConsumer

Writes to temporary files:

pub struct TempfileConsumer {
    // Internal state
}

Key Methods:

  • new() - Create consumer with new temp file
  • consume(stream) - Write stream items to temp file
  • path() - 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:

  1. TempfileProducer/TempfileConsumer creates temp file
  2. Data flows through pipeline
  3. Temp file is automatically cleaned up when dropped
  4. 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 traits
  • streamweave-error - Error handling
  • streamweave-message (optional) - Message envelope support
  • tokio - Async runtime
  • tempfile - Temporary file creation
  • futures - Stream utilities
  • async-stream - Stream generation

ยง๐ŸŽฏ Use Cases

Temporary files are used for:

  1. Intermediate Processing: Store intermediate results
  2. Large Data: Handle data too large for memory
  3. Batch Processing: Process data in batches
  4. Testing: Create test data files
  5. Data Transformation: Transform data through temp files

ยง๐Ÿ“– Documentation

ยง๐Ÿ”— See Also

ยง๐Ÿค Contributing

Contributions are welcome! Please see the Contributing Guide for details.

ยง๐Ÿ“„ License

This project is licensed under the CC BY-SA 4.0 license.

Re-exportsยง

pub use consumers::*;
pub use producers::*;

Modulesยง

consumers
producers