From 10e540c075453902a8014fe756a6eb3fe017374c Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 22 Sep 2022 14:27:30 +0300 Subject: [PATCH] join decoder+encoder --- src/main.rs | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index b239c14..0ae488d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,12 +76,6 @@ struct FileData { data: Vec, } -#[derive(Debug)] -struct FileText { - name: String, - text: String, -} - fn reader_task(tx: mpsc::Sender, input_filename: String, regex: Regex) { let zip_file = fs::File::open(input_filename).unwrap(); let mut archive = zip::ZipArchive::new(zip_file).unwrap(); @@ -108,20 +102,24 @@ fn reader_task(tx: mpsc::Sender, input_filename: String, regex: Regex) println!("read done ✅"); } -fn decoder_task(rx: mpsc::Receiver, tx: mpsc::Sender, encoding: EncodingRef) { +fn transcoder_task( + rx: mpsc::Receiver, + tx: mpsc::Sender, + encoding_from: EncodingRef, + encoding_to: EncodingRef, +) { while let Ok(FileData { name, data }) = rx.recv() { - let text = encoding.decode(&data, DecoderTrap::Ignore).unwrap(); - tx.send(FileText { name, text }).unwrap(); + let text = encoding_from.decode(&data, DecoderTrap::Ignore).unwrap(); + let new_data = encoding_to + .encode(text.as_str(), EncoderTrap::Ignore) + .unwrap(); + tx.send(FileData { + name, + data: new_data, + }) + .unwrap(); } - println!("decode done ✅"); -} - -fn encoder_task(rx: mpsc::Receiver, tx: mpsc::Sender, encoding: EncodingRef) { - while let Ok(FileText { name, text }) = rx.recv() { - let data = encoding.encode(text.as_str(), EncoderTrap::Ignore).unwrap(); - tx.send(FileData { name, data }).unwrap(); - } - println!("encode done ✅"); + println!("transcode done ✅"); } fn writer_task( @@ -149,23 +147,28 @@ fn main() -> Result<(), Box> { let args = Cli::parse(); let regex = Regex::new(&args.regex).expect("regex"); - let encoding_input = encoding_from_whatwg_label(&args.from).expect("input encoding"); - let encoding_output = encoding_from_whatwg_label(&args.to).expect("output encoding"); + let encoding_from = encoding_from_whatwg_label(&args.from).expect("input encoding"); + let encoding_to = encoding_from_whatwg_label(&args.to).expect("output encoding"); let compression: zip::CompressionMethod = args.compression.into(); let compression_level = args.compression_level; let input_filename = args.src; let output_filename = args.dst; let (reader_tx, reader_rx) = mpsc::channel::(); - let (decoder_tx, decoder_rx) = mpsc::channel::(); - let (encoder_tx, encoder_rx) = mpsc::channel::(); + let (transcoder_tx, transcoder_rx) = mpsc::channel::(); let handles = vec![ thread::spawn(move || reader_task(reader_tx, input_filename, regex)), - thread::spawn(move || decoder_task(reader_rx, decoder_tx, encoding_input)), - thread::spawn(move || encoder_task(decoder_rx, encoder_tx, encoding_output)), thread::spawn(move || { - writer_task(encoder_rx, output_filename, compression, compression_level) + transcoder_task(reader_rx, transcoder_tx, encoding_from, encoding_to) + }), + thread::spawn(move || { + writer_task( + transcoder_rx, + output_filename, + compression, + compression_level, + ) }), ];