From 64580d538187968e77d3937334794c7c3b0aa14b Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 27 Dec 2023 00:35:34 -0500 Subject: [PATCH] closures are hard --- src-lib/src/video.rs | 51 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src-lib/src/video.rs b/src-lib/src/video.rs index deef8e8..d9010fd 100644 --- a/src-lib/src/video.rs +++ b/src-lib/src/video.rs @@ -3,18 +3,45 @@ use core::cmp::Ordering; use std::fs; use std::path::Path; +pub fn screenshotify(input: &String) -> Result, String> { + let output_d = format!("{}.d", input); + let ext = "jpg".to_string(); + let mut result = vec![]; + let _ = ify(input, |content_span| { + let output = format!("{}/{}.{}", output_d, result.len(), ext); + let _ = screenshot(&output, input, (content_span.stop + content_span.start) / 2.0)?; + result.push(output); + Ok(()) + })?; + Ok(result) +} + pub fn clipify(input: &String) -> Result, String> { + let output_d = format!("{}.d", input); + let ext = input.split(".").last().unwrap(); + let mut result = vec![]; + let _ = ify(input, |content_span| { + let output = format!("{}/{}.{}", output_d, result.len(), ext); + let _ = clip(&output, input, content_span)?; + result.push(output); + Ok(()) + })?; + Ok(result) +} + +fn ify(input: &String, mut cb: impl FnMut(ContentSpan) -> Result<(), String>) -> Result<(), String> { match inspect(input) { Ok(inspection) => { - let output_d = format!("{}.d", input); - let ext = input.split(".").last().unwrap(); - let mut result = vec![]; - for i in 0..inspection.len() { - let output = format!("{}/{}.{}", output_d, i, ext); - let _ = clip(&output, input, inspection[i])?; - result.push(output); + match inspection.iter() + .map(|x| cb(*x)) + .filter(|x| x.is_err()) + .map(|x| x.err()) + .filter(|x| x.is_some()) + .map(|x| x.unwrap()) + .nth(0) { + Some(err) => Err(format!("callback failed: {}", err)), + None => Ok(()), } - Ok(result) }, Err(msg) => Err(msg), } @@ -224,6 +251,14 @@ mod test_inspection { use super::*; const FILE: &str = "/Users/breel/Movies/bel_1_1.mp4"; + #[test] + fn test_screenshotify() { + let result = screenshotify(&FILE.to_string()).unwrap(); + for i in result.iter() { + eprintln!("{}", i); + } + } + #[test] fn test_clipify() { let result = clipify(&FILE.to_string()).unwrap();