rust-lang-nursery / rust-cookbook

https://rust-lang-nursery.github.io/rust-cookbook
Creative Commons Zero v1.0 Universal
2.24k stars 286 forks source link

3.1 Decompress a tarball while removing a prefix from the paths with E0107 #666

Open marc47marc47 opened 2 years ago

marc47marc47 commented 2 years ago
Compiling tar03 v0.1.0 (/Users/marc47/rust/tar03) error[E0107]: this enum takes 2 generic arguments but 1 generic argument was supplied --> src/main.rs:8:14 8 fn main() -> Result<()> { ^^^^^^ -- supplied 1 generic argument
expected 2 generic arguments
note: enum defined here, with 2 generic parameters: T, E --> /Users/marc47/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:503:10 503 pub enum Result<T, E> { ^^^^^^ - - help: add missing generic argument
8 fn main() -> Result<(), E> {
+++

For more information about this error, try rustc --explain E0107. error: could not compile tar03 due to previous error

marc47marc47 commented 2 years ago

use flate2::read::GzDecoder; use std::fs::File; use std::path::PathBuf; use tar::Archive;

type Result = std::result::Result<T, std::io::Error>; // i add this line to solve this issue

fn main() -> Result<()> { let file = File::open("archive.tar.gz")?; let mut archive = Archive::new(GzDecoder::new(file)); let prefix = "bundle/logs";

println!("Extracted the following files:");
archive
    .entries()?
    .filter_map(|e| e.ok())
    .map(|mut entry| -> Result<PathBuf> {
        let path = entry.path()?.strip_prefix(prefix).unwrap().to_owned();
        entry.unpack(&path)?;
        Ok(path)
    })
    .filter_map(|e| e.ok())
    .for_each(|x| println!("> {}", x.display()));

Ok(())

}