closures are hard

main
Bel LaPointe 2023-12-27 00:35:34 -05:00
parent 2071499feb
commit 64580d5381
1 changed files with 43 additions and 8 deletions

View File

@ -3,18 +3,45 @@ use core::cmp::Ordering;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
pub fn screenshotify(input: &String) -> Result<Vec<String>, 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<Vec<String>, String> { pub fn clipify(input: &String) -> Result<Vec<String>, 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) { match inspect(input) {
Ok(inspection) => { Ok(inspection) => {
let output_d = format!("{}.d", input); match inspection.iter()
let ext = input.split(".").last().unwrap(); .map(|x| cb(*x))
let mut result = vec![]; .filter(|x| x.is_err())
for i in 0..inspection.len() { .map(|x| x.err())
let output = format!("{}/{}.{}", output_d, i, ext); .filter(|x| x.is_some())
let _ = clip(&output, input, inspection[i])?; .map(|x| x.unwrap())
result.push(output); .nth(0) {
Some(err) => Err(format!("callback failed: {}", err)),
None => Ok(()),
} }
Ok(result)
}, },
Err(msg) => Err(msg), Err(msg) => Err(msg),
} }
@ -224,6 +251,14 @@ mod test_inspection {
use super::*; use super::*;
const FILE: &str = "/Users/breel/Movies/bel_1_1.mp4"; 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] #[test]
fn test_clipify() { fn test_clipify() {
let result = clipify(&FILE.to_string()).unwrap(); let result = clipify(&FILE.to_string()).unwrap();