Johann Woelper 6 years ago
commit
876dc5c172
6 changed files with 131 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 59 0
      Cargo.lock
  3. 8 0
      Cargo.toml
  4. 50 0
      src/main.rs
  5. 6 0
      test/catalog.xml
  6. 6 0
      test/folder/test.xml

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+/target
+**/*.rs.bk

+ 59 - 0
Cargo.lock

@@ -0,0 +1,59 @@
+[[package]]
+name = "iterfolder"
+version = "0.1.0"
+dependencies = [
+ "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "same-file"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "walkdir"
+version = "2.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267"
+"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1"
+"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
+"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9"
+"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 8 - 0
Cargo.toml

@@ -0,0 +1,8 @@
+[package]
+name = "iterfolder"
+version = "0.1.0"
+authors = ["Johann Woelper <woelper@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+walkdir = "*"

+ 50 - 0
src/main.rs

@@ -0,0 +1,50 @@
+#![feature(dbg_macro)]
+use std::ffi::OsStr;
+use std::path::PathBuf;
+use walkdir::WalkDir; //an external lib for traversing dirs //easy path manipulations like os.path on python
+
+// task: count all occurrences of <texture> tags in all xml files
+
+fn main() {
+    // cheap one way to do it
+    let dir = "test";
+
+    // The dirty python way
+    for f in WalkDir::new(&dir) {
+        let file = f.unwrap();
+        if file.path().is_file() {
+            let ext = file.path().extension().unwrap();
+            if ext == "xml" {
+                dbg!(ext);
+            }
+        }
+    }
+
+    // The python + rust safe way
+    for file in WalkDir::new(&dir) {
+        match file {
+            Ok(valid_file) => {
+                match valid_file.path().extension() {
+                    Some(ext) => {
+                        if ext == "xml" {
+                            dbg!(valid_file);
+                            // do something here
+                        }
+                    }
+                    None => (),
+                }
+            }
+            Err(_e) => (),
+        }
+    }
+
+    // The functional way - produce a vec of valid files, then do something with it or pass it along
+    let xml_files: Vec<_> = WalkDir::new(&dir)
+        .into_iter() // creates an iterator
+        .filter_map(|e| e.ok()) // filters out invalid entries
+        .filter(|f| f.path().is_file()) // filters out directories
+        .filter(|f| f.path().extension() == Some(OsStr::new("xml"))) // leaves xml - a bit verbose
+        .collect();
+
+    dbg!(xml_files);
+}

+ 6 - 0
test/catalog.xml

@@ -0,0 +1,6 @@
+<note>
+    <heading>Reminder</heading>
+    <texture>blue.png</texture>
+    <texture>blood.png</texture>
+    <texture>concrete.png</texture>
+</note>

+ 6 - 0
test/folder/test.xml

@@ -0,0 +1,6 @@
+<note>
+    <heading>Reminder</heading>
+    <texture>carpet.png</texture>
+    <texture>leopard.png</texture>
+
+</note>