Getty Ritter 9 months ago
commit
6c0fce3fe7
4 changed files with 80 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 21 0
      Cargo.lock
  3. 11 0
      Cargo.toml
  4. 47 0
      src/main.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/target

+ 21 - 0
Cargo.lock

@@ -0,0 +1,21 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "eliza_error"
+version = "0.99.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fa1037d8a172e26e7c3178e0af0d21515e56f8c85fb1c637cfbd31abbb237c31"
+
+[[package]]
+name = "xml-rs"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"
+
+[[package]]
+name = "xmldir"
+version = "0.1.0"
+dependencies = [
+ "eliza_error",
+ "xml-rs",
+]

+ 11 - 0
Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "xmldir"
+version = "0.1.0"
+authors = ["Getty Ritter <gettylefou@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+xml-rs = "*"
+eliza_error = "*"

+ 47 - 0
src/main.rs

@@ -0,0 +1,47 @@
+use eliza_error::Error;
+use std::path::Path;
+use xml::writer::events::XmlEvent;
+
+struct DirWriter<W: std::io::Write> {
+    writer: xml::writer::EventWriter<W>,
+}
+
+impl<W: std::io::Write> DirWriter<W> {
+    fn walk_root(&mut self) -> Result<(), Error> {
+        self.writer
+            .write(XmlEvent::start_element("dir").attr("name", "."))?;
+        self.walk(&Path::new("."))?;
+        self.writer.write(XmlEvent::end_element())?;
+        Ok(())
+    }
+
+    fn walk(&mut self, path: &Path) -> Result<(), Error> {
+        for entry in std::fs::read_dir(path)? {
+            let entry = entry?;
+            let path = entry.path();
+            let name = path.file_name().unwrap().to_str().unwrap();
+            if path.is_dir() {
+                self.writer
+                    .write(XmlEvent::start_element("dir").attr("name", name))?;
+                self.walk(&path)?;
+                self.writer.write(XmlEvent::end_element())?;
+            } else {
+                self.writer
+                    .write(XmlEvent::start_element("file").attr("name", name))?;
+                let contents = std::fs::read_to_string(path)?;
+                self.writer.write(XmlEvent::characters(&contents))?;
+                self.writer.write(XmlEvent::end_element())?;
+            }
+        }
+        Ok(())
+    }
+}
+
+fn main() -> () {
+    let mut writer = DirWriter {
+        writer: xml::writer::EventWriter::new(std::io::stdout()),
+    };
+    if let Err(err) = writer.walk_root() {
+        eprintln!("shit's fucked: {:?}", err);
+    }
+}