directory assertion type
change slvrzqwopxuyukkwoqvzvsonvolnlqsu
commit 990f52e555638b3cd2a604cb7baf8e9d4075b2a6
author Alpha Chen <alpha@kejadlen.dev>
date
parent wkvynztk
diff --git a/src/main.rs b/src/main.rs
index 7ce4ca0..7b58005 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -99,9 +99,13 @@ impl Default for Registry {
             Debug::new(args).map(|d| Box::new(d) as Box<dyn AssertionType>)
         });
 
+        registry.register("directory", |args| {
+            Directory::new(args).map(|d| Box::new(d) as Box<dyn AssertionType>)
+        });
         registry.register("symlink", |args| {
             Symlink::new(args).map(|s| Box::new(s) as Box<dyn AssertionType>)
         });
+
         registry
     }
 }
@@ -201,6 +205,63 @@ impl AssertionType for Symlink {
     }
 }
 
+struct Directory {
+    path: String,
+}
+
+impl Directory {
+    fn new(args: LuaMultiValue) -> Result<Self> {
+        let args_vec: Vec<LuaValue> = args.into_vec();
+
+        if args_vec.len() != 1 {
+            return Err(FrorkError::InvalidArguments(format!(
+                "Directory requires exactly 1 argument, got {}",
+                args_vec.len()
+            ))
+            .into());
+        }
+
+        let path = args_vec[0]
+            .to_string()
+            .map_err(|_| FrorkError::InvalidArguments("Argument must be a string".to_string()))?;
+
+        let expanded_path = expand_tilde(&path);
+
+        Ok(Self {
+            path: expanded_path,
+        })
+    }
+}
+
+impl std::fmt::Display for Directory {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "directory {}", self.path)
+    }
+}
+
+impl AssertionType for Directory {
+    fn status(&self) -> Result<Status> {
+        let path = Path::new(&self.path);
+        if path.is_dir() {
+            Ok(Status::Ok)
+        } else if path.exists() {
+            todo!(
+                "{}",
+                format!("directory {} exists but is not a directory", self.path)
+            );
+        } else {
+            Ok(Status::Missing)
+        }
+    }
+
+    fn install(&self) -> Result<()> {
+        std::fs::create_dir_all(&self.path)
+            .map_err(|e| eyre!("Failed to create directory: {}", e))?;
+        debug!("created: {}", self);
+        Ok(())
+    }
+}
+
 struct Debug {
     status_fn: Option<LuaFunction>,
     install_fn: Option<LuaFunction>,