diff options
| author | jacksonfu14 <jackson@fashionunited.com> | 2025-11-17 06:56:09 +0100 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2025-11-17 06:56:09 +0100 |
| commit | a20f9a327c4d9e5ac8f42db4e2e9fb29770c3d37 (patch) | |
| tree | 568c2650e8bf015b896f86297176a485b9bb5c08 /morphology-img/src/lib.rs | |
| parent | 3077c7af770b055804d5cf48994a4e4cf6d31146 (diff) | |
feature: generate and process ly files
Diffstat (limited to 'morphology-img/src/lib.rs')
| -rw-r--r-- | morphology-img/src/lib.rs | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/morphology-img/src/lib.rs b/morphology-img/src/lib.rs index b94bb5c..3ba78f2 100644 --- a/morphology-img/src/lib.rs +++ b/morphology-img/src/lib.rs @@ -32,3 +32,200 @@ pub mod render; // Public API - only export what external clients should use pub use error::RenderError; pub use note::sequence_to_markup; + +use std::path::PathBuf; + +/// Configuration for rendering sequences to SVGs. +/// +/// This struct uses the builder pattern to configure how sequences are rendered +/// to SVG files using LilyPond. +/// +/// # Examples +/// +/// ``` +/// use morphology_img::RenderConfig; +/// +/// // Basic configuration with default settings +/// let config = RenderConfig::new("output"); +/// +/// // Advanced configuration with custom settings +/// let config = RenderConfig::new("output") +/// .keep_temp_files(true) +/// .lilypond_binary("/usr/local/bin/lilypond"); +/// ``` +#[derive(Debug, Clone)] +pub struct RenderConfig { + /// Directory where SVG files should be created + pub output_dir: PathBuf, + + /// Whether to keep temporary .ly files for debugging + pub keep_temp_files: bool, + + /// Path to the LilyPond binary (default: "lilypond") + pub lilypond_binary: PathBuf, +} + +impl Default for RenderConfig { + fn default() -> Self { + Self { + output_dir: PathBuf::from("output"), + keep_temp_files: false, + lilypond_binary: PathBuf::from("lilypond"), + } + } +} + +impl RenderConfig { + /// Creates a new RenderConfig with the specified output directory. + /// + /// All other settings use their default values: + /// - `keep_temp_files`: false + /// - `lilypond_binary`: "lilypond" + /// + /// # Examples + /// + /// ``` + /// use morphology_img::RenderConfig; + /// + /// let config = RenderConfig::new("my-output"); + /// assert_eq!(config.output_dir.to_str().unwrap(), "my-output"); + /// ``` + pub fn new(output_dir: impl Into<PathBuf>) -> Self { + Self { + output_dir: output_dir.into(), + ..Default::default() + } + } + + /// Sets whether to keep temporary .ly files for debugging. + /// + /// By default, temporary files are automatically cleaned up. Setting this + /// to `true` will preserve them for inspection. + /// + /// # Examples + /// + /// ``` + /// use morphology_img::RenderConfig; + /// + /// let config = RenderConfig::new("output") + /// .keep_temp_files(true); + /// + /// assert!(config.keep_temp_files); + /// ``` + pub fn keep_temp_files(mut self, keep: bool) -> Self { + self.keep_temp_files = keep; + self + } + + /// Sets the path to the LilyPond binary. + /// + /// By default, this is "lilypond", which will be found in the system PATH. + /// Use this method to specify a custom path to the LilyPond executable. + /// + /// # Examples + /// + /// ``` + /// use morphology_img::RenderConfig; + /// + /// let config = RenderConfig::new("output") + /// .lilypond_binary("/usr/local/bin/lilypond"); + /// + /// assert_eq!(config.lilypond_binary.to_str().unwrap(), "/usr/local/bin/lilypond"); + /// ``` + pub fn lilypond_binary(mut self, path: impl Into<PathBuf>) -> Self { + self.lilypond_binary = path.into(); + self + } +} + +/// Renders multiple sequences to SVG files. +/// +/// This is the main public API for converting meta-affine sequences into +/// SVG visualizations using LilyPond. The function handles the complete pipeline: +/// 1. Creates temporary directory for .ly files +/// 2. Writes LilyPond support files (.ily) +/// 3. Generates .ly files from sequences +/// 4. Invokes LilyPond to render SVGs +/// 5. Cleans up temporary files (unless configured to keep them) +/// +/// # Arguments +/// - `sequences`: The meta-affine sequences to render +/// - `config`: Configuration for output directory, LilyPond binary, etc. +/// +/// # Returns +/// Vector of paths to generated SVG files, in the same order as input sequences +/// +/// # Errors +/// - `RenderError::FileIO` if directories cannot be created or files cannot be written +/// - `RenderError::LilyPondNotFound` if LilyPond is not available +/// - `RenderError::LilyPondExecution` if LilyPond fails to render any sequence +/// - `RenderError::Template` if template processing fails +/// +/// # Examples +/// +/// ```no_run +/// use morphology::{Sequence, Value}; +/// use morphology_img::{render_sequences, RenderConfig}; +/// +/// // Create some sequences +/// let sequences = vec![ +/// Sequence::new(vec![Value::origin()]), +/// Sequence::new(vec![Value::new(1, 0)]), +/// ]; +/// +/// // Configure rendering +/// let config = RenderConfig::new("output"); +/// +/// // Render to SVGs +/// let svg_paths = render_sequences(&sequences, &config).unwrap(); +/// println!("Generated {} SVG files", svg_paths.len()); +/// ``` +/// +/// # Advanced Configuration +/// +/// ```no_run +/// use morphology::{Sequence, Value}; +/// use morphology_img::{render_sequences, RenderConfig}; +/// +/// let sequences = vec![Sequence::new(vec![Value::origin()])]; +/// +/// // Keep temporary .ly files for debugging +/// let config = RenderConfig::new("output") +/// .keep_temp_files(true) +/// .lilypond_binary("/usr/local/bin/lilypond"); +/// +/// let svg_paths = render_sequences(&sequences, &config).unwrap(); +/// ``` +pub fn render_sequences( + sequences: &[morphology::Sequence], + config: &RenderConfig, +) -> Result<Vec<PathBuf>, RenderError> { + // Check if LilyPond is available before doing any work + render::check_lilypond_available()?; + + // 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())?; + + // Render .ly files to SVGs + let svg_paths = render::render_ly_files_to_svg( + &ly_files, + &config.output_dir, + &config.lilypond_binary, + )?; + + // Handle temp directory cleanup + if config.keep_temp_files { + // Persist the temp directory by keeping it + let temp_path = temp_dir.keep(); + eprintln!("Temporary files kept at: {}", temp_path.display()); + } + // Otherwise, temp_dir will be automatically dropped and cleaned up + + Ok(svg_paths) +} |