memory_efficient_streaming/
memory_efficient_streaming.rs

1//! Memory-efficient streaming example for the pptx-to-md crate
2//!
3//! This example demonstrates how to use the streaming API to process large
4//! PPTX files with minimal memory usage.
5//!
6//! Run with: cargo run --example memory_efficient_streaming <path/to/your/presentation.pptx>
7
8use std::env;
9use std::path::Path;
10use std::fs;
11use pptx_to_md::{ParserConfig, PptxContainer, Result};
12
13fn main() -> Result<()> {
14    // Get the PPTX file path from command line arguments
15    let args: Vec<String> = env::args().collect();
16    let pptx_path = if args.len() > 1 {
17        &args[1]
18    } else {
19        eprintln!("Usage: cargo run --example memory_efficient_streaming <path/to/presentation.pptx>");
20        return Ok(());
21    };
22
23    println!("Processing PPTX file: {}", pptx_path);
24
25    // Use the config builder to build your config
26    let config = ParserConfig::builder()
27        .extract_images(true)
28        .build();
29    
30    // Open the PPTX file with the streaming API
31    let mut streamer = PptxContainer::open(Path::new(pptx_path), config)?;
32    
33    // Create output directory
34    let output_dir = "output_streaming";
35    fs::create_dir_all(output_dir)?;
36
37    // Process slides one by one using the iterator
38    for slide_result in streamer.iter_slides() {
39        match slide_result {
40            Ok(slide) => {
41                println!("Processing slide {} ({} elements)", slide.slide_number, slide.elements.len());
42
43                if let Some(md_content) = slide.convert_to_md() {
44                    let output_path = format!("{}/slide_{}.md", output_dir, slide.slide_number);
45                    fs::write(&output_path, md_content)?;
46                    println!("Saved slide {} to {}", slide.slide_number, output_path);
47                }
48            },
49            Err(e) => {
50                eprintln!("Error processing slide: {:?}", e);
51            }
52        }
53    }
54
55    println!("All slides processed successfully!");
56
57    Ok(())
58}