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::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> {
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();