File: lzw-compress.rs

package info (click to toggle)
rust-lzw 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 128 kB
  • sloc: makefile: 2
file content (31 lines) | stat: -rw-r--r-- 763 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! Compresses the input from stdin and writes the result to stdout.

extern crate lzw;

use std::io::{self, Write, BufRead};

fn main() {
    match (|| -> io::Result<()> {
        let mut encoder = try!(
            lzw::Encoder::new(lzw::LsbWriter::new(io::stdout()), 8)
        );
        let stdin = io::stdin();
        let mut stdin = stdin.lock();
        loop {
            let len = {
                let buf = try!(stdin.fill_buf());
                try!(encoder.encode_bytes(buf));
                buf.len()
            };
            if len == 0 {
                break
            }
            stdin.consume(len);
        }
        Ok(())
    })() {
        Ok(()) => (),
        Err(err) => { let _ = write!(io::stderr(), "{}", err); }
    }
    
}