diff options
Diffstat (limited to 'morphology-img/src')
| -rw-r--r-- | morphology-img/src/lib.rs | 58 | ||||
| -rw-r--r-- | morphology-img/src/render.rs | 15 |
2 files changed, 69 insertions, 4 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs index 3ba78f2..8e82f91 100644 --- a/morphology-img/src/lib.rs +++ b/morphology-img/src/lib.rs @@ -34,6 +34,25 @@ pub use error::RenderError; pub use note::sequence_to_markup; use std::path::PathBuf; +use std::sync::Arc; + +/// Type alias for progress callback function. +/// +/// The callback receives: +/// - `current`: The number of sequences processed so far (1-indexed) +/// - `total`: The total number of sequences to process +/// +/// # Examples +/// +/// ```no_run +/// use morphology_img::ProgressCallback; +/// use std::sync::Arc; +/// +/// let callback: ProgressCallback = Arc::new(|current, total| { +/// println!("Progress: {}/{}", current, total); +/// }); +/// ``` +pub type ProgressCallback = Arc<dyn Fn(usize, usize) + Send + Sync>; /// Configuration for rendering sequences to SVGs. /// @@ -53,7 +72,7 @@ use std::path::PathBuf; /// .keep_temp_files(true) /// .lilypond_binary("/usr/local/bin/lilypond"); /// ``` -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct RenderConfig { /// Directory where SVG files should be created pub output_dir: PathBuf, @@ -63,6 +82,20 @@ pub struct RenderConfig { /// Path to the LilyPond binary (default: "lilypond") pub lilypond_binary: PathBuf, + + /// Optional callback for progress reporting during batch rendering + pub progress_callback: Option<ProgressCallback>, +} + +impl std::fmt::Debug for RenderConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RenderConfig") + .field("output_dir", &self.output_dir) + .field("keep_temp_files", &self.keep_temp_files) + .field("lilypond_binary", &self.lilypond_binary) + .field("progress_callback", &self.progress_callback.as_ref().map(|_| "<callback>")) + .finish() + } } impl Default for RenderConfig { @@ -71,6 +104,7 @@ impl Default for RenderConfig { output_dir: PathBuf::from("output"), keep_temp_files: false, lilypond_binary: PathBuf::from("lilypond"), + progress_callback: None, } } } @@ -136,6 +170,27 @@ impl RenderConfig { self.lilypond_binary = path.into(); self } + + /// Sets a progress callback for batch rendering. + /// + /// The callback will be called after each sequence is rendered, allowing + /// you to track progress for large batches. + /// + /// # Examples + /// + /// ``` + /// use morphology_img::RenderConfig; + /// use std::sync::Arc; + /// + /// let config = RenderConfig::new("output") + /// .progress_callback(Arc::new(|current, total| { + /// println!("Rendered {}/{} sequences", current, total); + /// })); + /// ``` + pub fn progress_callback(mut self, callback: ProgressCallback) -> Self { + self.progress_callback = Some(callback); + self + } } /// Renders multiple sequences to SVG files. @@ -217,6 +272,7 @@ pub fn render_sequences( &ly_files, &config.output_dir, &config.lilypond_binary, + config.progress_callback.as_ref(), )?; // Handle temp directory cleanup diff --git a/morphology-img/src/render.rs b/morphology-img/src/render.rs index 189dd61..ca61df4 100644 --- a/morphology-img/src/render.rs +++ b/morphology-img/src/render.rs @@ -138,6 +138,7 @@ pub fn render_ly_to_svg( /// - `ly_files`: Paths to the .ly files to render /// - `output_dir`: Directory where SVGs should be created /// - `lilypond_binary`: Path to the LilyPond binary +/// - `progress_callback`: Optional callback for progress reporting /// /// # Returns /// Vector of paths to generated SVG files, in the same order as input @@ -157,7 +158,7 @@ pub fn render_ly_to_svg( /// let output_dir = Path::new("output"); /// let lilypond = Path::new("lilypond"); /// -/// let svg_paths = render_ly_files_to_svg(&ly_files, output_dir, lilypond).unwrap(); +/// let svg_paths = render_ly_files_to_svg(&ly_files, output_dir, lilypond, None).unwrap(); /// println!("Generated {} SVG files", svg_paths.len()); /// ``` /// @@ -165,16 +166,24 @@ pub fn render_ly_to_svg( /// - Files are processed sequentially (not in parallel) /// - All SVGs are created in the same output directory /// - If any file fails, an error is returned immediately +/// - Progress callback is called after each file is rendered (if provided) pub fn render_ly_files_to_svg( ly_files: &[PathBuf], output_dir: &Path, lilypond_binary: &Path, + progress_callback: Option<&crate::ProgressCallback>, ) -> Result<Vec<PathBuf>, RenderError> { - let mut svg_paths = Vec::with_capacity(ly_files.len()); + let total_count = ly_files.len(); + let mut svg_paths = Vec::with_capacity(total_count); - for ly_file in ly_files { + for (index, ly_file) in ly_files.iter().enumerate() { let svg_path = render_ly_to_svg(ly_file, output_dir, lilypond_binary)?; svg_paths.push(svg_path); + + // Call progress callback if provided + if let Some(callback) = progress_callback { + callback(index + 1, total_count); + } } Ok(svg_paths) |