join decoder+encoder
This commit is contained in:
parent
f7f2d91bac
commit
10e540c075
53
src/main.rs
53
src/main.rs
@ -76,12 +76,6 @@ struct FileData {
|
|||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct FileText {
|
|
||||||
name: String,
|
|
||||||
text: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn reader_task(tx: mpsc::Sender<FileData>, input_filename: String, regex: Regex) {
|
fn reader_task(tx: mpsc::Sender<FileData>, input_filename: String, regex: Regex) {
|
||||||
let zip_file = fs::File::open(input_filename).unwrap();
|
let zip_file = fs::File::open(input_filename).unwrap();
|
||||||
let mut archive = zip::ZipArchive::new(zip_file).unwrap();
|
let mut archive = zip::ZipArchive::new(zip_file).unwrap();
|
||||||
@ -108,20 +102,24 @@ fn reader_task(tx: mpsc::Sender<FileData>, input_filename: String, regex: Regex)
|
|||||||
println!("read done ✅");
|
println!("read done ✅");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decoder_task(rx: mpsc::Receiver<FileData>, tx: mpsc::Sender<FileText>, encoding: EncodingRef) {
|
fn transcoder_task(
|
||||||
|
rx: mpsc::Receiver<FileData>,
|
||||||
|
tx: mpsc::Sender<FileData>,
|
||||||
|
encoding_from: EncodingRef,
|
||||||
|
encoding_to: EncodingRef,
|
||||||
|
) {
|
||||||
while let Ok(FileData { name, data }) = rx.recv() {
|
while let Ok(FileData { name, data }) = rx.recv() {
|
||||||
let text = encoding.decode(&data, DecoderTrap::Ignore).unwrap();
|
let text = encoding_from.decode(&data, DecoderTrap::Ignore).unwrap();
|
||||||
tx.send(FileText { name, text }).unwrap();
|
let new_data = encoding_to
|
||||||
|
.encode(text.as_str(), EncoderTrap::Ignore)
|
||||||
|
.unwrap();
|
||||||
|
tx.send(FileData {
|
||||||
|
name,
|
||||||
|
data: new_data,
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
println!("decode done ✅");
|
println!("transcode done ✅");
|
||||||
}
|
|
||||||
|
|
||||||
fn encoder_task(rx: mpsc::Receiver<FileText>, tx: mpsc::Sender<FileData>, 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 ✅");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writer_task(
|
fn writer_task(
|
||||||
@ -149,23 +147,28 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
let regex = Regex::new(&args.regex).expect("regex");
|
let regex = Regex::new(&args.regex).expect("regex");
|
||||||
let encoding_input = encoding_from_whatwg_label(&args.from).expect("input encoding");
|
let encoding_from = encoding_from_whatwg_label(&args.from).expect("input encoding");
|
||||||
let encoding_output = encoding_from_whatwg_label(&args.to).expect("output encoding");
|
let encoding_to = encoding_from_whatwg_label(&args.to).expect("output encoding");
|
||||||
let compression: zip::CompressionMethod = args.compression.into();
|
let compression: zip::CompressionMethod = args.compression.into();
|
||||||
let compression_level = args.compression_level;
|
let compression_level = args.compression_level;
|
||||||
let input_filename = args.src;
|
let input_filename = args.src;
|
||||||
let output_filename = args.dst;
|
let output_filename = args.dst;
|
||||||
|
|
||||||
let (reader_tx, reader_rx) = mpsc::channel::<FileData>();
|
let (reader_tx, reader_rx) = mpsc::channel::<FileData>();
|
||||||
let (decoder_tx, decoder_rx) = mpsc::channel::<FileText>();
|
let (transcoder_tx, transcoder_rx) = mpsc::channel::<FileData>();
|
||||||
let (encoder_tx, encoder_rx) = mpsc::channel::<FileData>();
|
|
||||||
|
|
||||||
let handles = vec![
|
let handles = vec![
|
||||||
thread::spawn(move || reader_task(reader_tx, input_filename, regex)),
|
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 || {
|
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,
|
||||||
|
)
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user