diff options
Diffstat (limited to 'morphology-img/src/lib.rs')
| -rw-r--r-- | morphology-img/src/lib.rs | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs index 8e82f91..7d0825f 100644 --- a/morphology-img/src/lib.rs +++ b/morphology-img/src/lib.rs @@ -85,6 +85,12 @@ pub struct RenderConfig { /// Optional callback for progress reporting during batch rendering pub progress_callback: Option<ProgressCallback>, + + /// Maximum number of sequences to process in a single batch + /// + /// If set, large batches will be split into smaller chunks to avoid + /// filesystem issues. If None, all sequences are processed in one batch. + pub max_batch_size: Option<usize>, } impl std::fmt::Debug for RenderConfig { @@ -94,6 +100,7 @@ impl std::fmt::Debug for RenderConfig { .field("keep_temp_files", &self.keep_temp_files) .field("lilypond_binary", &self.lilypond_binary) .field("progress_callback", &self.progress_callback.as_ref().map(|_| "<callback>")) + .field("max_batch_size", &self.max_batch_size) .finish() } } @@ -105,6 +112,7 @@ impl Default for RenderConfig { keep_temp_files: false, lilypond_binary: PathBuf::from("lilypond"), progress_callback: None, + max_batch_size: None, } } } @@ -191,6 +199,27 @@ impl RenderConfig { self.progress_callback = Some(callback); self } + + /// Sets the maximum batch size for processing sequences. + /// + /// When set, large batches of sequences will be split into smaller chunks + /// to avoid overwhelming the filesystem or LilyPond. Each chunk is processed + /// independently with its own temporary directory. + /// + /// # Examples + /// + /// ``` + /// use morphology_img::RenderConfig; + /// + /// let config = RenderConfig::new("output") + /// .max_batch_size(50); + /// + /// assert_eq!(config.max_batch_size, Some(50)); + /// ``` + pub fn max_batch_size(mut self, size: usize) -> Self { + self.max_batch_size = Some(size); + self + } } /// Renders multiple sequences to SVG files. @@ -255,17 +284,60 @@ pub fn render_sequences( sequences: &[morphology::Sequence], config: &RenderConfig, ) -> Result<Vec<PathBuf>, RenderError> { + // Handle empty input + if sequences.is_empty() { + return Ok(Vec::new()); + } + // Check if LilyPond is available before doing any work render::check_lilypond_available()?; + // Determine batch size + let batch_size = config.max_batch_size.unwrap_or(sequences.len()); + let mut all_svg_paths = Vec::with_capacity(sequences.len()); + let total_count = sequences.len(); + + // Process sequences in chunks with global indexing + let mut global_offset = 0; + for chunk in sequences.chunks(batch_size) { + let svg_paths = render_sequences_batch(chunk, config, global_offset, total_count)?; + all_svg_paths.extend(svg_paths); + global_offset += chunk.len(); + } + + Ok(all_svg_paths) +} + +/// Helper function to render a single batch of sequences. +/// +/// This function handles the complete rendering pipeline for a batch: +/// creating temp directory, writing support files, generating .ly files, +/// and rendering to SVGs. +/// +/// # Arguments +/// - `sequences`: The batch of sequences to render +/// - `config`: Configuration for rendering +/// - `global_offset`: The starting index for this batch in the overall sequence list +/// - `total_count`: The total number of sequences across all batches +fn render_sequences_batch( + sequences: &[morphology::Sequence], + config: &RenderConfig, + global_offset: usize, + total_count: usize, +) -> Result<Vec<PathBuf>, RenderError> { // Create temporary directory for .ly files let temp_dir = file_gen::create_temp_dir()?; // Write support files (.ily) to temp directory file_gen::write_support_files(temp_dir.path())?; - // Generate .ly files from sequences - let ly_files = file_gen::generate_ly_files(sequences, temp_dir.path())?; + // Generate .ly files from sequences with proper global indexing + let ly_files = file_gen::generate_ly_files_with_offset( + sequences, + temp_dir.path(), + global_offset, + total_count, + )?; // Render .ly files to SVGs let svg_paths = render::render_ly_files_to_svg( |