Skip to content

Instantly share code, notes, and snippets.

@ugovaretto
Last active November 28, 2022 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugovaretto/fcc3f3821d162dd087198daa2be16a4d to your computer and use it in GitHub Desktop.
Save ugovaretto/fcc3f3821d162dd087198daa2be16a4d to your computer and use it in GitHub Desktop.
heic2jpg.rs
#!/usr/bin/env rust-script
use std::{
env, fs,
io::{self, Error, ErrorKind},
};
fn main() -> io::Result<()> {
let d = env::args()
.nth(1)
.ok_or(Error::new(ErrorKind::Other, "Missing argument"))?;
use std::process::Command;
let v: Option<Vec<_>> = fs::read_dir(d)?
.filter_map(Result::ok)
.filter_map(|f| {
f.path().extension().and_then(|s| {
if s.to_ascii_lowercase() == "heic" {
Some(f.path())
} else {
None
}
})
})
.map(|f| {
f.file_stem().and_then(|x|
Command::new("convert")
.arg(&f)
.arg(x.to_string_lossy().as_ref().to_owned() + ".jpg")
.spawn().ok())
})
.collect();
if v.is_none() {
return Err(Error::new(ErrorKind::Other, "Error"));
}
Ok(())
}
#!/usr/bin/env rust-script
//!
//!```cargo
//![dependencies]
//!rayon = "1.5"
//!glob = "*"
//!```
//!`
use std::{
env,
io::{self, Error, ErrorKind},
};
use rayon::prelude::*;
use glob::{glob_with, MatchOptions};
fn main() -> io::Result<()> {
let d = env::args()
.nth(1)
.ok_or(Error::new(ErrorKind::Other, "Missing argument"))?;
let options = MatchOptions {
case_sensitive: false,
require_literal_separator: false,
require_literal_leading_dot: false,
};
let files: Vec<_> = glob_with(&(d + "/*.heic"), options)
.map_err(|e| Error::new(ErrorKind::Other, format!("{:?}", e)))?
.filter_map(Result::ok).collect();
if files.is_empty() {
return Ok(());
}
use std::process::Command;
let res: Option<Vec<_>> = files.par_iter().map(|f| {
f.file_stem().and_then(|x|
Command::new("convert")
.arg(f)
.arg(x.to_string_lossy().into_owned() + ".jpg")
.spawn().ok())
})
.collect();
if res.is_none() {
return Err(Error::new(ErrorKind::Other, "Error"));
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment