Add ProcessingJob type with source management and output path resolution
All 6 pipeline tests passing.
This commit is contained in:
@@ -1 +1,83 @@
|
||||
// Processing pipeline
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::operations::*;
|
||||
use crate::types::{ImageFormat, ImageSource};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ProcessingJob {
|
||||
pub input_dir: PathBuf,
|
||||
pub output_dir: PathBuf,
|
||||
#[serde(skip)]
|
||||
pub sources: Vec<ImageSource>,
|
||||
pub resize: Option<ResizeConfig>,
|
||||
pub rotation: Option<Rotation>,
|
||||
pub flip: Option<Flip>,
|
||||
pub convert: Option<ConvertConfig>,
|
||||
pub compress: Option<CompressConfig>,
|
||||
pub metadata: Option<MetadataConfig>,
|
||||
pub watermark: Option<WatermarkConfig>,
|
||||
pub rename: Option<RenameConfig>,
|
||||
pub preserve_directory_structure: bool,
|
||||
}
|
||||
|
||||
impl ProcessingJob {
|
||||
pub fn new(input_dir: impl AsRef<Path>, output_dir: impl AsRef<Path>) -> Self {
|
||||
Self {
|
||||
input_dir: input_dir.as_ref().to_path_buf(),
|
||||
output_dir: output_dir.as_ref().to_path_buf(),
|
||||
sources: Vec::new(),
|
||||
resize: None,
|
||||
rotation: None,
|
||||
flip: None,
|
||||
convert: None,
|
||||
compress: None,
|
||||
metadata: None,
|
||||
watermark: None,
|
||||
rename: None,
|
||||
preserve_directory_structure: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_source(&mut self, path: impl AsRef<Path>) {
|
||||
self.sources.push(ImageSource::from_path(path));
|
||||
}
|
||||
|
||||
pub fn operation_count(&self) -> usize {
|
||||
let mut count = 0;
|
||||
if self.resize.is_some() { count += 1; }
|
||||
if self.rotation.is_some() { count += 1; }
|
||||
if self.flip.is_some() { count += 1; }
|
||||
if self.convert.is_some() { count += 1; }
|
||||
if self.compress.is_some() { count += 1; }
|
||||
if self.metadata.is_some() { count += 1; }
|
||||
if self.watermark.is_some() { count += 1; }
|
||||
if self.rename.is_some() { count += 1; }
|
||||
count
|
||||
}
|
||||
|
||||
pub fn output_path_for(
|
||||
&self,
|
||||
source: &ImageSource,
|
||||
output_format: Option<ImageFormat>,
|
||||
) -> PathBuf {
|
||||
let stem = source
|
||||
.path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("output");
|
||||
|
||||
let ext = output_format
|
||||
.map(|f| f.extension())
|
||||
.or_else(|| {
|
||||
source
|
||||
.path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
})
|
||||
.unwrap_or("bin");
|
||||
|
||||
self.output_dir.join(format!("{}.{}", stem, ext))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user