Johann Woelper před 4 roky
rodič
revize
f0e132d4d1
3 změnil soubory, kde provedl 88 přidání a 7 odebrání
  1. 65 0
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 21 6
      src/main.rs

+ 65 - 0
Cargo.lock

@@ -100,6 +100,17 @@ dependencies = [
  "miniz_oxide",
 ]
 
+[[package]]
+name = "getrandom"
+version = "0.1.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
 [[package]]
 name = "hermit-abi"
 version = "0.1.12"
@@ -129,6 +140,7 @@ name = "partun"
 version = "0.1.0"
 dependencies = [
  "clap",
+ "rand",
  "zip",
 ]
 
@@ -138,6 +150,53 @@ version = "0.1.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "780fb4b6698bbf9cf2444ea5d22411cef2953f0824b98f33cf454ec5615645bd"
 
+[[package]]
+name = "ppv-lite86"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
+
+[[package]]
+name = "rand"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
+dependencies = [
+ "getrandom",
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
+dependencies = [
+ "rand_core",
+]
+
 [[package]]
 name = "strsim"
 version = "0.8.0"
@@ -175,6 +234,12 @@ version = "0.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
 
+[[package]]
+name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
+
 [[package]]
 name = "winapi"
 version = "0.3.8"

+ 2 - 1
Cargo.toml

@@ -8,4 +8,5 @@ edition = "2018"
 
 [dependencies]
 zip = "*"
-clap = "*"
+clap = "*"
+rand = "*"

+ 21 - 6
src/main.rs

@@ -1,7 +1,8 @@
 use std::io;
 use std::fs;
-use clap::{Arg, App};
 use std::path::PathBuf;
+use clap::{Arg, App};
+use rand::seq::SliceRandom;
 
 fn main() {
 
@@ -33,8 +34,8 @@ fn main() {
 
     let archive = matches.value_of("ZIP").unwrap();
     let filter = matches.value_of("filter");
-    let ignorepath = matches.is_present("ignorepath");
-    let random = matches.value_of("random");
+    let do_ignorepath = matches.is_present("ignorepath");
+    let do_random = matches.is_present("random");
 
 
     let archive_path = std::path::Path::new(archive);
@@ -42,9 +43,23 @@ fn main() {
 
     let mut zip_archive = zip::ZipArchive::new(zipfile).unwrap();
 
+    let mut indices = (0..zip_archive.len()).collect::<Vec<_>>().into_iter().filter(|x| {
+        //if a filter flag is passed
+        if let Some(f) = filter {
+            let file = zip_archive.by_index(*x).unwrap();
+            return file.name().contains(f)
+        }
+        else {
+            return true
+        }
+    }).collect::<Vec<_>>();
+
+    if do_random {
+        indices = vec![indices.choose(&mut rand::thread_rng()).unwrap_or(&0).clone()];
+    }
     
-    
-    for i in 0..zip_archive.len() {
+    // for i in 0..zip_archive.len() {
+    for i in indices {
         let mut file = zip_archive.by_index(i).unwrap();
         let mut outpath = file.sanitized_name();
 
@@ -56,7 +71,7 @@ fn main() {
         }
 
         // If ignorepath is set, turn the filename into the path
-        if ignorepath {
+        if do_ignorepath {
             if let Some(p) = outpath.file_name() {
                 outpath = PathBuf::from(p);
             }