Skip to content

Commit

Permalink
qr code maker
Browse files Browse the repository at this point in the history
  • Loading branch information
jRimbault committed Jan 19, 2024
1 parent 3f8b888 commit 64fb7c9
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions qr/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs::File, path::PathBuf, sync::mpsc::sync_channel};
use std::{fs::File, path::PathBuf, sync::mpsc::sync_channel, thread::Scope};

mod encoder;

Expand Down Expand Up @@ -27,40 +27,41 @@ fn main() -> Result<()> {
env_logger::builder()
.filter_level(args.verbose.log_level_filter())
.init();
run(&args)
std::thread::scope(|scope| run(scope, &args))
}

fn run(args: &Args) -> Result<()> {
std::thread::scope(|scope| {
let (sender, receiver) = sync_channel(args.files.len());
for file in &args.files {
let name = file
.file_name()
.and_then(|n| n.to_str())
.context("file name should be utf8")?;
let file = File::open(&file)?;
let sender = sender.clone();
scope.spawn(move || {
let encoder = QrFileEncoder::new(file, name);
for (i, image) in encoder.into_iter().enumerate() {
log::debug!("encoded part of {name}");
sender
.send((format!("{:02}-{name}.png", i + 1), image))
.unwrap();
}
});
}
drop(sender);
let out = &args.out;
std::fs::create_dir_all(out)?;
for (name, image) in receiver {
let path = out.join(&name);
image.save(&path).context(format!("writing {name:?}"))?;
log::info!("saved {name:?} to {out:?}");
if args.open {
open::that_detached(path)?;
fn run<'a, 'b>(scope: &'a Scope<'a, 'b>, args: &'b Args) -> Result<()>
where
'b: 'a,
{
let (sender, receiver) = sync_channel(args.files.len());
for file in &args.files {
let name = file
.file_name()
.and_then(|n| n.to_str())
.context("file name should be utf8")?;
let file = File::open(&file)?;
let sender = sender.clone();
scope.spawn(move || {
let encoder = QrFileEncoder::new(file, name);
for (i, image) in encoder.into_iter().enumerate() {
log::debug!("encoded part of {name}");
sender
.send((format!("{:02}-{name}.png", i + 1), image))
.unwrap();
}
});
}
drop(sender);
let out = &args.out;
std::fs::create_dir_all(out)?;
for (name, image) in receiver {
let path = out.join(&name);
image.save(&path).context(format!("writing {name:?}"))?;
log::info!("saved {name:?} to {out:?}");
if args.open {
open::that_detached(path)?;
}
Ok(())
})
}
Ok(())
}

0 comments on commit 64fb7c9

Please sign in to comment.